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

package example;

// line 3 "../SingletonToOneTest.ump"
public class Student
{

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

  //Student Attributes
  private String number;

  //Student Associations
  private Mentor mentor;

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

  public Student(String aNumber, Mentor aMentor)
  {
    number = aNumber;
    boolean didAddMentor = setMentor(aMentor);
    if (!didAddMentor)
    {
      throw new RuntimeException("Unable to create student due to mentor. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
  }

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

  public boolean setNumber(String aNumber)
  {
    boolean wasSet = false;
    number = aNumber;
    wasSet = true;
    return wasSet;
  }

  public String getNumber()
  {
    return number;
  }
  /* Code from template association_GetOne */
  public Mentor getMentor()
  {
    return mentor;
  }
  /* Code from template association_SetOneToOptionalOne */
  public boolean setMentor(Mentor aNewMentor)
  {
    boolean wasSet = false;
    if (aNewMentor == null)
    {
      //Unable to setMentor to null, as student must always be associated to a mentor
      return wasSet;
    }
    
    Student existingStudent = aNewMentor.getStudent();
    if (existingStudent != null && !equals(existingStudent))
    {
      //Unable to setMentor, the current mentor already has a student, which would be orphaned if it were re-assigned
      return wasSet;
    }
    
    Mentor anOldMentor = mentor;
    mentor = aNewMentor;
    mentor.setStudent(this);

    if (anOldMentor != null)
    {
      anOldMentor.setStudent(null);
    }
    wasSet = true;
    return wasSet;
  }

  public void delete()
  {
    Mentor existingMentor = mentor;
    mentor = null;
    if (existingMentor != null)
    {
      existingMentor.setStudent(null);
    }
  }


  public String toString()
  {
    return super.toString() + "["+
            "number" + ":" + getNumber()+ "]" + System.getProperties().getProperty("line.separator") +
            "  " + "mentor = "+(getMentor()!=null?Integer.toHexString(System.identityHashCode(getMentor())):"null");
  }
}
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

package example;

// line 8 "../SingletonToOneTest.ump"
public class Mentor
{

  //------------------------
  // STATIC VARIABLES
  //------------------------

  private static Mentor theInstance = null;

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

  //Mentor Attributes
  private String name;

  //Mentor Associations
  private Student student;

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

  private Mentor()
  {
    name = null;
  }

  public static Mentor getInstance()
  {
    if(theInstance == null)
    {
      theInstance = new Mentor();
    }
    return theInstance;
  }

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

  public boolean setName(String aName)
  {
    boolean wasSet = false;
    name = aName;
    wasSet = true;
    return wasSet;
  }

  public String getName()
  {
    return name;
  }
  /* Code from template association_GetOne */
  public Student getStudent()
  {
    return student;
  }

  public boolean hasStudent()
  {
    boolean has = student != null;
    return has;
  }
  /* Code from template association_SetOptionalOneToOne */
  public boolean setStudent(Student aNewStudent)
  {
    boolean wasSet = false;
    if (student != null && !student.equals(aNewStudent) && equals(student.getMentor()))
    {
      //Unable to setStudent, as existing student would become an orphan
      return wasSet;
    }

    student = aNewStudent;
    Mentor anOldMentor = aNewStudent != null ? aNewStudent.getMentor() : null;

    if (!this.equals(anOldMentor))
    {
      if (anOldMentor != null)
      {
        anOldMentor.student = null;
      }
      if (student != null)
      {
        student.setMentor(this);
      }
    }
    wasSet = true;
    return wasSet;
  }

  public void delete()
  {
    Student existingStudent = student;
    student = null;
    if (existingStudent != null)
    {
      existingStudent.delete();
    }
  }


  public String toString()
  {
    return super.toString() + "["+
            "name" + ":" + getName()+ "]" + System.getProperties().getProperty("line.separator") +
            "  " + "student = "+(getStudent()!=null?Integer.toHexString(System.identityHashCode(getStudent())):"null");
  }
}