01: package org.ofbiz.rules.utensil;
02:
03: /**
04: * <p><b>Title:</b> Limiting Linear Calculator
05: * <p><b>Description:</b> None
06: * <p>Copyright (c) 1999 Steven J. Metsker.
07: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
08: *
09: * <p>Permission is hereby granted, free of charge, to any person obtaining a
10: * copy of this software and associated documentation files (the "Software"),
11: * to deal in the Software without restriction, including without limitation
12: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13: * and/or sell copies of the Software, and to permit persons to whom the
14: * Software is furnished to do so, subject to the following conditions:
15: *
16: * <p>The above copyright notice and this permission notice shall be included
17: * in all copies or substantial portions of the Software.
18: *
19: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
24: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
25: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26: *
27: * <br>
28: * <p>A LimitingLinearCalculator is a LinearCalculator where the
29: * data points given in the constructor limit the extrapoltation.
30: *
31: * The X value of a LimitingLinearCalculator will never be
32: * below the minimum of xFrom and xTo, and will never be above
33: * the maximum of these two.
34: *
35: * @author Steven J. Metsker
36: * @version 1.0
37: */
38: public class LimitingLinearCalculator extends LinearCalculator {
39:
40: /**
41: * Create a LimitingLinearCalculator from known points on two scales.
42: *
43: * @param double xFrom
44: * @param double xTo
45: * @param double yFrom
46: * @param double yTo
47: */
48: public LimitingLinearCalculator(double xFrom, double xTo,
49: double yFrom, double yTo) {
50: super (xFrom, xTo, yFrom, yTo);
51: }
52:
53: /**
54: * Return the value on the first scale, corresponding to the given
55: * value on the second scale. Limit the X value to be between xFrom
56: * and xTo.
57: *
58: * @return the value on the first scale, corresponding to the given
59: * value on the second scale
60: */
61: public double calculateXforGivenY(double y) {
62: if (y <= yTo && y <= yFrom) {
63: return yFrom <= yTo ? xFrom : xTo;
64: }
65: if (y >= yTo && y >= yFrom) {
66: return yFrom >= yTo ? xFrom : xTo;
67: }
68: return super .calculateXforGivenY(y);
69: }
70:
71: /**
72: * Return the value on the second scale, corresponding to the given
73: * value on the first scale. Limit the Y value to be between yFrom
74: * and yTo.
75: *
76: * @return the value on the second scale, corresponding to the given
77: * value on the first scale
78: */
79: public double calculateYforGivenX(double x) {
80: if (x <= xTo && x <= xFrom) {
81: return xFrom <= xTo ? yFrom : yTo;
82: }
83: if (x >= xTo && x >= xFrom) {
84: return xFrom >= xTo ? yFrom : yTo;
85: }
86: return super.calculateYforGivenX(x);
87: }
88: }
|