/** The LinkSet class stores a set of links
*
* @version 0.24, 11/20/98
* Copyright 1998 Simon St.Laurent 
* Information at: http://www.simonstl.com/projects/xlinkfilter/
* This code is licensed by Simon St.Laurent under the Mozilla Public License.
* See http://www.mozilla.org/MPL/ for details.
* No warranty provided - use at your own risk
*/

/* Modification history
* 11/14/98 Delegated to vector instead of inheritance - Toivo Lainevool
* 11/16/98 Added size and linkAt methods - Toivo Lainevool
* 11/17/98 Added PrintAllLinks method for command line testing - Simon St.Laurent
* 11/17/98 Changed this to newLinks in addLinkSet() - SSL
* 11/20/98 Added isUnique() method to check uniqueness of links - SSL
* 11/20/98 Made addLink() check for duplicates with isUnique() - SSL
*/

package com.simonstl.sax.xlink; 

import java.util.Vector;
import java.util.Enumeration;

public class LinkSet
   {
   
   /*constructor*/
   public LinkSet() 
      {
      }

   /** Adds a link to the Set
    * @param newLink the link to add
    */
   public void addLink(Link newLink) 
      {
      //check for/remove duplicates
      if (isUnique(newLink)) {
	 linkSet_.addElement( newLink );
	 }
      }

   
   /** Gets a set of link that match a URL
    * @param URL the URL to match
    * @return the set of matching Links
    */
   public LinkSet getByURL(String URL)
      {
      LinkSet returnLinks;
      Link linkTemp;
      int numLinks;

      returnLinks=new LinkSet();

      Enumeration enum = this.getAllLinks();
      while(enum.hasMoreElements()) 
         {
         linkTemp=(Link)enum.nextElement();
         if (linkTemp.getOriginURL().equals(URL))
            {returnLinks.addLink(linkTemp);}
         }
      return returnLinks;
      }

   
   /** Gets a set of link that match a location.
    * @param loc the location to match
    * @return the set of matching Links
    */
   public LinkSet getByLoc(String loc)
      {
      LinkSet returnLinks;
      Link linkTemp;
      int numLinks;

      returnLinks=new LinkSet();

      Enumeration enum = this.getAllLinks();
      while(enum.hasMoreElements()) 
         {
         linkTemp=(Link)enum.nextElement();
         if (linkTemp.getOriginLoc().equals(loc))
            {returnLinks.addLink(linkTemp);}
         }
      return returnLinks;
      }

   
   /** Get a set of links that match a URL and location.
    * @param URL the URL to match
    * @param loc the location to match
    * @return the set of matching Links
    */
   public LinkSet getByURLAndLoc(String URL, String loc)
      {
      LinkSet returnLinks;
      Link linkTemp;
      int numLinks;

      returnLinks=new LinkSet();

      Enumeration enum = this.getAllLinks();
      while(enum.hasMoreElements()) 
         {
         linkTemp=(Link)enum.nextElement();
         if ( (linkTemp.getOriginURL().equals(URL)) &&
(linkTemp.getOriginLoc().equals(loc)))
            {
            returnLinks.addLink(linkTemp);
            }
         }
      return returnLinks;
      }

   //Utility functions

   /** toString - for debugging
    * @return the string representation of this LinkSet
    */
   public String toString() 
      {
      Enumeration enum = this.getAllLinks();
      Link linkTemp;
      
      String string = new String();
      while(enum.hasMoreElements()) 
         {
         linkTemp=(Link)enum.nextElement();
         string += linkTemp.toString() + "\n";
         }
      return string;
      }

   
   /** Add a set of links to this linkSet
    * @param newLinks set to add to this one.
    */
   public void addLinkSet(LinkSet newLinks) 
      {
      Enumeration enum = newLinks.getAllLinks();
      Link linkTemp;
      
      while( enum.hasMoreElements() )
         {
         linkTemp = (Link)enum.nextElement();
         this.addLink( linkTemp );
         }
      }
   
   
   /** Gets an enumerator for all the links in this set
    * @return an Enumeration for all of the links in this set
    */
   public Enumeration getAllLinks()
      {
      return linkSet_.elements();
      }


   /** Gets an link for at a specified position
    * @return an Link
    */
   public Link linkAt( int index )
      {
      return (Link)linkSet_.elementAt( index );
      }

   
   /** Gets the size of the linkSet
    * @return an Link
    */
   public int size()
      {
      return linkSet_.size();
      }


   /** Gets a set of link that match a URL
    * @param URL the URL to match
    * @return the set of matching Links
    */
   public boolean isUnique(Link testLink)
      {
      Link linkTemp;
      int numLinks;
      boolean result;
      
      result=true;

      Enumeration enum = this.getAllLinks();
      while((enum.hasMoreElements()) && (result)) 
         {
         linkTemp=(Link)enum.nextElement();
         if (linkTemp.equals(testLink))
            {result=false;}
         }
      return result;
      }   
   /** Remove all the links in this set
    */
   public void removeAllLinks()
      {
      linkSet_.removeAllElements();
      }

/** Prints LinkSet - mostly for debugging
*/
   public void printAllLinks () 
	{
	  int numLinks=this.size();
	  Link linkTemp;

	  for (int i=0; i<numLinks; i++) {	
		linkTemp=this.linkAt(i);
		linkTemp.print();
	  }
	}   
   
   private Vector linkSet_ = new Vector();

   }
