我有一堆Grails域类,我希望能够将其视为Number
s,Integer
s。在大多数情况下,它们只是具有一些额外属性的数值,用作元数据。这是一个例子:
class Score {
String meaning
Integer value
static hasMany = [responses:Response]
static constraints = {
meaning blank: false, maxSize: 100, unique: true
value min: 1, unique: true // Assume we're using a base-1 ranking system, where 1 is the lowest
}
}
我尝试将@Delegate
添加到value
字段,但似乎没有任何影响:我仍然无法7 + myScore
。我得到的只是缺少方法异常,因为Integer没有匹配plus(Score)
的签名。
这样做的正确方法是什么,因为@Delegate
似乎不起作用?
注意:我还需要将我的域类转换为带有元数据的各种Collection
,但我希望它将是相同的解决方案。
答案 0 :(得分:1)
我想象每个人都已经离开了,现在这个问题已经超过一年半了,但是,我很惊讶没有人提供Groovy类别作为解决方案。事实上,在我看来,类别是解决这个问题的最简单的自然解决方案。在不改变原始域类的“Grailsiness”的情况下,您仍然可以相对容易地灌输所需的数字行为。
首先定义类别类:
class ScoreCategory {
static asType(Score s, Class newClass) {
switch (newClass) {
case Integer.class:
case Integer.TYPE:
case Number.class: return s?.value ?: 0
default: throw new GroovyCastException("Cannot cast to ${newClass}")
}
}
static boolean equals(Score me, def you) {
you instanceof Score && me as int == you as int
}
static Score plus(Score a, b) { plusImpl(a, b) }
static Score plus(Score a, Number b) { plusImpl(a, b) }
static Score plus(Number a, Score b) { plusImpl(a, b) }
private static plusImpl(a, b) { new Score(value: (a as int) + (b as int)) }
static Score minus(Score a, b) { minusImpl(a, b) }
static Score minus(Score a, Number b) { minusImpl(a, b) }
static Score minus(Number a, Score b) { minusImpl(a, b) }
private static minusImpl(a, b) { a + -b }
static Score multiply(Score a, Number b) { multImpl(a,b) }
static Score multiply(Number a, Score b) { multImpl(a,b) }
private static multImpl(a,b) { new Score(value: (a as int) * (b as int)) }
static Integer div(Score a, b) { (a as int).intdiv(b as int) }
static Score div(Score a, Number b) { new Score(value:(a as int).intdiv(b as int)) }
static Score negative(Score a) { new Score(value: -(a as int)) }
static Score abs(Score a) { new Score(value: (a as int).abs())}
}
然后,在应用程序的某个适当的全局位置声明mixins:
Number.metaClass.mixin ScoreCategory
Score.metaClass.mixin ScoreCategory
毕竟,如下所示,Score
对象现在应该像数字一样:
def (w, x, y, z) = [54, 78, 21, 32]
def (s1, s2) = [new Score(value:w), new Score(value:x)]
assert (s1 + s2) == new Score(value: w + x)
assert (s1 + y) == new Score(value: w + y)
assert (z + s2) == new Score(value: z + x)
assert (s1 - s2) == new Score(value: w - x)
assert (s1 - y) == new Score(value: w - y)
assert (z - s2) == new Score(value: z - x)
assert (s1 * y) == new Score(value: w * y)
assert (z * s2) == new Score(value: z * x)
assert (s2 / s1) == x.intdiv(w)
assert (s1 / y) == new Score(value: w / y)
答案 1 :(得分:0)
在plus
元类上添加Number
运算符的重载:
Number.metaClass.plus = { Score s -> delegate + s.value }
在您的应用中的BootStrap.groovy或插件中的doWithDynamicMethods
中添加此内容。
修改强>
正如评论中所指出的,如果Score
位于操作的左侧,则不起作用。向plus
类添加Score
方法来处理:
Number plus(Number n) { value + n }
答案 2 :(得分:0)
您的回答可以是operator overloding。在这里你重载了plus方法来操作整数和双精度:
class Score {
...
int plus(int otherValue){
return otherValue + value
}
double plus(double otherValue){
return (value as double) + otherValue
}
// And you can avoid my pitfall overriding asType method
Object asType(Class clazz) {
if (clazz == Integer) {
return value
}
else if(class == Double){
return value as Double
}
else {
super.asType(clazz)
}
}
}
assert new Score(value: 4) + 15 == 19
assert new Score(value: 4) + 15.32 == 19.32
assert 15.32 + (new Score(value: 4) as Double) == 19.32
assert 15 + (new Score(value: 4) as Integer) == 19
答案 3 :(得分:-1)
您也可以扩展号码。
class Score extends Number {
Integer value
public int intValue() { return value }
public double doubleValue() { return value }
public long longValue() { return value }
public float floatValue() { return value }
}
Score sc = new Score( value: 5 );
println 10 + sc