454163ed2cc39dacc8b8bea9c5e82e6e8209701f
[Sone.git] / src / test / java / net / pterodactylus / sone / test / TestValue.java
1 package net.pterodactylus.sone.test;
2
3 import java.util.concurrent.atomic.AtomicReference;
4
5 import javax.annotation.Nonnull;
6 import javax.annotation.Nullable;
7
8 import net.pterodactylus.util.config.ConfigurationException;
9 import net.pterodactylus.util.config.Value;
10
11 import com.google.common.base.Objects;
12
13 /**
14  * Simple {@link Value} implementation.
15  *
16  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
17  */
18 public class TestValue<T> implements Value<T> {
19
20         private final AtomicReference<T> value = new AtomicReference<T>();
21
22         public TestValue(@Nullable T originalValue) {
23                 value.set(originalValue);
24         }
25
26         @Override
27         @Nullable
28         public T getValue() throws ConfigurationException {
29                 return value.get();
30         }
31
32         @Override
33         @Nullable
34         public T getValue(@Nullable T defaultValue) {
35                 final T realValue = value.get();
36                 return (realValue != null) ? realValue : defaultValue;
37         }
38
39         @Override
40         public void setValue(@Nullable T newValue) throws ConfigurationException {
41                 value.set(newValue);
42         }
43
44         @Override
45         public int hashCode() {
46                 return value.hashCode();
47         }
48
49         @Override
50         public boolean equals(Object obj) {
51                 return (obj instanceof TestValue) && Objects.equal(value.get(),
52                                 ((TestValue) obj).value.get());
53         }
54
55         @Override
56         @Nonnull
57         public String toString() {
58                 return String.valueOf(value.get());
59         }
60
61         @Nonnull
62         public static <T> Value<T> from(@Nullable T value) {
63                 return new TestValue<T>(value);
64         }
65
66 }