使用Grails SortedSet时,无法从空列表中访问first()元素

Der*_*rek 3 java grails

我正在努力动态创建一些grails域对象,然后添加一个在另一个grails域对象中声明的SortedSet.我创建了一个Project类,填充了它的值,并检查以确保它是有效的.它是有效的,所以我想将此项目添加到员工.

我的代码基本上是这样的

Employee employee = Employee.get(session.empid)
...
//populate some Project objects
...
//add projects to employee
employee.addToProjects(project)
Run Code Online (Sandbox Code Playgroud)

这可能会出错?如果我执行project.validate(),然后检查错误,那么唯一一个说项目没有与之关联的有效员工 - 但是当我执行employee.addToProjects时,这应该消失.Employee有很多Project对象,它的声明如下:

class Employee implements Comparable
{
    static hasMany = [projects:Project]

    static constraints = 
    {
    }

    static mapping = {
        projects cascade:"all,delete-orphan", lazy:false
    }

    SortedSet<Project> projects = new TreeSet<Project>();
}


public class Project implements Comparable
{  
    static belongsTo = [employee:Employee]

    static hasMany = [roles:Role]

    static mapping = {
          roles lazy:false, cascade:"all,delete-orphan"
    }

    @XmlElement
    List<Role> roles = new ArrayList<Role>();


    /*
     * 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) > 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<RoleSkill> roleSkills = new ArrayList<RoleSkill>()
    @XmlElement
    List<RoleTool> roleTools  = new ArrayList<RoleTool>()

    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}
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 13

让[] .first()抛出一个'java.util.NoSuchElementException:在我看来,无法从空List中访问first()元素似乎是'un-groovy'.

我使用groovy的safe-deference运算符(?)来避免NPE/NoSuchElementException

def list=[]
println list[0] //returns null
println list.first()    //NoSuchElementException
println list?.first()  //NoSuchElementException.  Would prefer it to return null.
def list2=[null,3,6]
println list2.first()  // returns null
println list2[0]       //returns null
Run Code Online (Sandbox Code Playgroud)

令人沮丧的是.first()是唯一一个抛出异常的groovy list方法.还有其他人经历过这个吗?

应该更改文档以澄清这一点.list [0] - 如果找不到元素,则返回null.当第一个元素恰好为空时,不区分空列表和大小写.

.first()仅在存在第一个元素时才返回第一个元素,否则抛出NoSuchElementException

  • 阿门!为什么抛出列表为空的绝对非异常事件的异常. (2认同)