All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.fix.dynamic.property.api.DelegatedProperty Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
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() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy