01: package org.cougaar.planning.ldm.measure;
02:
03: /**
04: * Shorthand for a Measure/Duration, e.g. Volume/Duration (FlowRate).
05: *
06: * @author gvidaver@bbn.com
07: * Date: May 17, 2007
08: * Time: 3:27:19 PM
09: * To change this template use File | Settings | File Templates.
10: */
11: public class GenericRate<N extends Measure> extends
12: GenericDerivative<N, Duration> implements Rate {
13: public GenericRate(N numerator, Duration denom) {
14: super (numerator, denom);
15: }
16:
17: public GenericRate() {
18: super ();
19: }
20:
21: protected GenericDerivative<N, Duration> newInstance(N numerator,
22: Duration denominator) {
23: return new GenericRate<N>(numerator, denominator);
24: }
25:
26: public static void main(String[] args) {
27: test1();
28: }
29:
30: private static void test1() {
31: Volume tenGallons = Volume.newGallons(10);
32: Duration oneHour = Duration.newHours(1);
33: GenericRate<Volume> gallonsPerHour = new GenericRate<Volume>(
34: tenGallons, oneHour);
35:
36: System.out.println("generic rate is " + gallonsPerHour);
37:
38: Measure volume = gallonsPerHour.multiply(oneHour);
39: System.out.println("volume " + volume + " should be " + 10
40: + " gallons");
41: volume = gallonsPerHour.multiply(Duration.newHours(10));
42: System.out.println("volume " + volume + " should be " + 100
43: + " gallons");
44:
45: GenericRate<Volume> twentyGallonsPerHour = (GenericRate<Volume>) gallonsPerHour
46: .add(gallonsPerHour);
47: System.out.println("Rate : " + gallonsPerHour + " + "
48: + gallonsPerHour + " = " + twentyGallonsPerHour);
49:
50: GenericRate<Volume> zeroGallonsPerHour = (GenericRate<Volume>) gallonsPerHour
51: .subtract(gallonsPerHour);
52: System.out.println("Rate : " + gallonsPerHour + " - "
53: + gallonsPerHour + " = " + zeroGallonsPerHour);
54:
55: Volume hundredGallons = (Volume) tenGallons.scale(10);
56: System.out.println("Volume : " + tenGallons.getNativeValue()
57: + " or " + tenGallons.getGallons() + " * 10 " + " = "
58: + hundredGallons.getNativeValue() + " or "
59: + hundredGallons.getGallons());
60:
61: GenericRate<Volume> hundredGallonsPerHour = (GenericRate<Volume>) gallonsPerHour
62: .scale(10);
63:
64: System.out.println("" + gallonsPerHour + " * 10" + " = "
65: + hundredGallonsPerHour);
66:
67: String commonUnit = gallonsPerHour.getUnitName(gallonsPerHour
68: .getCommonUnit());
69:
70: System.out.println("" + gallonsPerHour + " common unit is "
71: + commonUnit);
72:
73: for (int i = 0; i < gallonsPerHour.getMaxUnit(); i++) {
74: System.out.println("rate " + gallonsPerHour.getValue(i)
75: + " in " + gallonsPerHour.getUnitName(i));
76: }
77: }
78: }
|