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
10 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
12 public class DefaultOption<T> implements Option<T> {
14 /** The default value. */
15 private final T defaultValue;
17 /** The current value. */
18 private volatile T value;
21 private Predicate<T> validator;
24 * Creates a new default option.
27 * The default value of the option
29 public DefaultOption(T defaultValue) {
30 this(defaultValue, null);
34 * Creates a new default option.
37 * The default value of the option
39 * The validator for value validation (may be {@code null})
41 public DefaultOption(T defaultValue, Predicate<T> validator) {
42 this.defaultValue = defaultValue;
43 this.validator = validator;
51 return (value != null) ? value : defaultValue;
55 * Returns the real value of the option. This will also return an unset
56 * value (usually {@code null})!
58 * @return The real value of the option
69 public boolean validate(T value) {
70 return (validator == null) || (value == null) || validator.apply(value);
77 public void set(T value) {
78 if ((value != null) && (validator != null) && (!validator.apply(value))) {
79 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");