Skip to content

Instantly share code, notes, and snippets.

@cobratbq
Last active December 21, 2015 14:26
Show Gist options
  • Save cobratbq/9a5388f1a1f6fa54601a to your computer and use it in GitHub Desktop.
Save cobratbq/9a5388f1a1f6fa54601a to your computer and use it in GitHub Desktop.
Example 3: Third component using expectations
package thirdcomponent;
import java.math.BigDecimal;
import java.math.MathContext;
public class ExpectationsExample {
public static void main(String[] args) {
final BigDecimal one = BigDecimal.ONE;
final BigDecimal three = BigDecimal.valueOf(3L);
final ExpectationsExample comp = new ExpectationsExample(
MathContext.DECIMAL32);
System.out.println(comp.calculate(one, three));
// prints 0.3333333
}
// provided, external choice
// final as we don't allow ad hoc changes
// value is dictated by environment, not managed by instance
private final MathContext c;
public ExpectationsExample(MathContext mc) {
this.c = mc;
}
public BigDecimal calculate(BigDecimal x, BigDecimal y) {
return x.divide(y, c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment