01: package net.sf.mockcreator.expectable;
02:
03: /**
04: * Expects float with a given deviation
05: *
06: * TODO: usage
07: */
08: public class ExpectFloat implements ExpectableParameter {
09: private Double from;
10: private Double till;
11:
12: /**
13: * Construct expectable by value and allowed error.
14: */
15: public ExpectFloat(float exp, float error) {
16: this ((double) exp, (double) error);
17: }
18:
19: /**
20: * Construct expectable by value and allowed error.
21: */
22: public ExpectFloat(double exp, double deviation) {
23: if (deviation <= 0.0) {
24: throw new IllegalArgumentException(
25: "deviation is zero or negative");
26: }
27:
28: from = new Double(exp - deviation);
29: till = new Double(exp + deviation);
30: }
31:
32: /**
33: * Construct expectable by float range.
34: */
35: public ExpectFloat(Float from, Float till) {
36: this ((from == null) ? null : new Double(from.doubleValue()),
37: (till == null) ? null : new Double(till.doubleValue()));
38: }
39:
40: /**
41: * Construct expectable by double range.
42: */
43: public ExpectFloat(Double from, Double till) {
44: if ((from == null) && (till == null)) {
45: throw new IllegalArgumentException(
46: "both values are null; use Ignore instead");
47: }
48:
49: if ((from != null) && (till != null)
50: && (from.doubleValue() > till.doubleValue())) {
51: throw new IllegalArgumentException("from is after till");
52: }
53:
54: this .from = from;
55: this .till = till;
56: }
57:
58: public boolean isExpected(Object actual) {
59: if (actual == null) {
60: return false;
61: }
62:
63: if (!(actual instanceof Float) && !(actual instanceof Double)) {
64: return false;
65: }
66:
67: Double act = (actual instanceof Double) ? (Double) actual
68: : new Double(((Float) actual).doubleValue());
69:
70: return ((from == null) ? true : (from.doubleValue() <= act
71: .doubleValue()))
72: && ((till == null) ? true : (till.doubleValue() >= act
73: .doubleValue()));
74: }
75:
76: public String toString() {
77: return "ExpectFloat(" + from + ";" + till + ")";
78: }
79: }
|