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

org.apache.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: 1.1.0
Show newest version
/*
 * 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 org.apache.brooklyn.util.collections;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.Nullable;

import org.apache.brooklyn.util.exceptions.Exceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

public class MutableSet extends LinkedHashSet {
    
    private static final long serialVersionUID = 2330133488446834595L;
    private static final Logger log = LoggerFactory.getLogger(MutableSet.class);

    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, @SuppressWarnings("unchecked") V ...vMore) {
        MutableSet result = new MutableSet();
        result.add(v1);
        result.add(v2);
        result.add(v3);
        for (V vi: vMore) result.add(vi);
        return result;
    }

    public static  MutableSet copyOf(@Nullable Iterable orig) {
        return orig==null ? new MutableSet() : new MutableSet(orig);
    }
    
    public static  MutableSet copyOf(@Nullable Iterator elements) {
        if (elements == null || !elements.hasNext()) {
            return of();
        }
        return new MutableSet.Builder().addAll(elements).build();
    }
    
    public MutableSet() {
    }
    
    public MutableSet(Iterable source) {
        super((source instanceof Collection) ? (Collection)source : Sets.newLinkedHashSet(source));
    }
    
    /** as {@link MutableList#asImmutableCopy()()} */
    public Set asImmutableCopy() {
        try {
            return ImmutableSet.copyOf(this);
        } catch (Exception e) {
            Exceptions.propagateIfFatal(e);
            log.warn("Error converting list to Immutable, using unmodifiable instead: "+e, e);
            return asUnmodifiableCopy();
        }
    }
    /** as {@link MutableList#asUnmodifiable()} */
    public Set asUnmodifiable() {
        return Collections.unmodifiableSet(this);
    }
    /** as {@link MutableList#asUnmodifiableCopy()} */
    public Set asUnmodifiableCopy() {
        return Collections.unmodifiableSet(MutableSet.copyOf(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 addIfNotNull(V value) {
            if (value != null) result.add(value);
            return this;
        }

        public Builder add(V value) {
            result.add(value);
            return this;
        }

        public Builder add(V v1, V v2, @SuppressWarnings("unchecked") V ...values) {
            result.add(v1);
            result.add(v2);
            for (V value: values) result.add(value);
            return this;
        }

        public Builder remove(V val) {
            result.remove(val);
            return this;
        }
        
        public Builder addAll(V[] values) {
            for (V v : values) {
                result.add(v);
            }
            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(Iterator iter) {
            while (iter.hasNext()) {
                add(iter.next());
            }
            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 retainAll(Iterable iterable) {
            if (iterable instanceof Collection) {
                result.retainAll((Collection) iterable);
            } else {
                Set toretain = Sets.newHashSet(iterable);
                result.retainAll(toretain);
            }
            return this;
        }
        
        public MutableSet build() {
          return new MutableSet(result);
        }
        
    }
    
    public boolean addIfNotNull(V e) {
        if (e!=null) return add(e);
        return false;
    }

    public boolean addAll(Iterable setToAdd) {
        // copy of parent, but accepting Iterable and null
        if (setToAdd==null) return false;
        boolean modified = false;
        Iterator e = setToAdd.iterator();
        while (e.hasNext()) {
            if (add(e.next()))
                modified = true;
        }
        return modified;
    }
    
    /** as {@link #addAll(Collection)} but fluent style and permitting null */
    public MutableSet putAll(Iterable setToAdd) {
        if (setToAdd!=null) addAll(setToAdd);
        return this;
    }

    /** as {@link #add(V)} but fluent style */
    public MutableSet put(V e) {
        add(e);
        return this;
    }

    /** as {@link #addIfNotNull(V)} but fluent style */
    public MutableSet putIfNotNull(V e) {
        if (e!=null) add(e);
        return this;
    }

    public boolean removeIfNotNull(V item) {
        if (item==null) return false;
        return remove(item);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy