Skip to content

Commit fc72abb

Browse files
Fix SpotBugs FE_FLOATING_POINT_EQUALITY warnings
Refactored to use `com.codename1.util.MathUtil.compare` for floating point comparisons to avoid using `Float.compare` and `Double.compare` which are not available in CLDC 1.1. Added `compare` methods to `com.codename1.util.MathUtil`. Fixed `FE_FLOATING_POINT_EQUALITY` warnings in: - CodenameOne/src/com/codename1/ui/geom/GeneralPath.java - CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java - CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java - CodenameOne/src/com/codename1/charts/models/XYSeries.java - CodenameOne/src/com/codename1/charts/models/XYValueSeries.java - CodenameOne/src/com/codename1/location/Location.java - CodenameOne/src/com/codename1/ui/Command.java - CodenameOne/src/com/codename1/ui/Stroke.java - CodenameOne/src/com/codename1/ui/layouts/LayeredLayout.java - CodenameOne/src/com/codename1/ui/plaf/Border.java Updated `generate-quality-report.py` to enforce this rule in CI.
1 parent a060d0d commit fc72abb

File tree

10 files changed

+82
-17
lines changed

10 files changed

+82
-17
lines changed

CodenameOne/src/com/codename1/charts/models/XYSeries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void remove(int index) {
188188
XYEntry<Double, Double> removedEntry = mXY.removeByIndex(index);
189189
double removedX = removedEntry.getKey();
190190
double removedY = removedEntry.getValue();
191-
if (Double.compare(removedX, mMinX) == 0 || Double.compare(removedX, mMaxX) == 0 || Double.compare(removedY, mMinY) == 0 || Double.compare(removedY, mMaxY) == 0) {
191+
if (com.codename1.util.MathUtil.compare(removedX, mMinX) == 0 || com.codename1.util.MathUtil.compare(removedX, mMaxX) == 0 || com.codename1.util.MathUtil.compare(removedY, mMinY) == 0 || com.codename1.util.MathUtil.compare(removedY, mMaxY) == 0) {
192192
initRange();
193193
}
194194
}

CodenameOne/src/com/codename1/charts/models/XYValueSeries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void add(double x, double y) {
9494
public void remove(int index) {
9595
super.remove(index);
9696
double removedValue = mValue.remove(index);
97-
if (Double.compare(removedValue, mMinValue) == 0 || Double.compare(removedValue, mMaxValue) == 0) {
97+
if (com.codename1.util.MathUtil.compare(removedValue, mMinValue) == 0 || com.codename1.util.MathUtil.compare(removedValue, mMaxValue) == 0) {
9898
initRange();
9999
}
100100
}

CodenameOne/src/com/codename1/location/Location.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public int compare(Location o1, Location o2) {
294294
*/
295295
boolean equalsLatLng(Location l) {
296296

297-
return l != null && Double.compare(l.latitude, latitude) == 0 && Double.compare(l.longitude, longitude) == 0;
297+
return l != null && com.codename1.util.MathUtil.compare(l.latitude, latitude) == 0 && com.codename1.util.MathUtil.compare(l.longitude, longitude) == 0;
298298

299299
}
300300
}

CodenameOne/src/com/codename1/ui/Command.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ public boolean equals(Object obj) {
275275
if (((Command) obj).command == null) {
276276
return obj.getClass() == getClass() && command == null &&
277277
((Command) obj).icon == icon && ((Command) obj).commandId == commandId &&
278-
((Command) obj).materialIcon == materialIcon && Float.compare(((Command) obj).materialIconSize, materialIconSize) == 0 &&
278+
((Command) obj).materialIcon == materialIcon && com.codename1.util.MathUtil.compare(((Command) obj).materialIconSize, materialIconSize) == 0 &&
279279
(Objects.equals(clientProperties, ((Command) obj).clientProperties));
280280
} else {
281281
return obj.getClass() == getClass() && ((Command) obj).command.equals(command) &&
282282
((Command) obj).icon == icon && ((Command) obj).commandId == commandId &&
283-
((Command) obj).materialIcon == materialIcon && Float.compare(((Command) obj).materialIconSize, materialIconSize) == 0 &&
283+
((Command) obj).materialIcon == materialIcon && com.codename1.util.MathUtil.compare(((Command) obj).materialIconSize, materialIconSize) == 0 &&
284284
(Objects.equals(clientProperties, ((Command) obj).clientProperties));
285285
}
286286
}

CodenameOne/src/com/codename1/ui/Stroke.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void setMiterLimit(float miterLimit) {
227227
public boolean equals(Object obj) {
228228
if (obj instanceof Stroke) {
229229
Stroke s = (Stroke) obj;
230-
return (Float.compare(s.miterLimit, miterLimit) == 0 && s.capStyle == capStyle && s.joinStyle == joinStyle && Float.compare(s.lineWidth, lineWidth) == 0);
230+
return (com.codename1.util.MathUtil.compare(s.miterLimit, miterLimit) == 0 && s.capStyle == capStyle && s.joinStyle == joinStyle && com.codename1.util.MathUtil.compare(s.lineWidth, lineWidth) == 0);
231231
}
232232
return false;
233233
}

CodenameOne/src/com/codename1/ui/layouts/LayeredLayout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,7 @@ public Inset changeReference(Container parent, Component newRef, float pos) {
32563256
// // This could potentially affect the opposite inset if it is a percentage
32573257
// referenceComponent(newRef).referencePosition(pos);
32583258
//} else {
3259-
if (newRef != referenceComponent || Float.compare(pos, referencePosition) != 0) {
3259+
if (newRef != referenceComponent || com.codename1.util.MathUtil.compare(pos, referencePosition) != 0) {
32603260
// This may potentially affect both this inset
32613261
// and the opposite inset if it is either flexible or
32623262
// percent.
@@ -3437,7 +3437,7 @@ public Inset translatePixels(int delta, boolean preferMM, Container parent) {
34373437
return this;
34383438
}
34393439
float percentDelta = delta / relH * 100f;
3440-
if (Float.compare(percentDelta, Float.NEGATIVE_INFINITY) == 0 || Float.compare(percentDelta, Float.POSITIVE_INFINITY) == 0) {
3440+
if (com.codename1.util.MathUtil.compare(percentDelta, Float.NEGATIVE_INFINITY) == 0 || com.codename1.util.MathUtil.compare(percentDelta, Float.POSITIVE_INFINITY) == 0) {
34413441
percentDelta = 0f;
34423442
}
34433443
value += percentDelta;
@@ -3450,7 +3450,7 @@ public Inset translatePixels(int delta, boolean preferMM, Container parent) {
34503450
}
34513451
float percentDelta = delta / relH * 100f;
34523452
//System.out.println("percentDelta="+percentDelta);
3453-
if (Float.compare(percentDelta, Float.NEGATIVE_INFINITY) == 0 || Float.compare(percentDelta, Float.POSITIVE_INFINITY) == 0) {
3453+
if (com.codename1.util.MathUtil.compare(percentDelta, Float.NEGATIVE_INFINITY) == 0 || com.codename1.util.MathUtil.compare(percentDelta, Float.POSITIVE_INFINITY) == 0) {
34543454
percentDelta = 0f;
34553455
}
34563456
value += percentDelta;

CodenameOne/src/com/codename1/ui/plaf/Border.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ public boolean equals(Object obj) {
11021102

11031103
boolean v = ((themeColors == b.themeColors) &&
11041104
(type == b.type) &&
1105-
(Float.compare(thickness, b.thickness) == 0) &&
1105+
(com.codename1.util.MathUtil.compare(thickness, b.thickness) == 0) &&
11061106
(colorA == b.colorA) &&
11071107
(colorB == b.colorB) &&
11081108
(colorC == b.colorC) &&

CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ ScalarUnit copy() {
13451345
public boolean equals(Object obj) {
13461346
if (obj instanceof ScalarUnit) {
13471347
ScalarUnit u = (ScalarUnit) obj;
1348-
return Float.compare(u.value, 0f) == 0 && Float.compare(value, 0f) == 0 || Float.compare(u.value, value) == 0 && u.type == type;
1348+
return com.codename1.util.MathUtil.compare(u.value, 0f) == 0 && com.codename1.util.MathUtil.compare(value, 0f) == 0 || com.codename1.util.MathUtil.compare(u.value, value) == 0 && u.type == type;
13491349
}
13501350
return false;
13511351
}

CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public RoundRectBorder stroke(Stroke stroke) {
330330
*/
331331
public RoundRectBorder stroke(float stroke, boolean mm) {
332332
strokeThickness = stroke;
333-
if (Float.compare(strokeThickness, 0f) == 0) {
333+
if (com.codename1.util.MathUtil.compare(strokeThickness, 0f) == 0) {
334334
this.stroke = null;
335335
return this;
336336
}
@@ -350,7 +350,7 @@ public RoundRectBorder stroke(float stroke, boolean mm) {
350350
* @return border instance so these calls can be chained
351351
*/
352352
public RoundRectBorder shadowSpread(float shadowSpread) {
353-
if (Float.compare(shadowSpread, this.shadowSpread) != 0) {
353+
if (com.codename1.util.MathUtil.compare(shadowSpread, this.shadowSpread) != 0) {
354354
this.shadowSpread = shadowSpread;
355355
dirty = true;
356356
}
@@ -404,7 +404,7 @@ public RoundRectBorder shadowColor(int shadowColor) {
404404
* @return border instance so these calls can be chained
405405
*/
406406
public RoundRectBorder shadowX(float shadowX) {
407-
if (Float.compare(shadowX, this.shadowX) != 0) {
407+
if (com.codename1.util.MathUtil.compare(shadowX, this.shadowX) != 0) {
408408
this.shadowX = shadowX;
409409
dirty = true;
410410
}
@@ -418,7 +418,7 @@ public RoundRectBorder shadowX(float shadowX) {
418418
* @return border instance so these calls can be chained
419419
*/
420420
public RoundRectBorder shadowY(float shadowY) {
421-
if (Float.compare(shadowY, this.shadowY) != 0) {
421+
if (com.codename1.util.MathUtil.compare(shadowY, this.shadowY) != 0) {
422422
this.shadowY = shadowY;
423423
dirty = true;
424424
}
@@ -432,7 +432,7 @@ public RoundRectBorder shadowY(float shadowY) {
432432
* @return border instance so these calls can be chained
433433
*/
434434
public RoundRectBorder shadowBlur(float shadowBlur) {
435-
if (Float.compare(shadowBlur, this.shadowBlur) != 0) {
435+
if (com.codename1.util.MathUtil.compare(shadowBlur, this.shadowBlur) != 0) {
436436
this.shadowBlur = shadowBlur;
437437
dirty = true;
438438
}
@@ -446,7 +446,7 @@ public RoundRectBorder shadowBlur(float shadowBlur) {
446446
* @return border instance so these calls can be chained
447447
*/
448448
public RoundRectBorder cornerRadius(float cornerRadius) {
449-
if (Float.compare(cornerRadius, this.cornerRadius) != 0) {
449+
if (com.codename1.util.MathUtil.compare(cornerRadius, this.cornerRadius) != 0) {
450450
this.cornerRadius = cornerRadius;
451451
dirty = true;
452452
}

CodenameOne/src/com/codename1/util/MathUtil.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,5 +1331,70 @@ public static long floor(double a) {
13311331
return (long) a;
13321332
}
13331333

1334+
/**
1335+
* Compares the two specified {@code float} values. The sign
1336+
* of the integer value returned is the same as that of the
1337+
* integer that would be returned by the call:
1338+
* <pre>
1339+
* new Float(f1).compareTo(new Float(f2))
1340+
* </pre>
1341+
*
1342+
* @param f1 the first {@code float} to compare.
1343+
* @param f2 the second {@code float} to compare.
1344+
* @return the value {@code 0} if {@code f1} is
1345+
* numerically equal to {@code f2}; a value less than
1346+
* {@code 0} if {@code f1} is numerically less than
1347+
* {@code f2}; and a value greater than {@code 0}
1348+
* if {@code f1} is numerically greater than
1349+
* {@code f2}.
1350+
* @since 1.4
1351+
*/
1352+
public static int compare(float f1, float f2) {
1353+
if (f1 < f2)
1354+
return -1; // Neither val is NaN, thisVal is smaller
1355+
if (f1 > f2)
1356+
return 1; // Neither val is NaN, thisVal is larger
1357+
1358+
// Cannot use floatToRawIntBits because of possibility of NaNs.
1359+
int thisBits = Float.floatToIntBits(f1);
1360+
int anotherBits = Float.floatToIntBits(f2);
1361+
1362+
return (thisBits == anotherBits ? 0 : // Values are equal
1363+
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1364+
1)); // (0.0, -0.0) or (NaN, !NaN)
1365+
}
1366+
1367+
/**
1368+
* Compares the two specified {@code double} values. The sign
1369+
* of the integer value returned is the same as that of the
1370+
* integer that would be returned by the call:
1371+
* <pre>
1372+
* new Double(d1).compareTo(new Double(d2))
1373+
* </pre>
1374+
*
1375+
* @param d1 the first {@code double} to compare
1376+
* @param d2 the second {@code double} to compare
1377+
* @return the value {@code 0} if {@code d1} is
1378+
* numerically equal to {@code d2}; a value less than
1379+
* {@code 0} if {@code d1} is numerically less than
1380+
* {@code d2}; and a value greater than {@code 0}
1381+
* if {@code d1} is numerically greater than
1382+
* {@code d2}.
1383+
* @since 1.4
1384+
*/
1385+
public static int compare(double d1, double d2) {
1386+
if (d1 < d2)
1387+
return -1; // Neither val is NaN, thisVal is smaller
1388+
if (d1 > d2)
1389+
return 1; // Neither val is NaN, thisVal is larger
1390+
1391+
// Cannot use doubleToRawLongBits because of possibility of NaNs.
1392+
long thisBits = Double.doubleToLongBits(d1);
1393+
long anotherBits = Double.doubleToLongBits(d2);
1394+
1395+
return (thisBits == anotherBits ? 0 : // Values are equal
1396+
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1397+
1)); // (0.0, -0.0) or (NaN, !NaN)
1398+
}
13341399

13351400
}

0 commit comments

Comments
 (0)