Skip to content

Commit a8fb5df

Browse files
FOP-3280: Optimise memory for MinOptMax
1 parent 8736bba commit a8fb5df

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

fop-core/src/main/java/org/apache/fop/traits/MinOptMax.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.fop.traits;
2121

2222
import 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
/**

fop-core/src/test/java/org/apache/fop/traits/MinOptMaxTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class MinOptMaxTestCase {
3737
@Test
3838
public void testZero() {
3939
assertEquals(MinOptMax.getInstance(0), MinOptMax.ZERO);
40+
assertTrue(MinOptMax.getInstance(0, 0, 0) == MinOptMax.ZERO);
4041
}
4142

4243
@Test

0 commit comments

Comments
 (0)