Dan
Dan

Reputation: 11183

Overriding comparison operators in Groovy

I would like to override >= operator in Groovy, have found this page, but I am still not sure how to do it. I have a class Banknote with properties serial and amount and I wish to implement comparison bases on the amount property.

Upvotes: 3

Views: 2248

Answers (1)

Dave Newton
Dave Newton

Reputation: 160231

You don't override the >= operator, you implement compareTo:

class Foo implements Comparable {
  int val
  int compareTo(Object o) { return val <=> ((Foo) o).val }
}

f1 = new Foo(val: 5)
f2 = new Foo(val: 10)
println f1 <= f2
=> true

Upvotes: 7

Related Questions