diff --git a/observable/pom.xml b/observable/pom.xml index 0c99b05..372944b 100644 --- a/observable/pom.xml +++ b/observable/pom.xml @@ -1,25 +1,29 @@ - - - 4.0.0 - - io.github.m-m-m - mmm-value-parent - ${revision} - - mmm-value-observable - jar - ${project.artifactId} - Java module providing observable values. - - - - ${project.groupId} - mmm-value - - - ${project.groupId} - mmm-event - - - + + + 4.0.0 + + io.github.m-m-m + mmm-value-parent + ${revision} + + mmm-value-observable + jar + ${project.artifactId} + Java module providing observable values. + + + + ${project.groupId} + mmm-value + + + ${project.groupId} + mmm-event + + + ${project.groupId} + mmm-base + + + diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/list/ChangeAwareLists.java b/observable/src/main/java/io/github/mmm/value/observable/container/list/ChangeAwareLists.java index af194d0..5588984 100644 --- a/observable/src/main/java/io/github/mmm/value/observable/container/list/ChangeAwareLists.java +++ b/observable/src/main/java/io/github/mmm/value/observable/container/list/ChangeAwareLists.java @@ -1,79 +1,94 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.value.observable.container.list; - -import java.util.List; - -import io.github.mmm.value.observable.container.list.impl.ChangeAwareListImpl; -import io.github.mmm.value.observable.container.list.impl.EmptyChangeAwareList; -import io.github.mmm.value.observable.container.list.impl.ImmutableChangeAwareList; - -/** - * Factory for {@link ChangeAwareList}. - * - * @since 1.0.0 - */ -public final class ChangeAwareLists { - - private ChangeAwareLists() { - - super(); - } - - /** - * @param the type of the elements. - * @return an empty, immutable {@link ChangeAwareList}. - */ - public static ChangeAwareList empty() { - - return EmptyChangeAwareList.INSTANCE; - } - - /** - * @param the type of the elements. - * @return a new empty mutable {@link ChangeAwareList}. - */ - public static ChangeAwareList of() { - - return new ChangeAwareListImpl<>(); - } - - /** - * @param the type of the elements. - * @param capacity the initial capacity of the list. - * @return a new empty mutable {@link ChangeAwareList}. - */ - public static ChangeAwareList of(int capacity) { - - return new ChangeAwareListImpl<>(capacity); - } - - /** - * @param the type of the elements. - * @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify - * this {@link List} afterwards, as this will not trigger modification events. - * @return a new empty mutable {@link ChangeAwareList}. - */ - public static ChangeAwareList of(List list) { - - if (list instanceof ChangeAwareList) { - return (ChangeAwareList) list; - } - return new ChangeAwareListImpl<>(list); - } - - /** - * @param the type of the elements. - * @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify - * this {@link List} afterwards, as this will not trigger modification events. - * @return a new empty mutable {@link ChangeAwareList}. - */ - public static ChangeAwareList ofUnmodifiable(List list) { - - if (list instanceof ImmutableChangeAwareList) { - return (ChangeAwareList) list; - } - return new ImmutableChangeAwareList<>(list); - } - -} +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.list; + +import java.util.List; +import java.util.function.Supplier; + +import io.github.mmm.value.observable.container.list.impl.ChangeAwareListImpl; +import io.github.mmm.value.observable.container.list.impl.EmptyChangeAwareList; +import io.github.mmm.value.observable.container.list.impl.ImmutableChangeAwareList; +import io.github.mmm.value.observable.container.list.impl.ImmutableChangeAwareListView; + +/** + * Factory for {@link ChangeAwareList}. + * + * @since 1.0.0 + */ +public final class ChangeAwareLists { + + private ChangeAwareLists() { + + super(); + } + + /** + * @param the type of the elements. + * @return an empty, immutable {@link ChangeAwareList}. + */ + public static ChangeAwareList empty() { + + return EmptyChangeAwareList.INSTANCE; + } + + /** + * @param the type of the elements. + * @return a new empty mutable {@link ChangeAwareList}. + */ + public static ChangeAwareList of() { + + return new ChangeAwareListImpl<>(); + } + + /** + * @param the type of the elements. + * @param capacity the initial capacity of the list. + * @return a new empty mutable {@link ChangeAwareList}. + */ + public static ChangeAwareList of(int capacity) { + + return new ChangeAwareListImpl<>(capacity); + } + + /** + * @param the type of the elements. + * @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify + * this {@link List} afterwards, as this will not trigger modification events. + * @return a new empty mutable {@link ChangeAwareList}. + */ + public static ChangeAwareList of(List list) { + + if (list instanceof ChangeAwareList) { + return (ChangeAwareList) list; + } + return new ChangeAwareListImpl<>(list); + } + + /** + * @param the type of the elements. + * @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Modifying such + * {@link List} afterwards will not trigger modification events in the read-only view returned by this method. + * @return a unmodifiable {@link ChangeAwareList} that acts as a view on the given {@link List}. + */ + public static ChangeAwareList ofUnmodifiable(List list) { + + if (list instanceof ImmutableChangeAwareList result) { + return result; + } + return new ImmutableChangeAwareList<>(list); + } + + /** + * @param the type of the elements. + * @param listSupplier the {@link Supplier} to the (mutable) {@link List} implementation to wrap as + * {@link ChangeAwareList}. Modifying such {@link List} afterwards will not trigger modification events in the + * read-only view returned by this method. + * @return a unmodifiable {@link ChangeAwareList} that acts as a view on the {@link Supplier#get() supplied} + * {@link List}. + */ + public static ChangeAwareList ofUnmodifiable(Supplier> listSupplier) { + + return new ImmutableChangeAwareListView<>(listSupplier); + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareList.java b/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareList.java index 5190a49..7a2b494 100644 --- a/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareList.java +++ b/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareList.java @@ -1,65 +1,65 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.value.observable.container.list.impl; - -import java.util.Collection; -import java.util.List; - -/** - * Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}. - * - * @param the type of the elements in the container. - * @since 1.0.0 - */ -public class ImmutableChangeAwareList extends ReadOnlyChangeAwareList { - - private final List list; - - /** - * The constructor. - * - * @param list the internal {@link List} to adopt. - */ - public ImmutableChangeAwareList(List list) { - - super(); - this.list = list; - } - - @Override - public E get(int index) { - - return this.list.get(index); - } - - @Override - public int size() { - - return this.list.size(); - } - - @Override - public int indexOf(Object element) { - - return this.list.indexOf(element); - } - - @Override - public int lastIndexOf(Object element) { - - return this.list.lastIndexOf(element); - } - - @Override - public boolean contains(Object element) { - - return this.list.contains(element); - } - - @Override - public boolean containsAll(Collection collection) { - - return this.list.containsAll(collection); - } - -} +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.list.impl; + +import java.util.Collection; +import java.util.List; + +/** + * Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}. + * + * @param the type of the elements in the container. + * @since 1.0.0 + */ +public class ImmutableChangeAwareList extends ReadOnlyChangeAwareList { + + private final List list; + + /** + * The constructor. + * + * @param list the internal {@link List} to adopt. + */ + public ImmutableChangeAwareList(List list) { + + super(); + this.list = list; + } + + @Override + public E get(int index) { + + return this.list.get(index); + } + + @Override + public int size() { + + return this.list.size(); + } + + @Override + public int indexOf(Object element) { + + return this.list.indexOf(element); + } + + @Override + public int lastIndexOf(Object element) { + + return this.list.lastIndexOf(element); + } + + @Override + public boolean contains(Object element) { + + return this.list.contains(element); + } + + @Override + public boolean containsAll(Collection collection) { + + return this.list.containsAll(collection); + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareListView.java b/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareListView.java new file mode 100644 index 0000000..8d87c5e --- /dev/null +++ b/observable/src/main/java/io/github/mmm/value/observable/container/list/impl/ImmutableChangeAwareListView.java @@ -0,0 +1,66 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.list.impl; + +import java.util.Collection; +import java.util.List; +import java.util.function.Supplier; + +/** + * Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}. + * + * @param the type of the elements in the container. + * @since 1.0.0 + */ +public class ImmutableChangeAwareListView extends ReadOnlyChangeAwareList { + + private final Supplier> listSupplier; + + /** + * The constructor. + * + * @param listSupplied the {@link Supplier} of the internal (mutable) {@link List} to adopt. + */ + public ImmutableChangeAwareListView(Supplier> listSupplied) { + + super(); + this.listSupplier = listSupplied; + } + + @Override + public E get(int index) { + + return this.listSupplier.get().get(index); + } + + @Override + public int size() { + + return this.listSupplier.get().size(); + } + + @Override + public int indexOf(Object element) { + + return this.listSupplier.get().indexOf(element); + } + + @Override + public int lastIndexOf(Object element) { + + return this.listSupplier.get().lastIndexOf(element); + } + + @Override + public boolean contains(Object element) { + + return this.listSupplier.get().contains(element); + } + + @Override + public boolean containsAll(Collection collection) { + + return this.listSupplier.get().containsAll(collection); + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/map/ChangeAwareMaps.java b/observable/src/main/java/io/github/mmm/value/observable/container/map/ChangeAwareMaps.java index 775add1..249fdde 100644 --- a/observable/src/main/java/io/github/mmm/value/observable/container/map/ChangeAwareMaps.java +++ b/observable/src/main/java/io/github/mmm/value/observable/container/map/ChangeAwareMaps.java @@ -1,86 +1,101 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.value.observable.container.map; - -import java.util.Map; -import java.util.Set; - -import io.github.mmm.value.observable.container.list.ChangeAwareList; -import io.github.mmm.value.observable.container.map.impl.ChangeAwareMapImpl; -import io.github.mmm.value.observable.container.map.impl.EmptyChangeAwareMap; -import io.github.mmm.value.observable.container.map.impl.ImmutableChangeAwareMap; - -/** - * Factory for {@link ChangeAwareMap}. - * - * @since 1.0.0 - */ -public final class ChangeAwareMaps { - - private ChangeAwareMaps() { - - super(); - } - - /** - * @param type of the {@link Map#containsKey(Object) keys}. - * @param type of the {@link Map#containsValue(Object) values}. - * @return an empty, immutable {@link ChangeAwareMap}. - */ - public static ChangeAwareMap empty() { - - return EmptyChangeAwareMap.INSTANCE; - } - - /** - * @param type of the {@link Map#containsKey(Object) keys}. - * @param type of the {@link Map#containsValue(Object) values}. - * @return a new empty mutable {@link ChangeAwareMap}. - */ - public static ChangeAwareMap of() { - - return new ChangeAwareMapImpl<>(); - } - - /** - * @param type of the {@link Map#containsKey(Object) keys}. - * @param type of the {@link Map#containsValue(Object) values}. - * @param capacity the initial capacity of the map. - * @return a new empty mutable {@link ChangeAwareMap}. - */ - public static ChangeAwareMap of(int capacity) { - - return new ChangeAwareMapImpl<>(capacity); - } - - /** - * @param type of the {@link Map#containsKey(Object) keys}. - * @param type of the {@link Map#containsValue(Object) values}. - * @param map the existing {@link Set} implementation to wrap as {@link ChangeAwareMap}. Please avoid to modify this - * {@link Map} afterwards, as this will not trigger modification events. - * @return a new empty mutable {@link ChangeAwareMap}. - */ - public static ChangeAwareMap of(Map map) { - - if (map instanceof ChangeAwareMap) { - return (ChangeAwareMap) map; - } - return new ChangeAwareMapImpl<>(map); - } - - /** - * @param type of the {@link Map#containsKey(Object) keys}. - * @param type of the {@link Map#containsValue(Object) values}. - * @param map the existing {@link Map} implementation to wrap as {@link ChangeAwareMap}. Please avoid to modify this - * {@link Map} afterwards, as this will not trigger modification events. - * @return a new empty mutable {@link ChangeAwareList}. - */ - public static ChangeAwareMap ofUnmodifiable(Map map) { - - if (map instanceof ImmutableChangeAwareMap) { - return (ChangeAwareMap) map; - } - return new ImmutableChangeAwareMap<>(map); - } - -} +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.map; + +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; + +import io.github.mmm.value.observable.container.map.impl.ChangeAwareMapImpl; +import io.github.mmm.value.observable.container.map.impl.EmptyChangeAwareMap; +import io.github.mmm.value.observable.container.map.impl.ImmutableChangeAwareMap; +import io.github.mmm.value.observable.container.map.impl.ImmutableChangeAwareMapView; + +/** + * Factory for {@link ChangeAwareMap}. + * + * @since 1.0.0 + */ +public final class ChangeAwareMaps { + + private ChangeAwareMaps() { + + super(); + } + + /** + * @param type of the {@link Map#containsKey(Object) keys}. + * @param type of the {@link Map#containsValue(Object) values}. + * @return an empty, immutable {@link ChangeAwareMap}. + */ + public static ChangeAwareMap empty() { + + return EmptyChangeAwareMap.INSTANCE; + } + + /** + * @param type of the {@link Map#containsKey(Object) keys}. + * @param type of the {@link Map#containsValue(Object) values}. + * @return a new empty mutable {@link ChangeAwareMap}. + */ + public static ChangeAwareMap of() { + + return new ChangeAwareMapImpl<>(); + } + + /** + * @param type of the {@link Map#containsKey(Object) keys}. + * @param type of the {@link Map#containsValue(Object) values}. + * @param capacity the initial capacity of the map. + * @return a new empty mutable {@link ChangeAwareMap}. + */ + public static ChangeAwareMap of(int capacity) { + + return new ChangeAwareMapImpl<>(capacity); + } + + /** + * @param type of the {@link Map#containsKey(Object) keys}. + * @param type of the {@link Map#containsValue(Object) values}. + * @param map the existing {@link Set} implementation to wrap as {@link ChangeAwareMap}. Please avoid to modify this + * {@link Map} afterwards, as this will not trigger modification events. + * @return a new empty mutable {@link ChangeAwareMap}. + */ + public static ChangeAwareMap of(Map map) { + + if (map instanceof ChangeAwareMap) { + return (ChangeAwareMap) map; + } + return new ChangeAwareMapImpl<>(map); + } + + /** + * @param type of the {@link Map#containsKey(Object) keys}. + * @param type of the {@link Map#containsValue(Object) values}. + * @param map the existing {@link Map} implementation to wrap as {@link ChangeAwareMap}. Modifying such {@link Map} + * afterwards will not trigger modification events in the read-only view returned by this method. + * @return a unmodifiable {@link ChangeAwareMap} that acts as a view on the given {@link Map}. + */ + public static ChangeAwareMap ofUnmodifiable(Map map) { + + if (map instanceof ImmutableChangeAwareMap) { + return (ChangeAwareMap) map; + } + return new ImmutableChangeAwareMap<>(map); + } + + /** + * @param type of the {@link Map#containsKey(Object) keys}. + * @param type of the {@link Map#containsValue(Object) values}. + * @param mapSupplier the {@link Supplier} to the (mutable) {@link Map} implementation to wrap as + * {@link ChangeAwareMap}. Modifying such {@link Map} afterwards will not trigger modification events in the + * read-only view returned by this method. + * @return a unmodifiable {@link ChangeAwareMap} that acts as a view on the {@link Supplier#get() supplied} + * {@link Map}. + */ + public static ChangeAwareMap ofUnmodifiable(Supplier> mapSupplier) { + + return new ImmutableChangeAwareMapView<>(mapSupplier); + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMap.java b/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMap.java index d39367a..50e1d14 100644 --- a/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMap.java +++ b/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMap.java @@ -1,116 +1,93 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.value.observable.container.map.impl; - -import java.util.AbstractSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/** - * Immutable implementation of {@link io.github.mmm.value.observable.container.map.ChangeAwareMap}. - * - * @param type of the {@link java.util.Map#containsKey(Object) keys}. - * @param type of the {@link java.util.Map#containsValue(Object) values}. - * @since 1.0.0 - */ -public class ImmutableChangeAwareMap extends ReadOnlyChangeAwareMap { - - private final Map map; - - /** - * The constructor. - * - * @param map the internal {@link Map} to adopt. - */ - public ImmutableChangeAwareMap(Map map) { - - super(); - this.map = map; - } - - @Override - public int size() { - - return this.map.size(); - } - - @Override - public boolean containsKey(Object key) { - - return this.map.containsKey(key); - } - - @Override - public boolean containsValue(Object value) { - - return this.map.containsValue(value); - } - - @Override - public V get(Object key) { - - return this.map.get(key); - } - - @Override - public Set> entrySet() { - - return new EntrySet(); - } - - private class EntrySet extends AbstractSet> { - - private final Set> entrySet; - - public EntrySet() { - - super(); - this.entrySet = ImmutableChangeAwareMap.this.map.entrySet(); - } - - @Override - public Iterator> iterator() { - - return new EntrySetIterator<>(this.entrySet.iterator()); - } - - @Override - public void clear() { - - throw new UnsupportedOperationException(); - } - - @Override - public int size() { - - return ImmutableChangeAwareMap.this.map.size(); - } - - } - - private static class EntrySetIterator implements Iterator> { - - private final Iterator> itr; - - public EntrySetIterator(Iterator> itr) { - - super(); - this.itr = itr; - } - - @Override - public boolean hasNext() { - - return this.itr.hasNext(); - } - - @Override - public Entry next() { - - return this.itr.next(); - } - - } - -} +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.map.impl; + +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import io.github.mmm.base.collection.ReadOnlyIterator; + +/** + * Immutable implementation of {@link io.github.mmm.value.observable.container.map.ChangeAwareMap}. + * + * @param type of the {@link java.util.Map#containsKey(Object) keys}. + * @param type of the {@link java.util.Map#containsValue(Object) values}. + * @since 1.0.0 + */ +public class ImmutableChangeAwareMap extends ReadOnlyChangeAwareMap { + + private final Map map; + + /** + * The constructor. + * + * @param map the internal {@link Map} to adopt. + */ + public ImmutableChangeAwareMap(Map map) { + + super(); + this.map = map; + } + + @Override + public int size() { + + return this.map.size(); + } + + @Override + public boolean containsKey(Object key) { + + return this.map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + + return this.map.containsValue(value); + } + + @Override + public V get(Object key) { + + return this.map.get(key); + } + + @Override + public Set> entrySet() { + + return new EntrySet(); + } + + private class EntrySet extends AbstractSet> { + + private final Set> entrySet; + + public EntrySet() { + + super(); + this.entrySet = ImmutableChangeAwareMap.this.map.entrySet(); + } + + @Override + public Iterator> iterator() { + + return new ReadOnlyIterator<>(this.entrySet.iterator()); + } + + @Override + public void clear() { + + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + + return ImmutableChangeAwareMap.this.map.size(); + } + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMapView.java b/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMapView.java new file mode 100644 index 0000000..38a0148 --- /dev/null +++ b/observable/src/main/java/io/github/mmm/value/observable/container/map/impl/ImmutableChangeAwareMapView.java @@ -0,0 +1,94 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.map.impl; + +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; + +import io.github.mmm.base.collection.ReadOnlyIterator; + +/** + * Immutable implementation of {@link io.github.mmm.value.observable.container.map.ChangeAwareMap}. + * + * @param type of the {@link java.util.Map#containsKey(Object) keys}. + * @param type of the {@link java.util.Map#containsValue(Object) values}. + * @since 1.0.0 + */ +public class ImmutableChangeAwareMapView extends ReadOnlyChangeAwareMap { + + private final Supplier> mapSupplier; + + /** + * The constructor. + * + * @param mapSupplier the internal {@link Map} to adopt. + */ + public ImmutableChangeAwareMapView(Supplier> mapSupplier) { + + super(); + this.mapSupplier = mapSupplier; + } + + @Override + public int size() { + + return this.mapSupplier.get().size(); + } + + @Override + public boolean containsKey(Object key) { + + return this.mapSupplier.get().containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + + return this.mapSupplier.get().containsValue(value); + } + + @Override + public V get(Object key) { + + return this.mapSupplier.get().get(key); + } + + @Override + public Set> entrySet() { + + return new EntrySet(); + } + + private class EntrySet extends AbstractSet> { + + private final Set> entrySet; + + public EntrySet() { + + super(); + this.entrySet = ImmutableChangeAwareMapView.this.mapSupplier.get().entrySet(); + } + + @Override + public Iterator> iterator() { + + return new ReadOnlyIterator<>(this.entrySet.iterator()); + } + + @Override + public void clear() { + + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + + return ImmutableChangeAwareMapView.this.mapSupplier.get().size(); + } + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/set/ChangeAwareSets.java b/observable/src/main/java/io/github/mmm/value/observable/container/set/ChangeAwareSets.java index 11c2a29..c102d08 100644 --- a/observable/src/main/java/io/github/mmm/value/observable/container/set/ChangeAwareSets.java +++ b/observable/src/main/java/io/github/mmm/value/observable/container/set/ChangeAwareSets.java @@ -1,81 +1,95 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.value.observable.container.set; - -import java.util.List; -import java.util.Set; - -import io.github.mmm.value.observable.container.list.ChangeAwareList; -import io.github.mmm.value.observable.container.set.impl.ChangeAwareSetImpl; -import io.github.mmm.value.observable.container.set.impl.EmptyChangeAwareSet; -import io.github.mmm.value.observable.container.set.impl.ImmutableChangeAwareSet; - -/** - * Factory for {@link ChangeAwareSet}. - * - * @since 1.0.0 - */ -public final class ChangeAwareSets { - - private ChangeAwareSets() { - - super(); - } - - /** - * @param the type of the elements. - * @return an empty, immutable {@link ChangeAwareSet}. - */ - public static ChangeAwareSet empty() { - - return EmptyChangeAwareSet.INSTANCE; - } - - /** - * @param the type of the elements. - * @return a new empty mutable {@link ChangeAwareSet}. - */ - public static ChangeAwareSet of() { - - return new ChangeAwareSetImpl<>(); - } - - /** - * @param the type of the elements. - * @param capacity the initial capacity of the set. - * @return a new empty mutable {@link ChangeAwareSet}. - */ - public static ChangeAwareSet of(int capacity) { - - return new ChangeAwareSetImpl<>(capacity); - } - - /** - * @param the type of the elements. - * @param set the existing {@link Set} implementation to wrap as {@link ChangeAwareSet}. Please avoid to modify this - * {@link Set} afterwards, as this will not trigger modification events. - * @return a new empty mutable {@link ChangeAwareSet}. - */ - public static ChangeAwareSet of(Set set) { - - if (set instanceof ChangeAwareSet) { - return (ChangeAwareSet) set; - } - return new ChangeAwareSetImpl<>(set); - } - - /** - * @param the type of the elements. - * @param set the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify this - * {@link List} afterwards, as this will not trigger modification events. - * @return a new empty mutable {@link ChangeAwareList}. - */ - public static ChangeAwareSet ofUnmodifiable(Set set) { - - if (set instanceof ImmutableChangeAwareSet) { - return (ChangeAwareSet) set; - } - return new ImmutableChangeAwareSet<>(set); - } - -} +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.set; + +import java.util.List; +import java.util.Set; +import java.util.function.Supplier; + +import io.github.mmm.value.observable.container.list.ChangeAwareList; +import io.github.mmm.value.observable.container.set.impl.ChangeAwareSetImpl; +import io.github.mmm.value.observable.container.set.impl.EmptyChangeAwareSet; +import io.github.mmm.value.observable.container.set.impl.ImmutableChangeAwareSet; +import io.github.mmm.value.observable.container.set.impl.ImmutableChangeAwareSetView; + +/** + * Factory for {@link ChangeAwareSet}. + * + * @since 1.0.0 + */ +public final class ChangeAwareSets { + + private ChangeAwareSets() { + + super(); + } + + /** + * @param the type of the elements. + * @return an empty, immutable {@link ChangeAwareSet}. + */ + public static ChangeAwareSet empty() { + + return EmptyChangeAwareSet.INSTANCE; + } + + /** + * @param the type of the elements. + * @return a new empty mutable {@link ChangeAwareSet}. + */ + public static ChangeAwareSet of() { + + return new ChangeAwareSetImpl<>(); + } + + /** + * @param the type of the elements. + * @param capacity the initial capacity of the set. + * @return a new empty mutable {@link ChangeAwareSet}. + */ + public static ChangeAwareSet of(int capacity) { + + return new ChangeAwareSetImpl<>(capacity); + } + + /** + * @param the type of the elements. + * @param set the existing {@link Set} implementation to wrap as {@link ChangeAwareSet}. Please avoid to modify this + * {@link Set} afterwards, as this will not trigger modification events. + * @return a new empty mutable {@link ChangeAwareSet}. + */ + public static ChangeAwareSet of(Set set) { + + if (set instanceof ChangeAwareSet) { + return (ChangeAwareSet) set; + } + return new ChangeAwareSetImpl<>(set); + } + + /** + * @param the type of the elements. + * @param set the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Modifying such {@link List} + * afterwards will not trigger modification events in the read-only view returned by this method. + * @return an unmodifiable {@link ChangeAwareList} that acts as a view on the given {@link Set}. + */ + public static ChangeAwareSet ofUnmodifiable(Set set) { + + if (set instanceof ImmutableChangeAwareSet) { + return (ChangeAwareSet) set; + } + return new ImmutableChangeAwareSet<>(set); + } + + /** + * @param the type of the elements. + * @param setSupplier the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Modifying such + * {@link List} afterwards will not trigger modification events in the read-only view returned by this method. + * @return an unmodifiable {@link ChangeAwareList} that acts as a view on the {@link Supplier#get() supplied} + * {@link Set}. + */ + public static ChangeAwareSet ofUnmodifiable(Supplier> setSupplier) { + + return new ImmutableChangeAwareSetView<>(setSupplier); + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSet.java b/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSet.java index 9d36332..97569b3 100644 --- a/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSet.java +++ b/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSet.java @@ -1,77 +1,56 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.value.observable.container.set.impl; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -/** - * Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}. - * - * @param the type of the elements in the container. - * @since 1.0.0 - */ -public class ImmutableChangeAwareSet extends ReadOnlyChangeAwareSet { - - private final Set set; - - /** - * The constructor. - * - * @param set the internal {@link Set} to adopt. - */ - public ImmutableChangeAwareSet(Set set) { - - super(); - this.set = set; - } - - @Override - public int size() { - - return this.set.size(); - } - - @Override - public boolean contains(Object element) { - - return this.set.contains(element); - } - - @Override - public boolean containsAll(Collection collection) { - - return this.set.containsAll(collection); - } - - @Override - public Iterator iterator() { - - return new SetIterator<>(this.set.iterator()); - } - - private static class SetIterator implements Iterator { - - private final Iterator itr; - - public SetIterator(Iterator itr) { - - super(); - this.itr = itr; - } - - @Override - public boolean hasNext() { - - return this.itr.hasNext(); - } - - @Override - public E next() { - - return this.itr.next(); - } - } - -} +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.set.impl; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Set; + +import io.github.mmm.base.collection.ReadOnlyIterator; + +/** + * Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}. + * + * @param the type of the elements in the container. + * @since 1.0.0 + */ +public class ImmutableChangeAwareSet extends ReadOnlyChangeAwareSet { + + private final Set set; + + /** + * The constructor. + * + * @param set the internal {@link Set} to adopt. + */ + public ImmutableChangeAwareSet(Set set) { + + super(); + this.set = set; + } + + @Override + public int size() { + + return this.set.size(); + } + + @Override + public boolean contains(Object element) { + + return this.set.contains(element); + } + + @Override + public boolean containsAll(Collection collection) { + + return this.set.containsAll(collection); + } + + @Override + public Iterator iterator() { + + return new ReadOnlyIterator<>(this.set.iterator()); + } + +} diff --git a/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSetView.java b/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSetView.java new file mode 100644 index 0000000..0346ba1 --- /dev/null +++ b/observable/src/main/java/io/github/mmm/value/observable/container/set/impl/ImmutableChangeAwareSetView.java @@ -0,0 +1,57 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.value.observable.container.set.impl; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Set; +import java.util.function.Supplier; + +import io.github.mmm.base.collection.ReadOnlyIterator; + +/** + * Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}. + * + * @param the type of the elements in the container. + * @since 1.0.0 + */ +public class ImmutableChangeAwareSetView extends ReadOnlyChangeAwareSet { + + private final Supplier> setSupplier; + + /** + * The constructor. + * + * @param setSupplier the internal {@link Set} to adopt. + */ + public ImmutableChangeAwareSetView(Supplier> setSupplier) { + + super(); + this.setSupplier = setSupplier; + } + + @Override + public int size() { + + return this.setSupplier.get().size(); + } + + @Override + public boolean contains(Object element) { + + return this.setSupplier.get().contains(element); + } + + @Override + public boolean containsAll(Collection collection) { + + return this.setSupplier.get().containsAll(collection); + } + + @Override + public Iterator iterator() { + + return new ReadOnlyIterator<>(this.setSupplier.get().iterator()); + } + +} diff --git a/observable/src/main/java/module-info.java b/observable/src/main/java/module-info.java index 82f047f..40ccaa9 100644 --- a/observable/src/main/java/module-info.java +++ b/observable/src/main/java/module-info.java @@ -1,75 +1,77 @@ -/* - * Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 - */ -/** - * Provides observable value API and implementation for standard Java types. - */ -module io.github.mmm.value.observable { - - requires transitive io.github.mmm.value; - - requires transitive io.github.mmm.event; - - exports io.github.mmm.value.observable; - - exports io.github.mmm.value.observable.booleans; - - exports io.github.mmm.value.observable.comparable; - - exports io.github.mmm.value.observable.container; - - exports io.github.mmm.value.observable.container.collection; - - exports io.github.mmm.value.observable.container.list; - - exports io.github.mmm.value.observable.container.map; - - exports io.github.mmm.value.observable.container.set; - - exports io.github.mmm.value.observable.enumeration; - - exports io.github.mmm.value.observable.locale; - - exports io.github.mmm.value.observable.number; - - exports io.github.mmm.value.observable.number.bigdecimal; - - exports io.github.mmm.value.observable.number.biginteger; - - exports io.github.mmm.value.observable.number.bytes; - - exports io.github.mmm.value.observable.number.doubles; - - exports io.github.mmm.value.observable.number.floats; - - exports io.github.mmm.value.observable.number.integers; - - exports io.github.mmm.value.observable.number.longs; - - exports io.github.mmm.value.observable.number.shorts; - - exports io.github.mmm.value.observable.object; - - exports io.github.mmm.value.observable.pattern; - - exports io.github.mmm.value.observable.string; - - exports io.github.mmm.value.observable.temporal; - - exports io.github.mmm.value.observable.temporal.duration; - - exports io.github.mmm.value.observable.temporal.instant; - - exports io.github.mmm.value.observable.temporal.localdate; - - exports io.github.mmm.value.observable.temporal.localdatetime; - - exports io.github.mmm.value.observable.temporal.localtime; - - exports io.github.mmm.value.observable.temporal.offsetdatetime; - - exports io.github.mmm.value.observable.temporal.offsettime; - - exports io.github.mmm.value.observable.temporal.zoneddatetime; +/* + * Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 + */ +/** + * Provides observable value API and implementation for standard Java types. + */ +module io.github.mmm.value.observable { + + requires transitive io.github.mmm.base; + + requires transitive io.github.mmm.value; + + requires transitive io.github.mmm.event; + + exports io.github.mmm.value.observable; + + exports io.github.mmm.value.observable.booleans; + + exports io.github.mmm.value.observable.comparable; + + exports io.github.mmm.value.observable.container; + + exports io.github.mmm.value.observable.container.collection; + + exports io.github.mmm.value.observable.container.list; + + exports io.github.mmm.value.observable.container.map; + + exports io.github.mmm.value.observable.container.set; + + exports io.github.mmm.value.observable.enumeration; + + exports io.github.mmm.value.observable.locale; + + exports io.github.mmm.value.observable.number; + + exports io.github.mmm.value.observable.number.bigdecimal; + + exports io.github.mmm.value.observable.number.biginteger; + + exports io.github.mmm.value.observable.number.bytes; + + exports io.github.mmm.value.observable.number.doubles; + + exports io.github.mmm.value.observable.number.floats; + + exports io.github.mmm.value.observable.number.integers; + + exports io.github.mmm.value.observable.number.longs; + + exports io.github.mmm.value.observable.number.shorts; + + exports io.github.mmm.value.observable.object; + + exports io.github.mmm.value.observable.pattern; + + exports io.github.mmm.value.observable.string; + + exports io.github.mmm.value.observable.temporal; + + exports io.github.mmm.value.observable.temporal.duration; + + exports io.github.mmm.value.observable.temporal.instant; + + exports io.github.mmm.value.observable.temporal.localdate; + + exports io.github.mmm.value.observable.temporal.localdatetime; + + exports io.github.mmm.value.observable.temporal.localtime; + + exports io.github.mmm.value.observable.temporal.offsetdatetime; + + exports io.github.mmm.value.observable.temporal.offsettime; + + exports io.github.mmm.value.observable.temporal.zoneddatetime; } \ No newline at end of file