sirius.kernel.commons.ValueHolder Maven / Gradle / Ivy
Show all versions of sirius-kernel Show documentation
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.commons;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Provides a mutable value holder
*
* Can be used to create final variable which can be modified from within an inner class.
*
* @param the type of the value contained in the value holder
*/
public class ValueHolder implements Supplier, Consumer {
private T value;
/**
* Creates a new value holder with the given initial value
*
* @param initialValue sets the value of the value holder
*/
public ValueHolder(T initialValue) {
this.value = initialValue;
}
/**
* Creates a new ValueHolder with the given initial value.
*
* This method can be used instead of a constructor, so that the type parameters don't need to be re-typed.
*
* @param initialValue sets the value of the value holder
* @param the type of the value being held by this ValueHolder
* @return a new ValueHolder initialized with the given value.
*/
public static ValueHolder of(T initialValue) {
return new ValueHolder<>(initialValue);
}
@Override
public T get() {
return value;
}
/**
* Sets the value of this value holder
*
* @param value the new value of this value holder
*/
public void set(T value) {
this.value = value;
}
@Override
public String toString() {
return Strings.toString(value);
}
@Override
public void accept(T t) {
this.value = t;
}
}