list of dots

User Manual    [Previous]   [Next]   

Sorted Associations

It is possible to arrange for associations to maintain themselves in sorted order according to the value of an attribute. Create a derived attribute in order to define a complex sort order or to sort on the value of an attribute of an associated class (the first example below does this in one case).

To declare an association as sorted do the following: after specifying the multiplicity, and any role name, give the keyword 'sorted' followed by the name of an attribute (sort key) in curly brackets. Whenever an add method is called to add an item to the association, Umple ensures the association is maintained in sorted order. .

Note that Umple associations are always 'ordered' by default; i.e. the user controls the position of items. Declaring an association as sorted makes this automatic. The presence of 'sorted' adds a 'sort' function to the API to allow for resorting (if the attributes on which sorting is based are changed, for example) and removes the API for manually ordering the association using addXAt().

At the time of writing, there remain several issues: For now, you should avoid having two sorted associations in the same class.

Errors are generated if the attribute specified as the sort key attribute does not exist in the class, or the sort key attribute is not of a recognized type.

Java serialization of sorted associations is possible without the need to modify the Java code generated by Umple. The two ends of a sorted association implement the Serializable interface by default. Association items can be serialized and deserialized. After deserialization, more items can be added to the association and the association is maintained in sorted order as demonstrated by the second example.

Example

// This example demonstrates two cases of sorted
// associations. The main program adds items out
// of order. But when they are printed the output
// will be sorted
class Academy {
  1 -- * Course;
  1 -- * Student registrants sorted {id};
}

class Student {
  Integer id;
  name;
}

class Course {
  code;
}

class Registration {
  * -- 1 Student;

  // In each course, sort registrations by name
  * sorted {name} -- 1 Course;

  // Derived delegated attribute used for sorting
  // printing
  name = {getStudent().getName()}

  // Derived delegated attribute used for sorting
  // printing
  code = {getCourse().getCode()}

  
  public String toString() {
    return "Registration: " + getName()
      + ":" + getCode();
  }
}

// Mixin with main program and toString method
class Academy {
  public static void main(String [ ] args) {
    Academy ac = new Academy();
    Student j = ac.addRegistrant(12,"Jim");
    Student a = ac.addRegistrant(4,"Ali");
    Student m = ac.addRegistrant(8,"Mary");
    Student f = ac.addRegistrant(3,"Francois");
    Course c = ac.addCourse("CS191");
    Course c2 = ac.addCourse("AN234");    
    j.addRegistration(c);
    a.addRegistration(c);
    m.addRegistration(c);
    f.addRegistration(c);
    m.addRegistration(c2);
    f.addRegistration(c2);
    System.out.println(ac);
  }
  public String toString() {
    String result="Students:\n";
    for (Student s: getRegistrants()) {
      result +=s + "\n";
    }
    result +="Courses:\n";
    for (Course c: getCourses()) {
      result +=c + "\n";
    }

    return result;
  }
}

class Student {
  public String toString() {
    String result="Student=" + id + "["
      + name + "\n";
    for (Registration r: getRegistrations()) {
      result +="  In: " + r + "\n";
    }
    return result;
  }
}

class Course {
  public String toString() {
    String result ="Course=" + code + "\n";
    for (Registration r: getRegistrations()) {
      result +="  Has: " + r + "\n";
    }    
    return result;
  }
}
      

Load the above code into UmpleOnline

 

Example of sorted associations serialisation in Java

// This example demonstrates serialisation of
// sorted associations in Java. The main program
// adds items to an object out of order, serialize the
// object, deserialize it and then add more items
// to it. When they are printed after deserialisation,
// all items will appear in the right order.

class Academy {
  1 -- * Student registrants sorted {id};
}

class Student {
  Integer id;
  name;
}

// Mixin with main program, toString method
// and serialization test

class Academy {
  depend java.nio.file.*;
  depend java.io.*;
  
  public static void main(String [ ] args) {
    String filename = "SortedSerializableAssociation.ser";
    Academy ac = new Academy();
    Student j = ac.addRegistrant(12,"Jim");
    Student a = ac.addRegistrant(4,"Ali");

    System.out.println("Before Serialization");
    System.out.println(ac);
   
    //serialization
    try
    { 
      FileOutputStream file = new FileOutputStream(filename); 
      ObjectOutputStream out = new ObjectOutputStream(file); 
      out.writeObject(ac);       
      out.close(); 
      file.close();       
    } 
    catch(Exception ex) 
    { 
      System.out.println(ex.getMessage());
    } 

    Academy ac2 = null; 

    // Deserialization 
    try
    { 
      FileInputStream file = new FileInputStream(filename); 
      ObjectInputStream in = new ObjectInputStream(file);       
      ac2 = (Academy)in.readObject();       
      in.close(); 
      file.close();
    } 
    catch(Exception ex) 
    { 
      System.out.println(ex.getMessage());
    } 

    // Adding new elements and comparing 
    try
    { 
	Student m = ac2.addRegistrant(8,"Mary");
	Student f = ac2.addRegistrant(3,"Francois");
    }    
    catch(Exception ex) 
    { 
      System.out.println(ex.getMessage());
    } 
    
    System.out.println("After Serialization");
    System.out.println(ac2);
    
  }
  
  public String toString() {
    String result="Students:\n";
    for (Student s: getRegistrants()) {
      result +=s + "\n";
    }
    return result;
  }
}

class Student {
  public String toString() {
    String result="Student=" + id + "["
      + name +"]"+ "\n";
    return result;
  }
}

      

Load the above code into UmpleOnline

 

Syntax


inlineAssociationEnd : [[multiplicity]] [~roleName]? [[isSorted]]?

isSorted- : sorted { [priority] }