brooklyn.util.collections.MutableSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-utils-common Show documentation
Show all versions of brooklyn-utils-common Show documentation
Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else
package brooklyn.util.collections;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import com.google.common.collect.Sets;
public class MutableSet extends LinkedHashSet {
private static final long serialVersionUID = 2330133488446834595L;
public static MutableSet of() {
return new MutableSet();
}
public static MutableSet of(V v1) {
MutableSet result = new MutableSet();
result.add(v1);
return result;
}
public static MutableSet of(V v1, V v2) {
MutableSet result = new MutableSet();
result.add(v1);
result.add(v2);
return result;
}
public static MutableSet of(V v1, V v2, V v3) {
MutableSet result = new MutableSet();
result.add(v1);
result.add(v2);
result.add(v3);
return result;
}
public static MutableSet copyOf(Iterable extends V> orig) {
return new MutableSet(orig);
}
public MutableSet() {
}
public MutableSet(Iterable extends V> source) {
super((source instanceof Collection) ? (Collection extends V>)source : Sets.newLinkedHashSet(source));
}
public Set toImmutable() {
// Don't use ImmutableSet as that does not accept nulls
return Collections.unmodifiableSet(Sets.newLinkedHashSet(this));
}
public static Builder builder() {
return new Builder();
}
/**
* @see guava's ImmutableSet.Builder
*/
public static class Builder {
final MutableSet result = new MutableSet();
public Builder() {}
public Builder add(V value) {
result.add(value);
return this;
}
public Builder remove(V val) {
result.remove(val);
return this;
}
public Builder addAll(Iterable extends V> iterable) {
if (iterable instanceof Collection) {
result.addAll((Collection extends V>) iterable);
} else {
for (V v : iterable) {
result.add(v);
}
}
return this;
}
public Builder addAll(V... values) {
for (V v : values) {
result.add(v);
}
return this;
}
public Builder removeAll(Iterable extends V> iterable) {
if (iterable instanceof Collection) {
result.removeAll((Collection extends V>) iterable);
} else {
for (V v : iterable) {
result.remove(v);
}
}
return this;
}
public Builder removeAll(V... values) {
for (V v : values) {
result.remove(v);
}
return this;
}
public MutableSet build() {
return new MutableSet(result);
}
}
}