ru.fix.dynamic.property.api.DelegatedProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dynamic-property-api Show documentation
Show all versions of dynamic-property-api Show documentation
https://github.com/ru-fix/dynamic-property
package ru.fix.dynamic.property.api;
import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.function.Supplier;
/**
* Be aware that DelegatedProperty
* does not notify listeners through {@link PropertyListener}
* All requests to {@link DynamicProperty#get()}
* will be delegated to {@link Supplier#get()} method of the given supplier.
*
* If you need a DynamicProperty with full listener support backed up by {@link Supplier} use DynamicPropertyPoller
* @see ru.fix.dynamic.property.polling.DynamicPropertyPoller
*/
public class DelegatedProperty implements DynamicProperty {
private final Supplier supplier;
public DelegatedProperty(Supplier supplier) {
Objects.requireNonNull(supplier);
this.supplier = supplier;
}
@Override
public T get() {
return supplier.get();
}
@Nonnull
@Override
public PropertySubscription createSubscription() {
return new PropertySubscription() {
@Override
public PropertySubscription setAndCallListener(@Nonnull PropertyListener listener) {
listener.onPropertyChanged(null, supplier.get());
return this;
}
@Override
public void close() {
}
@Override
public T get() {
return supplier.get();
}
};
}
@Override
public void close() {
}
}