1 package net.pterodactylus.sone.utils;
3 import com.google.common.base.Predicate;
6 * Basic implementation of an {@link Option}.
9 * The type of the option
11 public class DefaultOption<T> implements Option<T> {
13 /** The default value. */
14 private final T defaultValue;
16 /** The current value. */
17 private volatile T value;
20 private Predicate<T> validator;
23 * Creates a new default option.
26 * The default value of the option
28 public DefaultOption(T defaultValue) {
29 this(defaultValue, null);
33 * Creates a new default option.
36 * The default value of the option
38 * The validator for value validation (may be {@code null})
40 public DefaultOption(T defaultValue, Predicate<T> validator) {
41 this.defaultValue = defaultValue;
42 this.validator = validator;
50 return (value != null) ? value : defaultValue;
54 * Returns the real value of the option. This will also return an unset
55 * value (usually {@code null})!
57 * @return The real value of the option
68 public boolean validate(T value) {
69 return (validator == null) || (value == null) || validator.apply(value);
76 public void set(T value) {
77 if ((value != null) && (validator != null) && (!validator.apply(value))) {
78 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");