groovy.util.ObservableSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy Show documentation
Show all versions of groovy Show documentation
Groovy: A powerful multi-faceted language for the JVM
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package groovy.util;
import groovy.lang.Closure;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Stack;
/**
* Set decorator that will trigger PropertyChangeEvents when a value changes.
* An optional Closure may be specified and will work as a filter, if it returns true the property
* will trigger an event (if the value indeed changed), otherwise it won't. The Closure may receive
* 1 or 2 parameters, the single one being the value, the other one both the key and value, for
* example:
*
* // skip all properties whose value is a closure
* def set = new ObservableSet( {!(it instanceof Closure)} )
* <p/>
* // skip all properties whose name matches a regex
* def set = new ObservableSet( { name, value -> !(name =˜ /[A-Z+]/) } )
*
* The current implementation will trigger specialized events in the following scenarios, you need
* not register a different listener as those events extend from PropertyChangeEvent
*
* - ObservableSet.ElementAddedEvent - a new element is added to the set
* - ObservableSet.ElementRemovedEvent - an element is removed from the set
* - ObservableSet.ElementUpdatedEvent - an element changes value (same as regular
* PropertyChangeEvent)
* - ObservableSet.ElementClearedEvent - all elements have been removed from the list
* - ObservableSet.MultiElementAddedEvent - triggered by calling set.addAll()
* - ObservableSet.MultiElementRemovedEvent - triggered by calling
* set.removeAll()/set.retainAll()
*
*
*
* Bound properties
*
* - content - read-only.
* - size - read-only.
*
*/
public class ObservableSet implements Set {
private final Set delegate;
private final PropertyChangeSupport pcs;
private final Closure test;
public static final String SIZE_PROPERTY = "size";
public static final String CONTENT_PROPERTY = "content";
public ObservableSet() {
this(new HashSet(), null);
}
public ObservableSet(Set delegate) {
this(delegate, null);
}
public ObservableSet(Closure test) {
this(new HashSet(), test);
}
public ObservableSet(Set delegate, Closure test) {
this.delegate = delegate;
this.test = test;
this.pcs = new PropertyChangeSupport(this);
}
public Set getContent() {
return Collections.unmodifiableSet(delegate);
}
protected Set getDelegateSet() {
return delegate;
}
protected Closure getTest() {
return test;
}
protected void fireElementAddedEvent(Object element) {
fireElementEvent(new ElementAddedEvent(this, element));
}
protected void fireMultiElementAddedEvent(List values) {
fireElementEvent(new MultiElementAddedEvent(this, values));
}
protected void fireElementClearedEvent(List values) {
fireElementEvent(new ElementClearedEvent(this, values));
}
protected void fireElementRemovedEvent(Object element) {
fireElementEvent(new ElementRemovedEvent(this, element));
}
protected void fireMultiElementRemovedEvent(List values) {
fireElementEvent(new MultiElementRemovedEvent(this, values));
}
protected void fireElementEvent(ElementEvent event) {
pcs.firePropertyChange(event);
}
protected void fireSizeChangedEvent(int oldValue, int newValue) {
pcs.firePropertyChange(new PropertyChangeEvent(this, SIZE_PROPERTY, oldValue, newValue));
}
// observable interface
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcs.addPropertyChangeListener(propertyName, listener);
}
public PropertyChangeListener[] getPropertyChangeListeners() {
return pcs.getPropertyChangeListeners();
}
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
return pcs.getPropertyChangeListeners(propertyName);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcs.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcs.removePropertyChangeListener(propertyName, listener);
}
public boolean hasListeners(String propertyName) {
return pcs.hasListeners(propertyName);
}
@Override
public int size() {
return delegate.size();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public boolean contains(Object o) {
return delegate.contains(o);
}
@Override
public Iterator iterator() {
return new ObservableIterator(delegate.iterator());
}
@Override
public Object[] toArray() {
return delegate.toArray();
}
@Override
public T[] toArray(T[] ts) {
return (T[]) delegate.toArray(ts);
}
@Override
public boolean add(E e) {
int oldSize = size();
boolean success = delegate.add(e);
if (success) {
if (test != null) {
Object result = test.call(e);
if (result instanceof Boolean && (Boolean) result) {
fireElementAddedEvent(e);
fireSizeChangedEvent(oldSize, size());
}
} else {
fireElementAddedEvent(e);
fireSizeChangedEvent(oldSize, size());
}
}
return success;
}
@Override
public boolean remove(Object o) {
int oldSize = size();
boolean success = delegate.remove(o);
if (success) {
fireElementRemovedEvent(o);
fireSizeChangedEvent(oldSize, size());
}
return success;
}
@Override
public boolean containsAll(Collection> objects) {
return delegate.containsAll(objects);
}
@Override
public boolean addAll(Collection extends E> c) {
Set duplicates = new HashSet();
if (null != c) {
for (E e : c) {
if (!delegate.contains(e)) continue;
duplicates.add(e);
}
}
int oldSize = size();
boolean success = delegate.addAll(c);
if (success && c != null) {
List values = new ArrayList();
for (E element : c) {
if (test != null) {
Object result = test.call(element);
if (result instanceof Boolean && (Boolean) result && !duplicates.contains(element)) {
values.add(element);
}
} else if (!duplicates.contains(element)) {
values.add(element);
}
}
if (!values.isEmpty()) {
fireMultiElementAddedEvent(values);
fireSizeChangedEvent(oldSize, size());
}
}
return success;
}
@Override
public boolean retainAll(Collection> c) {
if (c == null) {
return false;
}
List values = new ArrayList();
// GROOVY-7822 use Set for O(1) performance for contains
if (!(c instanceof Set)) {
c = new HashSet