daevil.property.Property Maven / Gradle / Ivy
package daevil.property;
import java.util.function.Function;
import java.util.function.Supplier;
public class Property {
private final Function setter;
private Supplier getter;
public Property(Supplier getter, Function setter) {
this.getter = getter;
this.setter = setter;
}
public static Writeonly set(Function setter) {
Property prop = new Property(() -> {
throw new UnsupportedOperationException();
}, setter);
return prop::set;
}
public static PropertyBuilder get(Supplier getter) {
return new PropertyBuilder() {
public Property set(Function setter) {
return new Property(getter, setter);
}
public Readonly readonly() {
Property prop = new Property(getter, Function.identity());
return prop::get;
}
};
}
public T set(T value) {
return setter.apply(value);
}
public T get() {
return getter.get();
}
public Named named() {
return new GuessesName(this.getter, this.setter);
}
public Named named(String name) {
return new ExplicitName(this.getter, this.setter, name);
}
public interface PropertyBuilder {
public Property set(Function setter);
public Readonly readonly();
}
public String toString(){
Object value = getter.get();
if(value instanceof String){
return value.toString();
}
return getter.get().toString();
}
}