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

gov.sandia.cognition.collection.DefaultMultiCollection Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                DefaultMultiCollection.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright March 24, 2006, Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
 * or on behalf of the U.S. Government. Export of this program may require a
 * license from the United States Government. See CopyrightHistory.txt for
 * complete details.
 *
 */

package gov.sandia.cognition.collection;

import gov.sandia.cognition.annotation.CodeReview;
import gov.sandia.cognition.annotation.CodeReviews;
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * The {@code DefaultMultiCollection} class implements a {@code Collection} that just
 * contains a set of internal collections inside. This allows for easy 
 * operations on as set of collections on the same object type.
 *
 * @param  Type of item stored in this class
 * @author Justin Basilico
 * @since 1.0
 */
@CodeReviews(
    reviews =
    {
        @CodeReview(
            reviewer = "Kevin R. Dixon",
            date = "2008-02-08",
            changesNeeded = false,
            comments = "Looks fine."
        )
        ,
        @CodeReview(
            reviewer = "Kevin R. Dixon",
            date = "2007-12-10",
            changesNeeded = false,
            comments = "Minor updates to the javadoc."
        )
        ,
        @CodeReview(
            reviewer = "Kevin R. Dixon",
            date = "2006-07-18",
            changesNeeded = false,
            comments = "Looks fine."
        )
    }
)
public class DefaultMultiCollection
    extends AbstractCollection
    implements Serializable, MultiCollection
{

    /** The set of collections that backs this collection. */
    private List> collections;

    /**
     * Creates a new instance of {@code DefaultMultiCollection}.
     *
     * @param  first The first collection to add.
     * @param  second The second collection to add.
     */
    public DefaultMultiCollection(
        final Collection first,
        final Collection second)
    {
        super();

        // Create the collections list and add the two new collections.
        this.setCollections(new ArrayList>());
        this.collections.add(first);
        this.collections.add(second);
    }

    /**
     * Creates a new instance of {@code DefaultMultiCollection}.
     *
     * @param  collections The Collection of Collections to add.
     */
    public DefaultMultiCollection(
        final Collection> collections)
    {
        super();

        this.setCollections(
            new ArrayList>(collections) );
    }

    @Override
    public boolean contains(
        Object o)
    {
        // An object is contained in this collection if any of the internal
        // collections contain it.
        for (Collection collection : this.collections)
        {
            if (collection.contains(o))
            {
                return true;
            }
        }

        return false;
    }

    public int size()
    {
        // Calculate the size by summing the size of the internal collections.
        int sizeSum = 0;

        for (Collection collection : this.collections)
        {
            sizeSum += collection.size();
        }

        return sizeSum;
    }

    public int getSubCollectionsCount()
    {
        return this.collections.size();
    }

    public Iterator iterator()
    {
        return new MultiIterator(this.collections);
    }

    public Collection> subCollections()
    {
        return this.collections;
    }

    /**
     * Sets the internal collections.
     *
     * @param  collections The internal collections.
     */
    private void setCollections(
        final List> collections)
    {
        this.collections = collections;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy