gpt4 book ai didi

java - 使用 Grails SortedSet 时无法访问空列表中的 first() 元素

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:31 25 4
gpt4 key购买 nike

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

我的代码基本上是这样的

Employee employee = Employee.get(session.empid)
...
//populate some Project objects
...
//add projects to employee
employee.addToProjects(project)

这里可能出了什么问题?如果我执行 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}
}
}

最佳答案

让 [].first() 从空列表中抛出“java.util.NoSuchElementException:无法访问 first() 元素”在我看来似乎“不合常规”。

我使用 groovy 的安全遵从运算符 (?) 来避免 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

.first() 是唯一抛出异常的常规列表方法,这似乎令人沮丧。还有其他人经历过吗?

应更改文档以阐明这一点。list[0] - 如果找不到元素则返回 null。不区分空列表和第一个元素恰好为 null 的情况。

.first() 只在有第一个元素时返回第一个元素,否则抛出NoSuchElementException

关于java - 使用 Grails SortedSet 时无法访问空列表中的 first() 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com