⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compositecollection.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2005 JBoss Inc
 * 
 * Licensed 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.drools.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

/**
 * Decorates a collection of other collections to provide a single unified view.
 * <p>
 * Changes made to this collection will actually be made on the decorated
 * collection. Add and remove operations require the use of a pluggable
 * strategy. If no strategy is provided then add and remove are unsupported.
 * 
 * @since Commons Collections 3.0
 * @version $Revision: 1.1 $ $Date: 2005/07/26 01:06:32 $
 * 
 * @author Brian McCallister
 * @author Stephen Colebourne
 * @author Phil Steitz
 */
public class CompositeCollection
    implements
    Collection {

    /** CollectionMutator to handle changes to the collection */
    protected CollectionMutator mutator;

    /** Collections in the composite */
    protected Collection[]      all;

    /**
     * Create an empty CompositeCollection.
     */
    public CompositeCollection() {
        super();
        this.all = new Collection[0];
    }

    /**
     * Create a Composite Collection with only coll composited.
     * 
     * @param coll
     *            a collection to decorate
     */
    public CompositeCollection(final Collection coll) {
        this();
        this.addComposited( coll );
    }

    /**
     * Create a CompositeCollection with colls as the initial list of composited
     * collections.
     * 
     * @param colls
     *            an array of collections to decorate
     */
    public CompositeCollection(final Collection[] colls) {
        this();
        this.addComposited( colls );
    }

    // -----------------------------------------------------------------------
    /**
     * Gets the size of this composite collection.
     * <p>
     * This implementation calls <code>size()</code> on each collection.
     * 
     * @return total number of elements in all contained containers
     */
    public int size() {
        int size = 0;
        for ( int i = this.all.length - 1; i >= 0; i-- ) {
            size += this.all[i].size();
        }
        return size;
    }

    /**
     * Checks whether this composite collection is empty.
     * <p>
     * This implementation calls <code>isEmpty()</code> on each collection.
     * 
     * @return true if all of the contained collections are empty
     */
    public boolean isEmpty() {
        for ( int i = this.all.length - 1; i >= 0; i-- ) {
            if ( this.all[i].isEmpty() == false ) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks whether this composite collection contains the object.
     * <p>
     * This implementation calls <code>contains()</code> on each collection.
     * 
     * @param obj
     *            the object to search for
     * @return true if obj is contained in any of the contained collections
     */
    public boolean contains(final Object obj) {
        for ( int i = this.all.length - 1; i >= 0; i-- ) {
            if ( this.all[i].contains( obj ) ) {
                return true;
            }
        }
        return false;
    }

    /**
     * Gets an iterator over all the collections in this composite.
     * <p>
     * This implementation uses an <code>IteratorChain</code>.
     * 
     * @return an <code>IteratorChain</code> instance which supports
     *         <code>remove()</code>. Iteration occurs over contained
     *         collections in the order they were added, but this behavior
     *         should not be relied upon.
     * @see IteratorChain
     */
    public Iterator iterator() {
        if ( this.all.length == 0 ) {
            return Collections.EMPTY_LIST.iterator();
        }
        final IteratorChain chain = new IteratorChain();
        for ( int i = 0; i < this.all.length; ++i ) {
            chain.addIterator( this.all[i].iterator() );
        }
        return chain;
    }

    /**
     * Returns an array containing all of the elements in this composite.
     * 
     * @return an object array of all the elements in the collection
     */
    public Object[] toArray() {
        final Object[] result = new Object[this.size()];
        int i = 0;
        for ( final Iterator it = this.iterator(); it.hasNext(); i++ ) {
            result[i] = it.next();
        }
        return result;
    }

    /**
     * Returns an object array, populating the supplied array if possible. See
     * <code>Collection</code> interface for full details.
     * 
     * @param array
     *            the array to use, populating if possible
     * @return an array of all the elements in the collection
     */
    public Object[] toArray(final Object[] array) {
        final int size = this.size();
        Object[] result;
        if ( array.length >= size ) {
            result = array;
        } else {
            result = (Object[]) Array.newInstance( array.getClass().getComponentType(),
                                                   size );
        }

        int offset = 0;
        for ( int i = 0; i < this.all.length; ++i ) {
            for ( final Iterator it = this.all[i].iterator(); it.hasNext(); ) {
                result[offset++] = it.next();
            }
        }
        if ( result.length > size ) {
            result[size] = null;
        }
        return result;
    }

    /**
     * Adds an object to the collection, throwing UnsupportedOperationException
     * unless a CollectionMutator strategy is specified.
     * 
     * @param obj
     *            the object to add
     * @return true if the collection was modified
     * @throws UnsupportedOperationException
     *             if CollectionMutator hasn't been set
     * @throws UnsupportedOperationException
     *             if add is unsupported
     * @throws ClassCastException
     *             if the object cannot be added due to its type
     * @throws NullPointerException
     *             if the object cannot be added because its null
     * @throws IllegalArgumentException
     *             if the object cannot be added
     */
    public boolean add(final Object obj) {
        if ( this.mutator == null ) {
            throw new UnsupportedOperationException( "add() is not supported on CompositeCollection without a CollectionMutator strategy" );
        }
        return this.mutator.add( this,
                                 this.all,
                                 obj );
    }

    /**
     * Removes an object from the collection, throwing
     * UnsupportedOperationException unless a CollectionMutator strategy is
     * specified.
     * 
     * @param obj
     *            the object being removed
     * @return true if the collection is changed
     * @throws UnsupportedOperationException
     *             if removed is unsupported
     * @throws ClassCastException
     *             if the object cannot be removed due to its type
     * @throws NullPointerException
     *             if the object cannot be removed because its null
     * @throws IllegalArgumentException
     *             if the object cannot be removed
     */
    public boolean remove(final Object obj) {
        if ( this.mutator == null ) {
            throw new UnsupportedOperationException( "remove() is not supported on CompositeCollection without a CollectionMutator strategy" );
        }
        return this.mutator.remove( this,
                                    this.all,
                                    obj );
    }

    /**
     * Checks whether this composite contains all the elements in the specified
     * collection.
     * <p>
     * This implementation calls <code>contains()</code> for each element in
     * the specified collection.
     * 
     * @param coll

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -