選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MinOptMax.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /* $Id$ */
  18. package org.apache.fop.traits;
  19. import java.io.Serializable;
  20. /**
  21. * This class holds the resolved (as mpoints) form of a
  22. * {@link org.apache.fop.fo.properties.LengthRangeProperty} or
  23. * {@link org.apache.fop.fo.properties.SpaceProperty} type property value.
  24. * <p/>
  25. * Instances of this class are immutable. All arithmetic methods like {@link #plus(MinOptMax) plus},
  26. * {@link #minus(MinOptMax) minus} or {@link #mult(int) mult} return a different instance. So it is
  27. * possible to pass around instances without copying.
  28. * <p/>
  29. * <code>MinOptMax</code> values are used during layout calculations.
  30. */
  31. public final class MinOptMax implements Serializable {
  32. private static final long serialVersionUID = -4791524475122206142L;
  33. /**
  34. * The zero <code>MinOptMax</code> instance with <code>min == opt == max == 0</code>.
  35. */
  36. public static final MinOptMax ZERO = getInstance(0);
  37. private final int min;
  38. private final int opt;
  39. private final int max;
  40. /**
  41. * Returns an instance of <code>MinOptMax</code> with the given values.
  42. *
  43. * @param min the minimum value
  44. * @param opt the optimum value
  45. * @param max the maximum value
  46. * @return the corresponding instance
  47. * @throws IllegalArgumentException if <code>min > opt || max < opt</code>.
  48. */
  49. public static MinOptMax getInstance(int min, int opt, int max) throws IllegalArgumentException {
  50. if (min > opt) {
  51. throw new IllegalArgumentException("min (" + min + ") > opt (" + opt + ")");
  52. }
  53. if (max < opt) {
  54. throw new IllegalArgumentException("max (" + max + ") < opt (" + opt + ")");
  55. }
  56. return new MinOptMax(min, opt, max);
  57. }
  58. /**
  59. * Returns an instance of <code>MinOptMax</code> with one fixed value for all three
  60. * properties (min, opt, max).
  61. *
  62. * @param value the value for min, opt and max
  63. * @return the corresponding instance
  64. * @see #isStiff()
  65. */
  66. public static MinOptMax getInstance(int value) {
  67. return new MinOptMax(value, value, value);
  68. }
  69. // Private constructor without consistency checks
  70. private MinOptMax(int min, int opt, int max) {
  71. assert min <= opt && opt <= max;
  72. this.min = min;
  73. this.opt = opt;
  74. this.max = max;
  75. }
  76. /**
  77. * Returns the minimum value of this <code>MinOptMax</code>.
  78. *
  79. * @return the minimum value of this <code>MinOptMax</code>.
  80. */
  81. public int getMin() {
  82. return min;
  83. }
  84. /**
  85. * Returns the optimum value of this <code>MinOptMax</code>.
  86. *
  87. * @return the optimum value of this <code>MinOptMax</code>.
  88. */
  89. public int getOpt() {
  90. return opt;
  91. }
  92. /**
  93. * Returns the maximum value of this <code>MinOptMax</code>.
  94. *
  95. * @return the maximum value of this <code>MinOptMax</code>.
  96. */
  97. public int getMax() {
  98. return max;
  99. }
  100. /**
  101. * Returns the shrinkability of this <code>MinOptMax</code> which is the absolute difference
  102. * between <code>min</code> and <code>opt</code>.
  103. *
  104. * @return the shrinkability of this <code>MinOptMax</code> which is always non-negative.
  105. */
  106. public int getShrink() {
  107. return opt - min;
  108. }
  109. /**
  110. * Returns the stretchability of this <code>MinOptMax</code> which is the absolute difference
  111. * between <code>opt</code> and <code>max</code>.
  112. *
  113. * @return the stretchability of this <code>MinOptMax</code> which is always non-negative.
  114. */
  115. public int getStretch() {
  116. return max - opt;
  117. }
  118. /**
  119. * Returns the sum of this <code>MinOptMax</code> and the given <code>MinOptMax</code>.
  120. *
  121. * @param operand the second operand of the sum (the first is this instance itself),
  122. * @return the sum of this <code>MinOptMax</code> and the given <code>MinOptMax</code>.
  123. */
  124. public MinOptMax plus(MinOptMax operand) {
  125. return new MinOptMax(min + operand.min, opt + operand.opt, max + operand.max);
  126. }
  127. /**
  128. * Adds the given value to all three components of this instance and returns the result.
  129. *
  130. * @param value value to add to the min, opt, max components
  131. * @return the result of the addition
  132. */
  133. public MinOptMax plus(int value) {
  134. return new MinOptMax(min + value, opt + value, max + value);
  135. }
  136. /**
  137. * Returns the difference of this <code>MinOptMax</code> and the given
  138. * <code>MinOptMax</code>. This instance must be a compound of the operand and another
  139. * <code>MinOptMax</code>, that is, there must exist a <code>MinOptMax</code> <i>m</i>
  140. * such that <code>this.equals(m.plus(operand))</code>. In other words, the operand
  141. * must have less shrink and stretch than this instance.
  142. *
  143. * @param operand the value to be subtracted
  144. * @return the difference of this <code>MinOptMax</code> and the given
  145. * <code>MinOptMax</code>.
  146. * @throws ArithmeticException if this instance has strictly less shrink or stretch
  147. * than the operand
  148. */
  149. public MinOptMax minus(MinOptMax operand) throws ArithmeticException {
  150. checkCompatibility(getShrink(), operand.getShrink(), "shrink");
  151. checkCompatibility(getStretch(), operand.getStretch(), "stretch");
  152. return new MinOptMax(min - operand.min, opt - operand.opt, max - operand.max);
  153. }
  154. private void checkCompatibility(int thisElasticity, int operandElasticity, String msge) {
  155. if (thisElasticity < operandElasticity) {
  156. throw new ArithmeticException(
  157. "Cannot subtract a MinOptMax from another MinOptMax that has less " + msge
  158. + " (" + thisElasticity + " < " + operandElasticity + ")");
  159. }
  160. }
  161. /**
  162. * Subtracts the given value from all three components of this instance and returns the result.
  163. *
  164. * @param value value to subtract from the min, opt, max components
  165. * @return the result of the subtraction
  166. */
  167. public MinOptMax minus(int value) {
  168. return new MinOptMax(min - value, opt - value, max - value);
  169. }
  170. /**
  171. * Do not use, backwards compatibility only. Returns an instance with the
  172. * given value added to the minimal value.
  173. *
  174. * @param minOperand the minimal value to be added.
  175. * @return an instance with the given value added to the minimal value.
  176. * @throws IllegalArgumentException if
  177. * <code>min + minOperand > opt || max < opt</code>.
  178. */
  179. public MinOptMax plusMin(int minOperand) throws IllegalArgumentException {
  180. return getInstance(min + minOperand, opt, max);
  181. }
  182. /**
  183. * Do not use, backwards compatibility only. Returns an instance with the
  184. * given value subtracted to the minimal value.
  185. *
  186. * @param minOperand the minimal value to be subtracted.
  187. * @return an instance with the given value subtracted to the minimal value.
  188. * @throws IllegalArgumentException if
  189. * <code>min - minOperand > opt || max < opt</code>.
  190. */
  191. public MinOptMax minusMin(int minOperand) throws IllegalArgumentException {
  192. return getInstance(min - minOperand, opt, max);
  193. }
  194. /**
  195. * Do not use, backwards compatibility only. Returns an instance with the
  196. * given value added to the maximal value.
  197. *
  198. * @param maxOperand the maximal value to be added.
  199. * @return an instance with the given value added to the maximal value.
  200. * @throws IllegalArgumentException if
  201. * <code>min > opt || max < opt + maxOperand</code>.
  202. */
  203. public MinOptMax plusMax(int maxOperand) throws IllegalArgumentException {
  204. return getInstance(min, opt, max + maxOperand);
  205. }
  206. /**
  207. * Do not use, backwards compatibility only. Returns an instance with the
  208. * given value subtracted to the maximal value.
  209. *
  210. * @param maxOperand the maximal value to be subtracted.
  211. * @return an instance with the given value subtracted to the maximal value.
  212. * @throws IllegalArgumentException if
  213. * <code>min > opt || max < opt - maxOperand</code>.
  214. */
  215. public MinOptMax minusMax(int maxOperand) throws IllegalArgumentException {
  216. return getInstance(min, opt, max - maxOperand);
  217. }
  218. /**
  219. * Returns the product of this <code>MinOptMax</code> and the given factor.
  220. *
  221. * @param factor the factor
  222. * @return the product of this <code>MinOptMax</code> and the given factor
  223. * @throws IllegalArgumentException if the factor is negative
  224. */
  225. public MinOptMax mult(int factor) throws IllegalArgumentException {
  226. if (factor < 0) {
  227. throw new IllegalArgumentException("factor < 0; was: " + factor);
  228. } else if (factor == 1) {
  229. return this;
  230. } else {
  231. return getInstance(min * factor, opt * factor, max * factor);
  232. }
  233. }
  234. /**
  235. * Determines whether this <code>MinOptMax</code> represents a non-zero dimension, which means
  236. * that not all values (min, opt, max) are zero.
  237. *
  238. * @return <code>true</code> if this <code>MinOptMax</code> represents a non-zero dimension;
  239. * <code>false</code> otherwise.
  240. */
  241. public boolean isNonZero() {
  242. return min != 0 || max != 0;
  243. }
  244. /**
  245. * Determines whether this <code>MinOptMax</code> doesn't allow for shrinking or stretching,
  246. * which means that all values (min, opt, max) are the same.
  247. *
  248. * @return <code>true</code> if whether this <code>MinOptMax</code> doesn't allow for shrinking
  249. * or stretching; <code>false</code> otherwise.
  250. * @see #isElastic()
  251. */
  252. public boolean isStiff() {
  253. return min == max;
  254. }
  255. /**
  256. * Determines whether this <code>MinOptMax</code> allows for shrinking or stretching, which
  257. * means that at least one of the min or max values isn't equal to the opt value.
  258. *
  259. * @return <code>true</code> if this <code>MinOptMax</code> allows for shrinking or stretching;
  260. * <code>false</code> otherwise.
  261. * @see #isStiff()
  262. */
  263. public boolean isElastic() {
  264. return min != opt || opt != max;
  265. }
  266. /**
  267. * Extends the minimum length to the given length if necessary, and adjusts opt and max
  268. * accordingly.
  269. *
  270. * @param newMin the new minimum length
  271. * @return a <code>MinOptMax</code> instance with the minimum length extended
  272. */
  273. public MinOptMax extendMinimum(int newMin) {
  274. if (min < newMin) {
  275. int newOpt = Math.max(newMin, opt);
  276. int newMax = Math.max(newOpt, max);
  277. return getInstance(newMin, newOpt, newMax);
  278. } else {
  279. return this;
  280. }
  281. }
  282. /**
  283. * {@inheritDoc}
  284. */
  285. public boolean equals(Object obj) {
  286. if (this == obj) {
  287. return true;
  288. }
  289. if (obj == null || getClass() != obj.getClass()) {
  290. return false;
  291. }
  292. MinOptMax minOptMax = (MinOptMax) obj;
  293. return opt == minOptMax.opt && max == minOptMax.max && min == minOptMax.min;
  294. }
  295. /**
  296. * {@inheritDoc}
  297. */
  298. public int hashCode() {
  299. int result = min;
  300. result = 31 * result + opt;
  301. result = 31 * result + max;
  302. return result;
  303. }
  304. /**
  305. * {@inheritDoc}
  306. */
  307. public String toString() {
  308. return "MinOptMax[min = " + min + ", opt = " + opt + ", max = " + max + "]";
  309. }
  310. }