d7e87593beb4bcffe42b3e492860477e9efade3b
[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 public class TestValue<T> implements Value<T> {
17
18         private final AtomicReference<T> value = new AtomicReference<T>();
19
20         public TestValue(@Nullable T originalValue) {
21                 value.set(originalValue);
22         }
23
24         @Override
25         @Nullable
26         public T getValue() throws ConfigurationException {
27                 return value.get();
28         }
29
30         @Override
31         @Nullable
32         public T getValue(@Nullable T defaultValue) {
33                 final T realValue = value.get();
34                 return (realValue != null) ? realValue : defaultValue;
35         }
36
37         @Override
38         public void setValue(@Nullable T newValue) throws ConfigurationException {
39                 value.set(newValue);
40         }
41
42         @Override
43         public int hashCode() {
44                 return value.hashCode();
45         }
46
47         @Override
48         public boolean equals(Object obj) {
49                 return (obj instanceof TestValue) && Objects.equal(value.get(),
50                                 ((TestValue) obj).value.get());
51         }
52
53         @Override
54         @Nonnull
55         public String toString() {
56                 return String.valueOf(value.get());
57         }
58
59         @Nonnull
60         public static <T> Value<T> from(@Nullable T value) {
61                 return new TestValue<T>(value);
62         }
63
64 }