Skip to content

Instantly share code, notes, and snippets.

@asoftwareguy
Created March 12, 2012 15:32
Show Gist options
  • Save asoftwareguy/2022678 to your computer and use it in GitHub Desktop.
Save asoftwareguy/2022678 to your computer and use it in GitHub Desktop.
A Groovy temperature conversion with Categories
package com.asoftwareguy.temperature;
/**
* This is an "old" existing Java class representing temperature.
* It assumes the unit is Fahrenheit.
*
*/
public final class AncientTemperature {
// in degrees Fahrenheit
private int temp;
public AncientTemperature(int temp) {
this.temp = temp;
}
@Override
public String toString() {
return new StringBuilder().append("The current temperature is " + temp + " degrees F.").toString();
}
}
package com.asoftwareguy.temperature
class TemperatureConversion {
static AncientTemperature getFahrenheit(String fahrenheit) {
new AncientTemperature(fahrenheit as int)
}
static AncientTemperature getCelsius(String celsius) {
BigDecimal fahrenheit = (celsius.toInteger() * (9/5) + 32)
fahrenheit = fahrenheit.setScale(0, BigDecimal.ROUND_DOWN)
new AncientTemperature(fahrenheit.toString() as int)
}
static AncientTemperature getFahrenheit(Integer fareheit) {
new AncientTemperature(fareheit)
}
static AncientTemperature getCelsius(Integer celsius) {
BigDecimal fahrenheit = (celsius * (9/5) + 32)
fahrenheit = fahrenheit.setScale(0, BigDecimal.ROUND_DOWN)
new AncientTemperature(fahrenheit.toString() as int)
}
static AncientTemperature plus(AncientTemperature first, AncientTemperature second) {
int tempFirst = first.temp
int tempSecond = second.temp;
int newTemp = tempFirst + tempSecond
return new AncientTemperature(newTemp)
}
static AncientTemperature minus(AncientTemperature first, AncientTemperature second) {
int tempFirst = first.temp
int tempSecond = second.temp;
int newTemp = tempFirst - tempSecond
return new AncientTemperature(newTemp)
}
}
package com.asoftwareguy.temperature
use(TemperatureConversion) {
assert "100".fahrenheit.toString() == 'The current temperature is 100 degrees F.'
println "100".fahrenheit
assert "50".fahrenheit.toString() == 'The current temperature is 50 degrees F.'
println "50".fahrenheit
assert "32".fahrenheit.toString() == 'The current temperature is 32 degrees F.'
println "32".fahrenheit
assert "100".celsius.toString() == 'The current temperature is 212 degrees F.'
println "100".celsius
assert 100.fahrenheit.toString() == 'The current temperature is 100 degrees F.'
println 100.fahrenheit
assert 50.fahrenheit.toString() == 'The current temperature is 50 degrees F.'
println 50.fahrenheit
assert 32.fahrenheit.toString() == 'The current temperature is 32 degrees F.'
println 32.fahrenheit
assert 100.celsius.toString() == 'The current temperature is 212 degrees F.'
println 100.celsius
assert (100.fahrenheit + 50.fahrenheit).toString() == 'The current temperature is 150 degrees F.'
println 100.fahrenheit + 50.fahrenheit
assert (100.fahrenheit + 0.celsius).toString() == 'The current temperature is 132 degrees F.'
println 100.fahrenheit + 0.celsius
assert (100.fahrenheit - 0.celsius).toString() == 'The current temperature is 68 degrees F.'
println 100.fahrenheit - 0.celsius
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment