/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The OWL2Prova library is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package rbsla.owl.converter.dlp;

import java.util.*;

/**
 * Container for individual mapping
 * @version 0.1 <1 Feb 2005>
 * @since 0.1
 */
public class IndividualList {
	
	  protected LinkedList IndiList;
	  
		/**
		 * Constructor
		 */
	  
	  public IndividualList () {
		IndiList = new LinkedList();
	  }
	  
		/**
		 * Constructor
		 * @param indi a String which is added to the IndividualList
		 */
	  
	  public IndividualList(String indi) {
	  	IndiList = new LinkedList();
	    this.addIndi(indi);
	  }
	  
		/**
		 * Constructor
		 * @param indiList a IndividualList which is added to the IndividualList
		 */
	  
	  public IndividualList(IndividualList indiList) {
	  	IndiList = new LinkedList();
	    this.addIndiList(indiList);
	  }

		/**
		 * add a String element at the end of the list
		 * @param indi String to add
		 * @return boolean
		 */
	  
	  public boolean addIndi (String indi) {
	    return IndiList.add( indi );
	  }
	  
		/**
		 * add a List element at the end of the list
		 * @param indiList List to add
		 * @return boolean
		 */
	  
	  public boolean addIndiList (IndividualList indiList) {
	    return IndiList.add( indiList );
	  }

	  public void addIndiAt (int index, String indi){
	    IndiList.add(index, indi);
	  }
	  public void addIndiListAt (int index, IndividualList indiList){
	    IndiList.add(index, indiList);
	  }
	  
	  public void addIndiFirst (String indi){
	    IndiList.addFirst( indi );
	  }
	  public void addIndiListFirst (IndividualList indiList){
	    IndiList.addFirst( indiList );
	  }

	  public void addIndiLast (String indi){
	    IndiList.addLast(indi );
	  }
	  public void addIndiListLast (IndividualList indiList){
	    IndiList.addLast(indiList);
	  }

		/**
		 * Query if the first element is a String element
		 * @return boolean
		 */
	  
	  public boolean isFirstString(){
	  	if (IndiList.getFirst() instanceof String){
	  		return true;
	  	}else{
	  		return false;
	  	}
	  }
	  
		/**
		 * get the first element of the list. It can be either a String or a List element.
		 */
	  
	  public String getFirstIndi(){
	    return (String) IndiList.getFirst();
	  }
	  public IndividualList getFirstIndiList(){
	    return (IndividualList) IndiList.getFirst();
	  }

		/**
		 * Query if the last element is a String element
		 * @return boolean
		 */
	  
	  public boolean isLastString(){
	  	if (IndiList.getLast() instanceof String){
	  		return true;
	  	}else{
	  		return false;
	  	}
	  }	  
	  public String getLastIndi(){
	    return (String) IndiList.getLast();
	  }	  
	  public IndividualList getLastIndiList(){
	    return (IndividualList) IndiList.getLast();
	  }

		/**
		 * Query if the element with the given index is a String element
		 * @param index an Integer
		 * @return boolean
		 */
	  
	  public boolean isString(int index){
	  	if (IndiList.get(index) instanceof String){
	  		return true;
	  	}else{
	  		return false;
	  	}
	  }	 
	  public String getIndi(int index){
	    return (String) IndiList.get(index);
	  }
	  public IndividualList getIndiList(int index){
	    return (IndividualList) IndiList.get(index);
	  }

		/**
		 * clear the list
		 */
	  
	  public void emptyList(){
	    IndiList.clear();
	  }

		/**
		 * Methodes to remove elements at first, last and at a index
		 */
	  
	  public void removeElementAt(int index){
	    IndiList.remove(index);
	  }

	  public String removeFirstIndi(){
	    return (String) IndiList.removeFirst();
	  }
	  public IndividualList removeFirstIndiList(){
	    return (IndividualList) IndiList.removeFirst();
	  }

	  public String removeLastIndi(){
	    return (String) IndiList.removeLast();
	  }
	  public IndividualList removeLastIndiList(){
	    return (IndividualList) IndiList.removeLast();
	  }

	  public boolean removeIndi (String indi) {
	    return IndiList.remove(indi);
	  }
	  public boolean removeIndiList (IndividualList indiList) {
	    return IndiList.remove(indiList);
	  }

		/**
		 * Query if the list contains a special String element
		 * @param indi the String element
		 * @return boolean
		 */
	  
