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

brooklyn.util.collections.MutableList 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.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ImmutableList;

public class MutableList extends ArrayList {
    private static final long serialVersionUID = -5533940507175152491L;

    public static  MutableList of() {
        return new MutableList();
    }
    
    public static  MutableList of(V v1) {
        MutableList result = new MutableList();
        result.add(v1);
        return result;
    }
    
    public static  MutableList of(V v1, V v2, V ...vv) {
        MutableList result = new MutableList();
        result.add(v1);
        result.add(v2);
        for (V v: vv) result.add(v);
        return result;
    }

    public static  MutableList copyOf(Iterable orig) {
        return new MutableList(orig);
    }
    
    public MutableList() {
    }
    
    public MutableList(Iterable source) {
        super((source instanceof Collection) ? (Collection)source : ImmutableList.copyOf(source));
    }
    
    public ImmutableList toImmutable() {
        return ImmutableList.copyOf(this);
    }
    
    public static  Builder builder() {
        return new Builder();
    }

    /**
     * @see guava's ImMutableList.Builder
     */
    public static class Builder {
        final MutableList result = new MutableList();

        public Builder() {}

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

        public Builder add(V value1, V value2, V ...values) {
            result.add(value1);
            result.add(value2);
            for (V v: values) result.add(v);
            return this;
        }

        public Builder remove(V val) {
            result.remove(val);
            return this;
        }
        
        /** @deprecated since 0.6.0 ambiguous with {@link #addAll(Iterable)}; 
         * use {@link #add(Object, Object, Object...)} */ 
        @Deprecated
        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 removeAll(Iterable iterable) {
            if (iterable instanceof Collection) {
                result.removeAll((Collection) iterable);
            } else {
                for (V v : iterable) {
                    result.remove(v);
                }
            }
            return this;
        }

        /** @deprecated since 0.6.0 ambiguous with {@link #removeAll(Iterable)}; 
         * use removeAll(Arrays.asList(Object, Object, Object...)) */ 
        @Deprecated
        public Builder removeAll(V... values) {
            for (V v : values) {
                result.remove(v);
            }
            return this;
        }

        public MutableList build() {
          return new MutableList(result);
        }
        
        public ImmutableList buildImmutable() {
            return ImmutableList.copyOf(result);
        }
    }
    
    /** as {@link List#add(Object)} but fluent style */
    public MutableList append(V item) {
        add(item);
        return this;
    }

    /** as {@link List#add(Object)} but excluding nulls, and fluent style */
    public MutableList appendIfNotNull(V item) {
        if (item!=null) add(item);
        return this;
    }

    /** as {@link List#add(Object)} but accepting multiple, and fluent style */
    public MutableList append(V item1, V item2, V ...items) {
        add(item1);
        add(item2);
        for (V item: items) add(item);
        return this;
    }

    /** as {@link List#add(Object)} but excluding nulls, accepting multiple, and fluent style */
    public MutableList appendIfNotNull(V item1, V item2, V ...items) {
        if (item1!=null) add(item1);
        if (item2!=null) add(item2);
        for (V item: items) 
            if (item!=null) add(item);
        return this;
    }

    /** as {@link List#addAll(Collection)} but fluent style */
    public MutableList appendAll(Iterable items) {
        for (V item: items) add(item);
        return this;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy