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

brooklyn.util.collections.MutableSet Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 0.7.0-M1
Show newest version
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 orig) {
        return new MutableSet(orig);
    }
    
    public MutableSet() {
    }
    
    public MutableSet(Iterable source) {
        super((source instanceof Collection) ? (Collection)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 iterable) {
            if (iterable instanceof Collection) {
                result.addAll((Collection) 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 iterable) {
            if (iterable instanceof Collection) {
                result.removeAll((Collection) 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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy