2020package org .apache .fop .traits ;
2121
2222import java .io .Serializable ;
23+ import java .util .Objects ;
2324
2425/**
2526 * This class holds the resolved (as mpoints) form of a
@@ -37,7 +38,7 @@ public final class MinOptMax implements Serializable {
3738 /**
3839 * The zero <code>MinOptMax</code> instance with <code>min == opt == max == 0</code>.
3940 */
40- public static final MinOptMax ZERO = getInstance ( 0 );
41+ public static final MinOptMax ZERO = new MinOptMax ( 0 , 0 , 0 );
4142
4243 private final int min ;
4344 private final int opt ;
@@ -59,6 +60,9 @@ public static MinOptMax getInstance(int min, int opt, int max) throws IllegalArg
5960 if (max < opt ) {
6061 throw new IllegalArgumentException ("max (" + max + ") < opt (" + opt + ")" );
6162 }
63+ if (min == 0 && opt == 0 && max == 0 ) {
64+ return ZERO ;
65+ }
6266 return new MinOptMax (min , opt , max );
6367 }
6468
@@ -71,7 +75,7 @@ public static MinOptMax getInstance(int min, int opt, int max) throws IllegalArg
7175 * @see #isStiff()
7276 */
7377 public static MinOptMax getInstance (int value ) {
74- return new MinOptMax (value , value , value );
78+ return getInstance (value , value , value );
7579 }
7680
7781 // Private constructor without consistency checks
@@ -136,7 +140,7 @@ public int getStretch() {
136140 * @return the sum of this <code>MinOptMax</code> and the given <code>MinOptMax</code>.
137141 */
138142 public MinOptMax plus (MinOptMax operand ) {
139- return new MinOptMax (min + operand .min , opt + operand .opt , max + operand .max );
143+ return getInstance (min + operand .min , opt + operand .opt , max + operand .max );
140144 }
141145
142146
@@ -147,7 +151,7 @@ public MinOptMax plus(MinOptMax operand) {
147151 * @return the result of the addition
148152 */
149153 public MinOptMax plus (int value ) {
150- return new MinOptMax (min + value , opt + value , max + value );
154+ return getInstance (min + value , opt + value , max + value );
151155 }
152156
153157 /**
@@ -166,7 +170,7 @@ public MinOptMax plus(int value) {
166170 public MinOptMax minus (MinOptMax operand ) throws ArithmeticException {
167171 checkCompatibility (getShrink (), operand .getShrink (), "shrink" );
168172 checkCompatibility (getStretch (), operand .getStretch (), "stretch" );
169- return new MinOptMax (min - operand .min , opt - operand .opt , max - operand .max );
173+ return getInstance (min - operand .min , opt - operand .opt , max - operand .max );
170174 }
171175
172176 private void checkCompatibility (int thisElasticity , int operandElasticity , String msge ) {
@@ -184,7 +188,7 @@ private void checkCompatibility(int thisElasticity, int operandElasticity, Strin
184188 * @return the result of the subtraction
185189 */
186190 public MinOptMax minus (int value ) {
187- return new MinOptMax (min - value , opt - value , max - value );
191+ return getInstance (min - value , opt - value , max - value );
188192 }
189193
190194 /**
@@ -328,10 +332,7 @@ public boolean equals(Object obj) {
328332 * {@inheritDoc}
329333 */
330334 public int hashCode () {
331- int result = min ;
332- result = 31 * result + opt ;
333- result = 31 * result + max ;
334- return result ;
335+ return Objects .hash (min , opt , max );
335336 }
336337
337338 /**
0 commit comments