This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix two traits issues, #46 and call private methods from trait methods
- Loading branch information
1 parent
13fb4ba
commit 416eaa0
Showing
5 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package traits | ||
|
||
/** | ||
* Created by jorgefrancoleza on 5/6/15. | ||
*/ | ||
trait Equal { | ||
|
||
int number = 5 | ||
|
||
boolean equalProperties() { | ||
def props = this.propertyList() | ||
if (props.size() == 2) { | ||
equalsTwo(*props) | ||
} else if (props.size() == 3) { | ||
this.equalsThree(*props) | ||
} else { | ||
throw new Exception() | ||
} | ||
} | ||
|
||
boolean allNumberFive() { | ||
propertyList().every { | ||
it == this.number && it == this.getNumber() && it == number | ||
} | ||
} | ||
|
||
private boolean equalsTwo(a, b) { | ||
a == b && b == a | ||
} | ||
|
||
private boolean equalsThree(a, b, c) { | ||
this.equalsTwo(a, b) && this.equalsTwo(b, c) && equalsTwo(c, a) | ||
} | ||
} | ||
|
||
class MyTwoNumbers implements Equal { | ||
def a = 5 | ||
def b = 5 | ||
def propertyList() { | ||
[a, b] | ||
} | ||
} | ||
|
||
class MyThreeNumbers implements Equal { | ||
def a = 6 | ||
def b = 6 | ||
def c = 6 | ||
|
||
def propertyList() { | ||
[a, b, c] | ||
} | ||
} | ||
|
||
def myTwoNumbers = new MyTwoNumbers() | ||
assert myTwoNumbers.equalProperties() | ||
assert myTwoNumbers.allNumberFive() | ||
myTwoNumbers.a = 7 | ||
assert !myTwoNumbers.equalProperties() | ||
assert !myTwoNumbers.allNumberFive() | ||
|
||
def myThreeNumbers = new MyThreeNumbers() | ||
assert myThreeNumbers.equalProperties() | ||
myThreeNumbers.c = 8 | ||
assert !myThreeNumbers.equalProperties() |