/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

package example;
import java.util.*;

// line 3 "../CascadeDelete.ump"
public class B
{

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

  //B Associations
  private List<C> myC;

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

  public B()
  {
    myC = new ArrayList<C>();
  }

  //------------------------
  // INTERFACE
  //------------------------
  /* Code from template association_GetMany */
  public C getMyC(int index)
  {
    C aMyC = myC.get(index);
    return aMyC;
  }

  public List<C> getMyC()
  {
    List<C> newMyC = Collections.unmodifiableList(myC);
    return newMyC;
  }

  public int numberOfMyC()
  {
    int number = myC.size();
    return number;
  }

  public boolean hasMyC()
  {
    boolean has = myC.size() > 0;
    return has;
  }

  public int indexOfMyC(C aMyC)
  {
    int index = myC.indexOf(aMyC);
    return index;
  }
  /* Code from template association_MinimumNumberOfMethod */
  public static int minimumNumberOfMyC()
  {
    return 0;
  }
  /* Code from template association_AddManyToOne */
  public C addMyC(D aMyD)
  {
    return new C(aMyD, this);
  }

  public boolean addMyC(C aMyC)
  {
    boolean wasAdded = false;
    if (myC.contains(aMyC)) { return false; }
    B existingB = aMyC.getB();
    boolean isNewB = existingB != null && !this.equals(existingB);
    if (isNewB)
    {
      aMyC.setB(this);
    }
    else
    {
      myC.add(aMyC);
    }
    wasAdded = true;
    return wasAdded;
  }

  public boolean removeMyC(C aMyC)
  {
    boolean wasRemoved = false;
    //Unable to remove aMyC, as it must always have a b
    if (!this.equals(aMyC.getB()))
    {
      myC.remove(aMyC);
      wasRemoved = true;
    }
    return wasRemoved;
  }
  /* Code from template association_AddIndexControlFunctions */
  public boolean addMyCAt(C aMyC, int index)
  {  
    boolean wasAdded = false;
    if(addMyC(aMyC))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfMyC()) { index = numberOfMyC() - 1; }
      myC.remove(aMyC);
      myC.add(index, aMyC);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMoveMyCAt(C aMyC, int index)
  {
    boolean wasAdded = false;
    if(myC.contains(aMyC))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfMyC()) { index = numberOfMyC() - 1; }
      myC.remove(aMyC);
      myC.add(index, aMyC);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addMyCAt(aMyC, index);
    }
    return wasAdded;
  }

  public void delete()
  {
    while (myC.size() > 0)
    {
      C aMyC = myC.get(myC.size() - 1);
      aMyC.delete();
      myC.remove(aMyC);
    }
    
  }

}
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

package example;

// line 6 "../CascadeDelete.ump"
public class C
{

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

  //C Associations
  private D myD;
  private B b;

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

  public C(D aMyD, B aB)
  {
    if (aMyD == null || aMyD.getC() != null)
    {
      throw new RuntimeException("Unable to create C due to aMyD. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
    myD = aMyD;
    boolean didAddB = setB(aB);
    if (!didAddB)
    {
      throw new RuntimeException("Unable to create myC due to b. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
  }

  public C(B aB)
  {
    myD = new D(this);
    boolean didAddB = setB(aB);
    if (!didAddB)
    {
      throw new RuntimeException("Unable to create myC due to b. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
  }

  //------------------------
  // INTERFACE
  //------------------------
  /* Code from template association_GetOne */
  public D getMyD()
  {
    return myD;
  }
  /* Code from template association_GetOne */
  public B getB()
  {
    return b;
  }
  /* Code from template association_SetOneToMany */
  public boolean setB(B aB)
  {
    boolean wasSet = false;
    if (aB == null)
    {
      return wasSet;
    }

    B existingB = b;
    b = aB;
    if (existingB != null && !existingB.equals(aB))
    {
      existingB.removeMyC(this);
    }
    b.addMyC(this);
    wasSet = true;
    return wasSet;
  }

  public void delete()
  {
    D existingMyD = myD;
    myD = null;
    if (existingMyD != null)
    {
      existingMyD.delete();
    }
    B placeholderB = b;
    this.b = null;
    if(placeholderB != null)
    {
      placeholderB.removeMyC(this);
    }
  }

}
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

package example;

// line 1 "../CascadeDelete.ump"
public class D
{

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

  //D Associations
  private C c;

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

  public D(C aC)
  {
    if (aC == null || aC.getMyD() != null)
    {
      throw new RuntimeException("Unable to create D due to aC. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
    c = aC;
  }

  public D(B aBForC)
  {
    c = new C(this, aBForC);
  }

  //------------------------
  // INTERFACE
  //------------------------
  /* Code from template association_GetOne */
  public C getC()
  {
    return c;
  }

  public void delete()
  {
    C existingC = c;
    c = null;
    if (existingC != null)
    {
      existingC.delete();
    }
  }

}