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