	  public boolean containsIndi (String indi){
	    return IndiList.contains(indi);
	  }
	  
		/**
		 * Query if the list contains a special list element
		 * @param indiList the list element
		 * @return boolean
		 */
	  
	  public boolean containsIndiList (IndividualList indiList){
	    return IndiList.contains(indiList);
	  }

	  public int sizeOfList (){
	    return IndiList.size();
	  }

	  public void listToString (){
	    for (int i=0; i < this.sizeOfList(); i++){
	    	if (this.isString(i)){
	    		System.out.println(this.getIndi(i));
	    	}else{
	    		System.out.print("[");
	    		this.getIndiList(i).listToString();
	    		System.out.println("]");
	    	}
	    }
	  }

	  public boolean isEmpty (){
	    return IndiList.isEmpty();
	  }
	  
		/**
		 * Query if a special Element is a element of the list or of one of the containing lists
		 * @param indi the String element
		 * @return boolean
		 */
	  
	  public boolean isElementOfList (String element){
	  	boolean result = false;
	  	for (int i=0; i < this.sizeOfList(); i++){
	  		if (this.isString(i)){
	  			if (this.getIndi(i).equals(element)){
	  				return true;
	  			}
	  		}else{
	  			IndividualList tmp = this.getIndiList(i);
	  			result = tmp.isElementOfList(element);
	  			if (result){
	  				return true;
	  			}
	  		}
	  	}
	  	return result;
	  }
	  
		/**
		 * Special case of adding an element to the IndividualList. 
		 * It is used in phase 3 in IdentifyMapper.
		 * @param ToAdd String element to add
		 * @param Selector existing element in a list element
		 * @return boolean
		 */
	  
	  public boolean phase3add(String ToAdd, String Selector){
	  	for (int i=0; i < this.sizeOfList(); i++){
	  		if (!this.isString(i)){
	  			if (this.getIndiList(i).isElementOfList(Selector)){
	  				this.getIndiList(i).addIndi(ToAdd);
	  				return true;
	  			}
	  		}
	  	}
	  	return false;
	  }
	  
		/**
		 * Returns the List which contains the String element
		 * @param indi the String element
		 * @return IndividualList which contains the String element
		 */
	  
	  public IndividualList getIndividualListOfElement(String element){
	  	for (int i = 0; i < this.sizeOfList(); i++){
	  		IndividualList tmp = this.getIndiList(i);
	  		if (tmp.isElementOfList(element)){
	  			return this.getIndiList(i);
	  		}
	  	}
	  	return new IndividualList();
	  }
	  
		/**
		 * Removes the List which contains the String element
		 * @param indi the String element
		 */
	  
	  public void removeIndividualListOfElement(String element){
	  	for (int i = 0; i < this.sizeOfList(); i++){
	  		IndividualList tmp = this.getIndiList(i);
	  		if (tmp.isElementOfList(element)){
	  			this.removeElementAt(i);
	  		}
	  	}
	  }
	  
		/**
		 * merges the IndividualList of two specific String elements and adds the merged
		 * list to the IndividualList
		 * @param element1 the String element which must be contained by the first list
		 * @param element2 the String element which must be contained by the second list
		 */
	  
	  public void mergeIndividualLists (String element1, String element2){
	  	for (int i = 0; i < this.sizeOfList(); i++){
	  		IndividualList tmp1 = this.getIndiList(i);
	  		if (tmp1.isElementOfList(element1)){
	  			this.removeElementAt(i);
	  			for (int j = 0; j < this.sizeOfList(); j++){
	  				IndividualList tmp2 = this.getIndiList(j);
	  				if (tmp2.isElementOfList(element2)){
	  					this.removeElementAt(j);
	  					this.addIndiList(this.merge(tmp1, tmp2));
	  				}
	  			}
	  		}
	  	}
	  }
	  
	  /**
		 * merges two given IndividualLists
		 * @param indiList1 first list to merge
		 * @param indiList2 second list to merge
		 * @return IndividualList is the merged list
		 */
	  
	  public IndividualList merge (IndividualList indiList1, IndividualList indiList2){
	  	IndividualList returnList = new IndividualList();
	  	for (int i = 0; i < indiList1.sizeOfList(); i++){
	  		returnList.addIndi(indiList1.getIndi(i));
	  	}
	  	for (int i = 0; i < indiList2.sizeOfList(); i++){
	  		returnList.addIndi(indiList2.getIndi(i));
	  	}
	  	return returnList;
	  }
	
}
