001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: MinOptMax.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.traits;
021:
022: /**
023: * This class holds the resolved (as mpoints) form of a LengthRange or
024: * Space type Property value.
025: * MinOptMax values are used during layout calculations. The instance
026: * variables are package visible.
027: */
028: public class MinOptMax implements java.io.Serializable, Cloneable {
029:
030: /** Publicly visible min(imum), opt(imum) and max(imum) values.*/
031: public int min;
032: public int opt;
033: public int max;
034:
035: /**
036: * New min/opt/max with zero values.
037: */
038: public MinOptMax() {
039: this (0);
040: }
041:
042: /**
043: * New min/opt/max with one fixed value.
044: *
045: * @param val the value for min, opt and max
046: */
047: public MinOptMax(int val) {
048: this (val, val, val);
049: }
050:
051: /**
052: * New min/opt/max with the three values.
053: *
054: * @param min the minimum value
055: * @param opt the optimum value
056: * @param max the maximum value
057: */
058: public MinOptMax(int min, int opt, int max) {
059: // TODO: assert min<=opt<=max
060: this .min = min;
061: this .opt = opt;
062: this .max = max;
063: }
064:
065: /**
066: * Copy constructor.
067: *
068: * @param op the MinOptMax object to copy
069: */
070: public MinOptMax(MinOptMax op) {
071: this .min = op.min;
072: this .opt = op.opt;
073: this .max = op.max;
074: }
075:
076: // TODO: remove this.
077: /**
078: * @see java.lang.Object#clone()
079: */
080: public Object clone() {
081: try {
082: return super .clone();
083: } catch (CloneNotSupportedException ex) {
084: // SHOULD NEVER OCCUR - all members are primitive types!
085: return null;
086: }
087: }
088:
089: /**
090: * Subtracts one MinOptMax instance from another returning a new one.
091: * @param op1 first instance to subtract from
092: * @param op2 second instance
093: * @return MinOptMax new instance
094: */
095: public static MinOptMax subtract(MinOptMax op1, MinOptMax op2) {
096: return new MinOptMax(op1.min - op2.max, op1.opt - op2.opt,
097: op1.max - op2.min);
098: }
099:
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: * Subtracts another MinOptMax instance from this one.
158: * @param op the other instance
159: */
160: public void subtract(MinOptMax op) {
161: min -= op.max;
162: opt -= op.opt;
163: max -= op.min;
164: }
165:
166: /** @return true if this instance represents a zero-width length (min=opt=max=0) */
167: public boolean isNonZero() {
168: return (min != 0 || max != 0);
169: }
170:
171: /** @return true if this instance allows for shrinking or stretching */
172: public boolean isElastic() {
173: return (min != opt || opt != max);
174: }
175:
176: /** @see java.lang.Object#toString() */
177: public String toString() {
178: StringBuffer sb = new StringBuffer();
179: sb.append("MinOptMax[min=");
180: if (min != opt) {
181: sb.append(min).append("; ");
182: }
183: sb.append("opt=");
184: if (opt != max) {
185: sb.append(opt).append("; ");
186: }
187: sb.append("max=").append(max);
188: sb.append("]");
189: return sb.toString();
190: }
191: }
|