/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE @UMPLE_VERSION@ modeling language!*/

package example;
import java.util.*;

public class Mentor
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Mentor Associations
  private Course main;
  private List<Course> secondaries;

  //Helper Variables
  private int cachedHashCode;
  private boolean canSetMain;
  private boolean canSetSecondaries;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public Mentor()
  {
    cachedHashCode = -1;
    canSetMain = true;
    canSetSecondaries = true;
    secondaries = new ArrayList<Course>();
  }

  //------------------------
  // INTERFACE
  //------------------------

  public Course getMain()
  {
    return main;
  }

  public boolean hasMain()
  {
    boolean has = main != null;
    return has;
  }

  public Course getSecondary(int index)
  {
    Course aSecondary = secondaries.get(index);
    return aSecondary;
  }

  public List<Course> getSecondaries()
  {
    List<Course> newSecondaries = Collections.unmodifiableList(secondaries);
    return newSecondaries;
  }

  public int numberOfSecondaries()
  {
    int number = secondaries.size();
    return number;
  }

  public boolean hasSecondaries()
  {
    boolean has = secondaries.size() > 0;
    return has;
  }

  public int indexOfSecondary(Course aSecondary)
  {
    int index = secondaries.indexOf(aSecondary);
    return index;
  }

  public boolean setMain(Course aNewMain)
  {
    boolean wasSet = false;
    if (!canSetMain) { return false; }
    if (aNewMain == null)
    {
      Course existingMain = main;
      main = null;
      
      if (existingMain != null && existingMain.getMentorMain() != null)
      {
        existingMain.setMentorMain(null);
      }
      wasSet = true;
      return wasSet;
    }

    Course currentMain = getMain();
    if (currentMain != null && !currentMain.equals(aNewMain))
    {
      currentMain.setMentorMain(null);
    }

    main = aNewMain;
    Mentor existingMentorMain = aNewMain.getMentorMain();

    if (!equals(existingMentorMain))
    {
      aNewMain.setMentorMain(this);
    }
    wasSet = true;
    return wasSet;
  }

  public static int minimumNumberOfSecondaries()
  {
    return 0;
  }

  public boolean addSecondary(Course aSecondary)
  {
    boolean wasAdded = false;
    if (!canSetSecondaries) { return false; }
    if (secondaries.contains(aSecondary)) { return false; }
    Mentor existingMentorSecondary = aSecondary.getMentorSecondary();
    if (existingMentorSecondary == null)
    {
      aSecondary.setMentorSecondary(this);
    }
    else if (!this.equals(existingMentorSecondary))
    {
      existingMentorSecondary.removeSecondary(aSecondary);
      addSecondary(aSecondary);
    }
    else
    {
      secondaries.add(aSecondary);
    }
    wasAdded = true;
    return wasAdded;
  }

  public boolean removeSecondary(Course aSecondary)
  {
    boolean wasRemoved = false;
    if (!canSetSecondaries) { return false; }
    if (secondaries.contains(aSecondary))
    {
      secondaries.remove(aSecondary);
      aSecondary.setMentorSecondary(null);
      wasRemoved = true;
    }
    return wasRemoved;
  }

  public boolean addSecondaryAt(Course aSecondary, int index)
  {  
    boolean wasAdded = false;
    if(addSecondary(aSecondary))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfSecondaries()) { index = numberOfSecondaries() - 1; }
      secondaries.remove(aSecondary);
      secondaries.add(index, aSecondary);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMoveSecondaryAt(Course aSecondary, int index)
  {
    boolean wasAdded = false;
    if(secondaries.contains(aSecondary))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfSecondaries()) { index = numberOfSecondaries() - 1; }
      secondaries.remove(aSecondary);
      secondaries.add(index, aSecondary);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addSecondaryAt(aSecondary, index);
    }
    return wasAdded;
  }

  public boolean equals(Object obj)
  {
    if (obj == null) { return false; }
    if (!getClass().equals(obj.getClass())) { return false; }

    Mentor compareTo = (Mentor)obj;
  
    if (getMain() == null && compareTo.getMain() != null)
    {
      return false;
    }
    else if (getMain() != null && !getMain().equals(compareTo.getMain()))
    {
      return false;
    }

    if (getSecondaries().size() != compareTo.getSecondaries().size())
    {
      return false;
    }

    for (int i=0; i<getSecondaries().size(); i++)
    {
      Course me = getSecondaries().get(i);
      Course them = compareTo.getSecondaries().get(i);
      if (me == null && them != null)
      {
       return false;
      }
      else if (me != null && !me.equals(them))
      {
        return false;
      }
    }

    return true;
  }

  public int hashCode()
  {
    if (cachedHashCode != -1)
    {
      return cachedHashCode;
    }
    cachedHashCode = 17;
    if (getMain() != null)
    {
      cachedHashCode = cachedHashCode * 23 + getMain().hashCode();
    }
    else
    {
      cachedHashCode = cachedHashCode * 23;
    }
    if (getSecondaries() != null)
    {
      cachedHashCode = cachedHashCode * 23 + getSecondaries().hashCode();
    }
    else
    {
      cachedHashCode = cachedHashCode * 23;
    }

    canSetMain = false;
    canSetSecondaries = false;
    return cachedHashCode;
  }

  public void delete()
  {
    if (main != null)
    {
      main.setMentorMain(null);
    }
    while( !secondaries.isEmpty() )
    {
      secondaries.get(0).setMentorSecondary(null);
    }
  }

}
