/*
 *
 * Copyright (c) 2007
 * Adrian Michel
 * http://www.tradery.com
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Adrian Michel makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
*/

package  com.tradery.collections;

import java.util.Hashtable;
import java.util.Enumeration;
import java.lang.Integer;

import com.tradery.contract.Contract;

/**
 * Set of unique objects implemented as a hastable
 */
public class SetAsHashtable extends Hashtable
{
  /**
   * puts a new object in the set. No duplicates are stored. The duplicate status is determined
   * based on the method equals implemented by the class.
   * 
   * @param object The object to be added to the set
   * @return If the set already had an object equal to the new object (where equal depends
   *         on the semantics defined by the object's class), returns the old object, otherwise
   *         returns null
   */
  public Object put( Object object )
  {
    if( Contract.REQUIRE )
      Contract.require( object != null );
    
    return super.put( object, object );
  }
  
  /**
   * Creates a reunion of the current set and another set, and the reunion will
   * be stored in the current set. No duplicates are stored.
   * 
   * @param set    The source set
   * @return The current set which holds the reunion of the two sets
   */
  public SetAsHashtable reunion( SetAsHashtable set )
  {
    if( Contract.REQUIRE )
      Contract.require( set != null );
    
    for( Enumeration e = set.elements() ; e.hasMoreElements() ;) 
      put( e.nextElement() );
    return this;
  }

  /**
   * Retrieves an enumeration of all objects in the set.
   * 
   * @return The enumeration of the objects in the sets
   */
  public Enumeration getElements()
  {
    return keys();
  }
}