/*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 "../OptionalOneToMNTest.ump" public class Mentor { //------------------------ // MEMBER VARIABLES //------------------------ //Mentor Attributes private String name; //Mentor Associations private List<Student> students; //------------------------ // CONSTRUCTOR //------------------------ public Mentor(String aName, Student... allStudents) { name = aName; students = new ArrayList<Student>(); boolean didAddStudents = setStudents(allStudents); if (!didAddStudents) { throw new RuntimeException("Unable to create Mentor, must have 3 to 4 students. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html"); } } //------------------------ // INTERFACE //------------------------ public boolean setName(String aName) { boolean wasSet = false; name = aName; wasSet = true; return wasSet; } public String getName() { return name; } /* Code from template association_GetMany */ public Student getStudent(int index) { Student aStudent = students.get(index); return aStudent; } public List<Student> getStudents() { List<Student> newStudents = Collections.unmodifiableList(students); return newStudents; } public int numberOfStudents() { int number = students.size(); return number; } public boolean hasStudents() { boolean has = students.size() > 0; return has; } public int indexOfStudent(Student aStudent) { int index = students.indexOf(aStudent); return index; } /* Code from template association_MinimumNumberOfMethod */ public static int minimumNumberOfStudents() { return 3; } /* Code from template association_MaximumNumberOfMethod */ public static int maximumNumberOfStudents() { return 4; } /* Code from template association_AddMNToOptionalOne */ public boolean addStudent(Student aStudent) { boolean wasAdded = false; if (students.contains(aStudent)) { return false; } if (numberOfStudents() >= maximumNumberOfStudents()) { return wasAdded; } Mentor existingMentor = aStudent.getMentor(); if (existingMentor != null && existingMentor.numberOfStudents() <= minimumNumberOfStudents()) { return wasAdded; } else if (existingMentor != null) { existingMentor.students.remove(aStudent); } students.add(aStudent); setMentor(aStudent,this); wasAdded = true; return wasAdded; } public boolean removeStudent(Student aStudent) { boolean wasRemoved = false; if (students.contains(aStudent) && numberOfStudents() > minimumNumberOfStudents()) { students.remove(aStudent); setMentor(aStudent,null); wasRemoved = true; } return wasRemoved; } /* Code from template association_SetMNToOptionalOne */ public boolean setStudents(Student... newStudents) { boolean wasSet = false; if (newStudents.length < minimumNumberOfStudents() || newStudents.length > maximumNumberOfStudents()) { return wasSet; } ArrayList<Student> checkNewStudents = new ArrayList<Student>(); HashMap<Mentor,Integer> mentorToNewStudents = new HashMap<Mentor,Integer>(); for (Student aStudent : newStudents) { if (checkNewStudents.contains(aStudent)) { return wasSet; } else if (aStudent.getMentor() != null && !this.equals(aStudent.getMentor())) { Mentor existingMentor = aStudent.getMentor(); if (!mentorToNewStudents.containsKey(existingMentor)) { mentorToNewStudents.put(existingMentor, Integer.valueOf(existingMentor.numberOfStudents())); } Integer currentCount = mentorToNewStudents.get(existingMentor); int nextCount = currentCount - 1; if (nextCount < 3) { return wasSet; } mentorToNewStudents.put(existingMentor, Integer.valueOf(nextCount)); } checkNewStudents.add(aStudent); } students.removeAll(checkNewStudents); for (Student orphan : students) { setMentor(orphan, null); } students.clear(); for (Student aStudent : newStudents) { if (aStudent.getMentor() != null) { aStudent.getMentor().students.remove(aStudent); } setMentor(aStudent, this); students.add(aStudent); } wasSet = true; return wasSet; } /* Code from template association_GetPrivate */ private void setMentor(Student aStudent, Mentor aMentor) { try { java.lang.reflect.Field mentorField = aStudent.getClass().getDeclaredField("mentor"); mentorField.setAccessible(true); mentorField.set(aStudent, aMentor); } catch (Exception e) { throw new RuntimeException("Issue internally setting aMentor to aStudent", e); } } /* Code from template association_AddIndexControlFunctions */ public boolean addStudentAt(Student aStudent, int index) { boolean wasAdded = false; if(addStudent(aStudent)) { if(index < 0 ) { index = 0; } if(index > numberOfStudents()) { index = numberOfStudents() - 1; } students.remove(aStudent); students.add(index, aStudent); wasAdded = true; } return wasAdded; } public boolean addOrMoveStudentAt(Student aStudent, int index) { boolean wasAdded = false; if(students.contains(aStudent)) { if(index < 0 ) { index = 0; } if(index > numberOfStudents()) { index = numberOfStudents() - 1; } students.remove(aStudent); students.add(index, aStudent); wasAdded = true; } else { wasAdded = addStudentAt(aStudent, index); } return wasAdded; } public void delete() { for(Student aStudent : students) { setMentor(aStudent,null); } students.clear(); } public String toString() { return super.toString() + "["+ "name" + ":" + getName()+ "]"; } } /*PLEASE DO NOT EDIT THIS CODE*/ /*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/ package example; // line 9 "../OptionalOneToMNTest.ump" public class Student { //------------------------ // MEMBER VARIABLES //------------------------ //Student Attributes private String number; //Student Associations private Mentor mentor; //------------------------ // CONSTRUCTOR //------------------------ public Student(String aNumber) { number = aNumber; } //------------------------ // 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; } public boolean hasMentor() { boolean has = mentor != null; return has; } public void delete() { if (mentor != null) { if (mentor.numberOfStudents() <= 3) { mentor.delete(); } else { Mentor placeholderMentor = mentor; this.mentor = null; placeholderMentor.removeStudent(this); } } } public String toString() { return super.toString() + "["+ "number" + ":" + getNumber()+ "]" + System.getProperties().getProperty("line.separator") + " " + "mentor = "+(getMentor()!=null?Integer.toHexString(System.identityHashCode(getMentor())):"null"); } }