title: Cannot access first() element from an empty list when using Grails SortedSet
body: <p>I am working on creating some grails domain objects dynamically and then adding them a SortedSet declared in another grails domain object. I have created a Project class, filled in its values, and checked to make sure it is valid. It is valid, so I want to add this Project to an Employee.</p>
  
  <p>My code essentially goes like this</p>
  
  <pre><code>Employee employee = Employee.get(session.empid)
  ...
  //populate some Project objects
  ...
  //add projects to employee
  employee.addToProjects(project)
  </code></pre>
  
  <p>What could be going wrong here? If I do a project.validate(), and then check for errors, the only one says that project has no valid employee associated with it - but that should go away once I do the employee.addToProjects. Employee hasMany Project objects, and it is declared like so:</p>
  
  <pre><code>class Employee implements Comparable
  {
      static hasMany = [projects:Project]
  
      static constraints = 
      {
      }
  
      static mapping = {
          projects cascade:"all,delete-orphan", lazy:false
      }
  
      SortedSet&lt;Project&gt; projects = new TreeSet&lt;Project&gt;();
  }
  
  
  public class Project implements Comparable
  {  
      static belongsTo = [employee:Employee]
  
      static hasMany = [roles:Role]
  
      static mapping = {
            roles lazy:false, cascade:"all,delete-orphan"
      }
  
      @XmlElement
      List&lt;Role&gt; roles = new ArrayList&lt;Role&gt;();
  
  
      /*
       * return sorted list.  overwriting default getter was causing error upon saving multiple roles.
       *
       */
      def List getSortedRoles(){
          Collections.sort(roles, new RoleComparator());
          return roles;
      }
  
  
      String toString()
      {
          return name
      }
  
  
      // compare by latest date of roles, then by name + id
      //if this is too intrusive, implement comparator with this logic and sort on rendering page
         int compareTo(obj) {
             if(obj == null){
                 return 1;
             }
  
             def myMaxRole = findMaxRole(roles);
             def rhsMaxRole = findMaxRole(obj.roles);
  
             def rcomparator = new RoleComparator();
  
             System.out.println(myMaxRole.title + " " + rhsMaxRole.title + " " + rcomparator.compare(myMaxRole, rhsMaxRole));
             return rcomparator.compare(myMaxRole, rhsMaxRole);
         }
  
      def List getExpandableRoleList()
      {
          return LazyList.decorate(roles, FactoryUtils.instantiateFactory(Role.class));
      }
  
  
      def setExpandableRoleList(List l)
      {
          return roles = l;
      }
  
          def Role findMaxRole(roles){
              RoleComparator rc = new RoleComparator();
  
              Role maxRole = roles.first();
              for(role in roles){
                  if(rc.compare(maxRole, role) &gt; 0){
                      maxRole = role;
                  }
              }
  
              return maxRole;
          }
  
  public class Role implements Comparable
  {
  
      static belongsTo = [project:Project]
      static hasMany = [roleSkills:RoleSkill,roleTools:RoleTool]
  
      static mapping = {
          duties type:"text"
          roleSkills cascade:"all,delete-orphan", lazy:false
          roleTools cascade:"all,delete-orphan", lazy:false
  
      }
  
      static contraints = {
          endDate(nullable: true)
      }
  
      boolean _deleted
      static transients = ['_deleted']
  
      @XmlElement
      String title = ""
      @XmlElement
      String duties = ""
      @XmlElement
      int levelOfEffort
      @XmlElement
      Date startDate = new Date()
      @XmlElement
      Date endDate = new Date()
      @XmlElement
      Date lastModified = new Date()
      @XmlElement
      LocationType locationType = new LocationType(type: "Unknown")
      @XmlElement
      String rank
      @XmlElement
      List&lt;RoleSkill&gt; roleSkills = new ArrayList&lt;RoleSkill&gt;()
      @XmlElement
      List&lt;RoleTool&gt; roleTools  = new ArrayList&lt;RoleTool&gt;()
  
      String toString()
      {   
          return title;
      }
  
      int compareTo(obj) {
  
          return title.compareTo(obj.title)
      }
  
      def skills() {
          return roleSkills.collect{it.skill}
      }
      def tools() {
          return roleTools.collect{it.tool}
      }
  }
  </code></pre>