KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > traits > MinOptMax


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MinOptMax.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.traits;
21
22 /**
23  * This class holds the resolved (as mpoints) form of a LengthRange or
24  * Space type Property value.
25  * MinOptMax values are used during layout calculations. The instance
26  * variables are package visible.
27  */

28 public class MinOptMax implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
29
30     /** Publicly visible min(imum), opt(imum) and max(imum) values.*/
31     public int min;
32     public int opt;
33     public int max;
34
35     /**
36      * New min/opt/max with zero values.
37      */

38     public MinOptMax() {
39         this(0);
40     }
41
42     /**
43      * New min/opt/max with one fixed value.
44      *
45      * @param val the value for min, opt and max
46      */

47     public MinOptMax(int val) {
48         this(val, val, val);
49     }
50
51     /**
52      * New min/opt/max with the three values.
53      *
54      * @param min the minimum value
55      * @param opt the optimum value
56      * @param max the maximum value
57      */

58     public MinOptMax(int min, int opt, int max) {
59         // TODO: assert min<=opt<=max
60
this.min = min;
61         this.opt = opt;
62         this.max = max;
63     }
64
65     /**
66      * Copy constructor.
67      *
68      * @param op the MinOptMax object to copy
69      */

70     public MinOptMax(MinOptMax op) {
71         this.min = op.min;
72         this.opt = op.opt;
73         this.max = op.max;
74     }
75
76     // TODO: remove this.
77
/**
78      * @see java.lang.Object#clone()
79      */

80     public Object JavaDoc clone() {
81         try {
82             return super.clone();
83         } catch (CloneNotSupportedException JavaDoc ex) {
84             // SHOULD NEVER OCCUR - all members are primitive types!
85
return null;
86         }
87     }
88
89     /**
90      * Subtracts one MinOptMax instance from another returning a new one.
91      * @param op1 first instance to subtract from
92      * @param op2 second instance
93      * @return MinOptMax new instance
94      */

95     public static MinOptMax subtract(MinOptMax op1, MinOptMax op2) {
96         return new MinOptMax(op1.min - op2.max, op1.opt - op2.opt,
97                              op1.max - op2.min);
98     }
99
100     /**
101      * Adds one MinOptMax instance to another returning a new one.
102      * @param op1 first instance
103      * @param op2 second instance
104      * @return MinOptMax new instance
105      */

106     public static MinOptMax add(MinOptMax op1, MinOptMax op2) {
107         return new MinOptMax(op1.min + op2.min, op1.opt + op2.opt,
108                              op1.max + op2.max);
109     }
110
111     /**
112      * Multiplies a MinOptMax instance with a factor returning a new instance.
113      * @param op1 MinOptMax instance
114      * @param mult multiplier
115      * @return MinOptMax new instance
116      */

117     public static MinOptMax multiply(MinOptMax op1, double mult) {
118         // TODO: assert mult>0
119
return new MinOptMax((int)(op1.min * mult),
120                              (int)(op1.opt * mult), (int)(op1.max * mult));
121     }
122
123     /**
124      * Adds another MinOptMax instance to this one.
125      * @param op the other instance
126      */

127     public void add(MinOptMax op) {
128         min += op.min;
129         opt += op.opt;
130         max += op.max;
131     }
132
133     /**
134      * Adds min, opt and max to their counterpart components.
135      * @param min the value to add to the minimum value
136      * @param opt the value to add to the optimum value
137      * @param max the value to add to the maximum value
138      */

139     public void add(int min, int opt, int max) {
140         this.min += min;
141         this.opt += opt;
142         this.max += max;
143         // TODO: assert min<=opt<=max
144
}
145
146     /**
147      * Adds a length to all components.
148      * @param len the length to add
149      */

150     public void add(int len) {
151         this.min += len;
152         this.opt += len;
153         this.max += len;
154     }
155
156
157     /**
158      * Subtracts another MinOptMax instance from this one.
159      * @param op the other instance
160      */

161     public void subtract(MinOptMax op) {
162         min -= op.max;
163         opt -= op.opt;
164         max -= op.min;
165     }
166     
167     /** @return true if this instance represents a zero-width length (min=opt=max=0) */
168     public boolean isNonZero() {
169         return (min != 0 || max != 0);
170     }
171
172     /** @return true if this instance allows for shrinking or stretching */
173     public boolean isElastic() {
174         return (min != opt || opt != max);
175     }
176     
177     /** @see java.lang.Object#toString() */
178     public String JavaDoc toString() {
179         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
180         sb.append("MinOptMax[min=");
181         if (min != opt) {
182             sb.append(min).append("; ");
183         }
184         sb.append("opt=");
185         if (opt != max) {
186             sb.append(opt).append("; ");
187         }
188         sb.append("max=").append(max);
189         sb.append("]");
190         return sb.toString();
191     }
192 }
193
194
Popular Tags