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


import java.util.*;

// line 2 "model.ump"
// line 40 "model.ump"
public class First implements I
{

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

  //First Associations
  private List<Second> seconds;

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

  public First()
  {
    seconds = new ArrayList<Second>();
  }

  //------------------------
  // INTERFACE
  //------------------------
  /* Code from template association_GetMany */
  public Second getSecond(int index)
  {
    Second aSecond = seconds.get(index);
    return aSecond;
  }

  public List<Second> getSeconds()
  {
    List<Second> newSeconds = Collections.unmodifiableList(seconds);
    return newSeconds;
  }

  public int numberOfSeconds()
  {
    int number = seconds.size();
    return number;
  }

  public boolean hasSeconds()
  {
    boolean has = seconds.size() > 0;
    return has;
  }

  public int indexOfSecond(Second aSecond)
  {
    int index = seconds.indexOf(aSecond);
    return index;
  }
  /* Code from template association_IsNumberOfValidMethod */
  public boolean isNumberOfSecondsValid()
  {
    boolean isValid = numberOfSeconds() >= minimumNumberOfSeconds() && numberOfSeconds() <= maximumNumberOfSeconds();
    return isValid;
  }
  /* Code from template association_MinimumNumberOfMethod */
  public static int minimumNumberOfSeconds()
  {
    return 1;
  }
  /* Code from template association_MaximumNumberOfMethod */
  public static int maximumNumberOfSeconds()
  {
    return 4;
  }
  /* Code from template association_AddMNToOnlyOne */
  public Second addSecond()
  {
    if (numberOfSeconds() >= maximumNumberOfSeconds())
    {
      return null;
    }
    else
    {
      return new Second(this);
    }
  }

  public boolean addSecond(Second aSecond)
  {
    boolean wasAdded = false;
    if (seconds.contains(aSecond)) { return false; }
    if (numberOfSeconds() >= maximumNumberOfSeconds())
    {
      return wasAdded;
    }

    First existingFirst = aSecond.getFirst();
    boolean isNewFirst = existingFirst != null && !this.equals(existingFirst);

    if (isNewFirst && existingFirst.numberOfSeconds() <= minimumNumberOfSeconds())
    {
      return wasAdded;
    }

    if (isNewFirst)
    {
      aSecond.setFirst(this);
    }
    else
    {
      seconds.add(aSecond);
    }
    wasAdded = true;
    return wasAdded;
  }

  public boolean removeSecond(Second aSecond)
  {
    boolean wasRemoved = false;
    //Unable to remove aSecond, as it must always have a first
    if (this.equals(aSecond.getFirst()))
    {
      return wasRemoved;
    }

    //first already at minimum (1)
    if (numberOfSeconds() <= minimumNumberOfSeconds())
    {
      return wasRemoved;
    }
    seconds.remove(aSecond);
    wasRemoved = true;
    return wasRemoved;
  }
  /* Code from template association_AddIndexControlFunctions */
  public boolean addSecondAt(Second aSecond, int index)
  {  
    boolean wasAdded = false;
    if(addSecond(aSecond))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfSeconds()) { index = numberOfSeconds() - 1; }
      seconds.remove(aSecond);
      seconds.add(index, aSecond);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMoveSecondAt(Second aSecond, int index)
  {
    boolean wasAdded = false;
    if(seconds.contains(aSecond))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfSeconds()) { index = numberOfSeconds() - 1; }
      seconds.remove(aSecond);
      seconds.add(index, aSecond);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addSecondAt(aSecond, index);
    }
    return wasAdded;
  }

  public void delete()
  {
    for(int i=seconds.size(); i > 0; i--)
    {
      Second aSecond = seconds.get(i - 1);
      aSecond.delete();
    }
  }

  @Override
  public boolean setSecond(Second aSecond){
          return false;
  }

  @Override
  public boolean setSeconds(Second... newSeconds){
          return false;
  }

}