Repository: incubator-tamaya Updated Branches: refs/heads/master a60570e8e -> 644a7b1de
TAMAYA-34: Removed metainfo from ConfiguredValue. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/644a7b1d Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/644a7b1d Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/644a7b1d Branch: refs/heads/master Commit: 644a7b1de511cb575bd4f4c117dd289a52e6888c Parents: a60570e Author: anatole <anat...@apache.org> Authored: Fri Dec 26 02:37:09 2014 +0100 Committer: anatole <anat...@apache.org> Committed: Fri Dec 26 02:37:09 2014 +0100 ---------------------------------------------------------------------- .../java/org/apache/tamaya/ConfiguredValue.java | 9 +- .../core/config/DefaultConfiguredValue.java | 158 +++++++++++++++++++ 2 files changed, 160 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/644a7b1d/api/src/main/java/org/apache/tamaya/ConfiguredValue.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/tamaya/ConfiguredValue.java b/api/src/main/java/org/apache/tamaya/ConfiguredValue.java index 839e67e..e75cd51 100644 --- a/api/src/main/java/org/apache/tamaya/ConfiguredValue.java +++ b/api/src/main/java/org/apache/tamaya/ConfiguredValue.java @@ -28,7 +28,8 @@ import java.util.function.Predicate; import java.util.function.Supplier; /** - * A accessor for a single configured value. This can be used to support values that may be reinjected, reconfigured. + * A accessor for a single configured value. This can be used to support values that may be reinjected, reconfigured or + * final. * <h3>Implementation Requirements</h3> * Instances of this class must be * <ul> @@ -84,12 +85,6 @@ public interface ConfiguredValue<T> { void removeListener(Consumer<PropertyChangeEvent> l); /** - * Get some descriptive meta info on the current value. - * @return the meta info, not null. - */ - String getMetaInfo(); - - /** * Evaluate if the item value has been updated since the last access. * @return true, if item value has been updated since the last access. */ http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/644a7b1d/core/src/main/java/org/apache/tamaya/core/config/DefaultConfiguredValue.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/config/DefaultConfiguredValue.java b/core/src/main/java/org/apache/tamaya/core/config/DefaultConfiguredValue.java new file mode 100644 index 0000000..eae4057 --- /dev/null +++ b/core/src/main/java/org/apache/tamaya/core/config/DefaultConfiguredValue.java @@ -0,0 +1,158 @@ +package org.apache.tamaya.core.config; + +import org.apache.tamaya.ConfiguredValue; +import org.apache.tamaya.annotation.LoadPolicy; + +import java.beans.PropertyChangeEvent; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +/** + * Implementation of a configured value (in progress). + */ +public class DefaultConfiguredValue<T> implements ConfiguredValue<T>{ + + private LoadPolicy loadPolicy = LoadPolicy.INITIAL; + private AtomicLong lastUpdate; + private AtomicLong lastAccess; + private Optional<T> value; + + public static final DefaultConfiguredValue EMPTY = new DefaultConfiguredValue(null); + + /** + * Returns an empty {@code Optional} instance. No value is present for this + * Optional. + * + * @apiNote Though it may be tempting to do so, avoid testing if an object + * is empty by comparing with {@code ==} against instances returned by + * {@code Option.empty()}. There is no guarantee that it is a singleton. + * Instead, use {@link #isPresent()}. + * + * @param <T> Type of the non-existent value + * @return an empty {@code Optional} + */ + public static <T> DefaultConfiguredValue<T> empty() { + DefaultConfiguredValue v = (DefaultConfiguredValue<T>) EMPTY; + return v; + } + + private DefaultConfiguredValue(Optional<T> item){ + this.value = item; + } + + public static <T> ConfiguredValue<T> of(T instance){ + return new DefaultConfiguredValue(Optional.of(instance)); + } + + public static <T> ConfiguredValue<T> ofNullable(T value){ + return value == null ? empty() : of(value); + } + + @Override + public LoadPolicy getLoadPolicy() { + return loadPolicy; + } + + @Override + public long getLastAccess() { + return lastAccess.get(); + } + + @Override + public long getLastUpdate() { + return lastUpdate.get(); + } + + @Override + public boolean isUpdatedSince(long timestamp) { + return getLastUpdate()>timestamp; + } + + @Override + public boolean isAccessedSince(long timestamp) { + return getLastAccess()>timestamp; + } + + @Override + public void addListener(Consumer<PropertyChangeEvent> l) { +// TODO + } + + @Override + public void removeListener(Consumer<PropertyChangeEvent> l) { +// TODO + } + + @Override + public T get() { + return value.get(); + } + + @Override + public void update() { +// TODO + } + + @Override + public boolean isPresent() { + return value.isPresent(); + } + + @Override + public void ifPresent(Consumer<? super T> consumer) { + value.ifPresent(consumer); + } + + @Override + public ConfiguredValue<T> filter(Predicate<? super T> predicate) { + Objects.requireNonNull(predicate); + if (!isPresent()) + return this; + else + return predicate.test(value.get()) ? this : empty(); + } + + @Override + public <U> ConfiguredValue<U> map(Function<? super T, ? extends U> mapper) { + Objects.requireNonNull(mapper); + if (!isPresent()) + return empty(); + else { + return DefaultConfiguredValue.ofNullable(mapper.apply(value.get())); + } + } + + @Override + public <U> ConfiguredValue<U> flatMap(Function<? super T, ConfiguredValue<U>> mapper) { + Objects.requireNonNull(mapper); + if (!isPresent()) + return empty(); + else { + return Objects.requireNonNull(mapper.apply(value.get())); + } + } + + @Override + public T orElse(T other) { + return value.orElse(other); + } + + @Override + public T orElseGet(Supplier<? extends T> other) { + return value.orElseGet(other); + } + + @Override + public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { + return value.orElseThrow(exceptionSupplier); + } + + public Optional<T> toOptional(){ + return value; + } +}