117 lines
3.4 KiB
Markdown
117 lines
3.4 KiB
Markdown
---
|
|
title: Groovy - Using Enums
|
|
tags:
|
|
- IT/Development/Groovy
|
|
---
|
|
|
|
Some examples (inspired by the Java Enum Tutorial):
|
|
|
|
```groovy
|
|
enum Day {
|
|
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
|
THURSDAY, FRIDAY, SATURDAY
|
|
}
|
|
|
|
def tellItLikeItIs(Day day) {
|
|
switch (day) {
|
|
case Day.MONDAY:
|
|
println "Mondays are bad."
|
|
break
|
|
case Day.FRIDAY:
|
|
println "Fridays are better."
|
|
break
|
|
case Day.SATURDAY:
|
|
case Day.SUNDAY:
|
|
println "Weekends are best."
|
|
break
|
|
default:
|
|
println "Midweek days are so-so."
|
|
break
|
|
}
|
|
}
|
|
|
|
tellItLikeItIs(Day.MONDAY) // => Mondays are bad.
|
|
tellItLikeItIs(Day.WEDNESDAY) // => Midweek days are so-so.
|
|
tellItLikeItIs(Day.FRIDAY) // => Fridays are better.
|
|
tellItLikeItIs(Day.SATURDAY) // => Weekends are best.
|
|
```
|
|
|
|
Or with a bit of refactoring, you could write the switch like this:
|
|
|
|
```groovy
|
|
def today = Day.SATURDAY
|
|
switch (today) {
|
|
// Saturday or Sunday
|
|
case [ Day.SATURDAY, Day.SUNDAY ]:
|
|
println "Weekends are cool"
|
|
break
|
|
// a day between Monday and Friday
|
|
case Day.MONDAY..Day.FRIDAY:
|
|
println "Boring work day"
|
|
break
|
|
default:
|
|
println "Are you sure this is a valid day?"
|
|
}
|
|
```
|
|
|
|
Here is a coin example:
|
|
|
|
```groovy
|
|
enum Coin {
|
|
penny(1), nickel(5), dime(10), quarter(25)
|
|
Coin(int value) { this.value = value }
|
|
private final int value
|
|
public int value() { return value }
|
|
}
|
|
|
|
assert Coin.values().size() == 4
|
|
|
|
def pocketMoney = 2 * Coin.quarter.value() + 5 * Coin.dime.value()
|
|
assert pocketMoney == 100
|
|
|
|
// another way to do abovedef coins = [ Coin.quarter ] * 2 + [ Coin.dime ] * 5
|
|
println coins // => [ quarter, quarter, dime, dime, dime, dime, dime ]
|
|
println coins.sum{ it.value() } // => 100
|
|
```
|
|
|
|
Here is a planet example:
|
|
|
|
```groovy
|
|
enum Planet {
|
|
MERCURY(3.303e+23, 2.4397e6),
|
|
VENUS(4.869e+24, 6.0518e6),
|
|
EARTH(5.976e+24, 6.37814e6),
|
|
MARS(6.421e+23, 3.3972e6),
|
|
JUPITER(1.9e+27, 7.1492e7),
|
|
SATURN(5.688e+26, 6.0268e7),
|
|
URANUS(8.686e+25, 2.5559e7),
|
|
NEPTUNE(1.024e+26, 2.4746e7)
|
|
private final double mass // in kilograms
|
|
private final double radius // in metres
|
|
Planet(double mass, double radius) {
|
|
this.mass = mass
|
|
this.radius = radius
|
|
}
|
|
private double mass() { return mass }
|
|
private double radius() { return radius }
|
|
// universal gravitational constant (m3 kg-1 s-2)
|
|
public static final double G = 6.67300E-11
|
|
double surfaceGravity() { return G * mass / (radius * radius) }
|
|
double surfaceWeight(double otherMass) { return otherMass * surfaceGravity() }
|
|
}
|
|
|
|
double earthWeight = 75.0 // kgdouble mass = earthWeight/Planet.EARTH.surfaceGravity()
|
|
for (p in Planet.values()) {
|
|
printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass))
|
|
}
|
|
|
|
// =>// Your weight on MERCURY is 28.331821// Your weight on VENUS is 67.874932// Your weight on EARTH is 75.000000// Your weight on MARS is 28.405289// Your weight on JUPITER is 189.791814// Your weight on SATURN is 79.951165// Your weight on URANUS is 67.884540// Your weight on NEPTUNE is 85.374605
|
|
```
|
|
|
|
Note: there are currently issues with using Groovy enums in conjunction with GroovyShell. Best bet would be to check Jira if you are having problems, e.g. http://jira.codehaus.org/browse/GROOVY-2135
|
|
|
|
|
|
Evernote hilft dir, nichts zu vergessen und alles mühelos zu ordnen und zu organisieren. Evernote herunterladen.
|
|
|
|
|