/*______________________________________________________________________________
 * 
 * Copyright 2000-2001 Paul Cantrell
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * (1) Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 * (2) Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution. 
 *
 * (3) The name of the author may not be used to endorse or promote
 *     products derived from this software without specific prior
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *_______________________________________________________________________________
 */

package net.innig.collect;

import java.util.*;

/**
    Finds the difference between two Collections.
    This class returns the delta using {@link java.util.Set},
    and therefore ignores duplicates and reordering.
    <p>
    CollectionDiff optionally creates internal copies of the input Collections.
    If you do not use this option, the results of CollectionDiff methods
    after a subsequent modification of the input collections are undefined.

    <p align="center">
    <table cellpadding=4 cellspacing=2 border=0 bgcolor="#338833" width="90%"><tr><td bgcolor="#EEEEEE">
        <b>Maturity:</b>
        This is a mature API, and a stable implementation.
        It performs well in informal testing, but has not undergone
        methodical or real-world testing.
    </td></tr><tr><td bgcolor="#EEEEEE">
        <b>Plans:</b>
        There are no current plans to expand or revise this class's functionality.
    </td></tr></table>
    
    @author Paul Cantrell
    @version [Development version]
*/

public class CollectionDiff<E>
    {
    /** Prepares a comparison of two collections without creating an
     *  internal copy. */
    
    public CollectionDiff(Collection<? extends E> oldStuff, Collection<? extends E> newStuff)
        { this(oldStuff, newStuff, false); }
    
    /** Prepares a comparison of two collections, optionally creating an
     *  internal copy of the collections to allow subsequent modification
     *  of the collections being compared. */
    
    public CollectionDiff(Collection<? extends E> oldStuff, Collection<? extends E> newStuff, boolean copy)
        {
        this.oldStuff = copy ? oldStuff : new HashSet<E>(oldStuff);
        this.newStuff = copy ? newStuff : new HashSet<E>(newStuff);
        }
    
    /** Returns the objects which are in newStuff but not oldStuff. */
    
    public Set<E> getAdded()
        {
        if(added == null)
            {
            added = new HashSet<E>(newStuff);
            added.removeAll(oldStuff);
            }
        return added;
        }
    
    /** Returns the objects which are in oldStuff but not newStuff. */
    
    public Set<E> getRemoved()
        {
        if(removed == null)	
            {
            removed = new HashSet<E>(oldStuff);
            removed.removeAll(newStuff);
            }
        return removed;
        }
    
    /** Returns the objects which are in both oldStuff and newStuff.
     *  Objects which have changed position in a list are considered "same". */
    
    public Set<E> getSame()
        {
        if(same == null)
            {
            same = new HashSet<E>(oldStuff);
            same.retainAll(newStuff);
            }
        return same;
        }
    
    public boolean sameElements()
        { return getAdded().isEmpty() && getRemoved().isEmpty(); }
    
    /** Returns true if the other object is a collection diff comparing equal collections. */
    
    public boolean equals(Object other)
        {
        if(other == null)
            return false;
        CollectionDiff otherdiff = (CollectionDiff) other;
        return otherdiff.oldStuff.equals(oldStuff)
            && otherdiff.newStuff.equals(newStuff);
        }
    
    public int hashCode()
        {
        return oldStuff.hashCode() * 41 + newStuff.hashCode();
        }
    
    private Collection<? extends E> oldStuff, newStuff;
    private Set<E> added, removed, same;
    }

