Skip to content
Permalink
Browse files
GEOMETRY-63: fixing various PMD and SonarQube issues
  • Loading branch information
darkma773r committed Apr 21, 2021
1 parent 3c3bc84 commit b60aee205a76a1d6112508c22dc6d3f310fe7f23
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 87 deletions.
@@ -113,7 +113,7 @@ public boolean equals(final Object obj) {
/** {@inheritDoc} */
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
final StringBuilder sb = new StringBuilder(50);
sb.append(getClass().getSimpleName())
.append("[point= ")
.append(getPoint())
@@ -152,7 +152,7 @@ public boolean equals(final Object obj) {
/** {@inheritDoc} */
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
final StringBuilder sb = new StringBuilder(30);
sb.append(this.getClass().getSimpleName())
.append("[center= ")
.append(center)
@@ -77,17 +77,21 @@ public final class SphericalCoordinates implements Spatial {
* @param azimuth Azimuth angle in radians.
* @param polar Polar angle in radians.
*/
private SphericalCoordinates(double radius, double azimuth, double polar) {
if (radius < 0) {
private SphericalCoordinates(final double radius, final double azimuth, final double polar) {
double rad = radius;
double az = azimuth;
double pol = polar;

if (rad < 0) {
// negative radius; flip the angles
radius = Math.abs(radius);
azimuth += PlaneAngleRadians.PI;
polar += PlaneAngleRadians.PI;
rad = Math.abs(rad);
az += PlaneAngleRadians.PI;
pol += PlaneAngleRadians.PI;
}

this.radius = radius;
this.azimuth = normalizeAzimuth(azimuth);
this.polar = normalizePolar(polar);
this.radius = rad;
this.azimuth = normalizeAzimuth(az);
this.polar = normalizePolar(pol);
}

/** Return the radius value. The value is in the range {@code [0, +Infinity)}.
@@ -285,11 +289,11 @@ public static double normalizeAzimuth(final double azimuth) {
* @param polar polar value in radians
* @return equivalent polar value in the range {@code [0, +pi]}
*/
public static double normalizePolar(double polar) {
public static double normalizePolar(final double polar) {
// normalize the polar angle; this is the angle between the polar vector and the point ray
// so it is unsigned (unlike the azimuth) and should be in the range [0, pi]
if (Double.isFinite(polar)) {
polar = Math.abs(PlaneAngleRadians.normalizeBetweenMinusPiAndPi(polar));
return Math.abs(PlaneAngleRadians.normalizeBetweenMinusPiAndPi(polar));
}

return polar;
@@ -57,15 +57,18 @@ public final class PolarCoordinates implements Spatial {
* @param radius Radius value.
* @param azimuth Azimuth angle in radians.
*/
private PolarCoordinates(double radius, double azimuth) {
if (radius < 0) {
private PolarCoordinates(final double radius, final double azimuth) {
double rad = radius;
double az = azimuth;

if (rad < 0) {
// negative radius; flip the angles
radius = Math.abs(radius);
azimuth += PlaneAngleRadians.PI;
rad = Math.abs(radius);
az += PlaneAngleRadians.PI;
}

this.radius = radius;
this.azimuth = normalizeAzimuth(azimuth);
this.radius = rad;
this.azimuth = normalizeAzimuth(az);
}

/** Return the radius value. The value will be greater than or equal to 0.
@@ -228,9 +231,9 @@ public static PolarCoordinates parse(final String input) {
* @param azimuth azimuth value in radians
* @return equivalent azimuth value in the range {@code [0, 2pi)}.
*/
public static double normalizeAzimuth(double azimuth) {
public static double normalizeAzimuth(final double azimuth) {
if (Double.isFinite(azimuth)) {
azimuth = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(azimuth);
return PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(azimuth);
}

return azimuth;
@@ -104,7 +104,7 @@ public List<R> getReadHandlers() {
*/
public List<GeometryFormat> getReadFormats() {
return readRegistry.getHandlers().stream()
.map(h -> h.getFormat())
.map(BoundaryReadHandler::getFormat)
.collect(Collectors.toList());
}

@@ -162,7 +162,7 @@ public List<W> getWriteHandlers() {
*/
public List<GeometryFormat> getWriteFormats() {
return writeRegistry.getHandlers().stream()
.map(h -> h.getFormat())
.map(BoundaryWriteHandler::getFormat)
.collect(Collectors.toList());
}

@@ -48,7 +48,7 @@ public class CharReadBuffer {
private int count;

/** True when the end of reader content is reached. */
private boolean eof;
private boolean reachedEof;

/** Minimum number of characters to request for each read. */
private final int minRead;
@@ -284,7 +284,7 @@ private void pushCharInternal(final char ch) {
* @throws IOException if an I/O error occurs
*/
private void readChars(final int n) throws IOException {
if (!eof) {
if (!reachedEof) {
int remaining = Math.max(n, minRead);

ensureCapacity(count + remaining);
@@ -298,7 +298,7 @@ private void readChars(final int n) throws IOException {

read = reader.read(buffer, tail, len);
if (read == EOF) {
eof = true;
reachedEof = true;
break;
}

@@ -285,9 +285,7 @@ public SimpleTextParser next(final int len) throws IOException {
if (hasMoreCharacters()) {
final StringBuilder sb = new StringBuilder(len);

consume(len, ch -> {
sb.append((char) ch);
});
consume(len, ch -> sb.append((char) ch));

token = sb.toString();
}
@@ -320,9 +318,8 @@ public SimpleTextParser nextWithLineContinuation(final char lineContinuationChar
if (hasMoreCharacters()) {
final StringBuilder sb = new StringBuilder(len);

consumeWithLineContinuation(lineContinuationChar, len, ch -> {
sb.append((char) ch);
});
consumeWithLineContinuation(lineContinuationChar, len,
ch -> sb.append((char) ch));

token = sb.toString();
}
@@ -37,28 +37,28 @@
final class ParsedDouble {

/** Minus sign character. */
private static final char MINUS = '-';
private static final char MINUS_CHAR = '-';

/** Decimal separator character. */
private static final char DECIMAL_SEP = '.';
private static final char DECIMAL_SEP_CHAR = '.';

/** Exponent character. */
private static final char EXPONENT = 'E';
private static final char EXPONENT_CHAR = 'E';

/** Zero digit character. */
private static final char ZERO = '0';
private static final char ZERO_CHAR = '0';

/** One digit character. */
private static final char ONE = '1';
private static final char ONE_CHAR = '1';

/** String containing the digits '0' - '9' in sequence. */
private static final String DIGITS = "0123456789";
/** String containing the decimal digits '0' - '9' in sequence. */
private static final String DECIMAL_DIGITS = "0123456789";

/** Shared instance representing the positive zero double value. */
private static final ParsedDouble POS_ZERO = new ParsedDouble(false, String.valueOf(ZERO), 0);
private static final ParsedDouble POS_ZERO = new ParsedDouble(false, String.valueOf(ZERO_CHAR), 0);

/** Shared instance representing the negative zero double value. */
private static final ParsedDouble NEG_ZERO = new ParsedDouble(true, String.valueOf(ZERO), 0);
private static final ParsedDouble NEG_ZERO = new ParsedDouble(true, String.valueOf(ZERO_CHAR), 0);

/** True if the value is negative. */
private final boolean negative;
@@ -110,7 +110,7 @@ public int getExponent() {
* @return true if the value is equal to zero
*/
public boolean isZero() {
return getPrecision() == 1 && digits.charAt(0) == ZERO;
return getPrecision() == 1 && digits.charAt(0) == ZERO_CHAR;
}

/** Return the precision of this instance, meaning the number of significant decimal
@@ -211,7 +211,7 @@ public String toPlainString(final boolean includeDecimalPlaceholder) {

final StringBuilder sb = new StringBuilder();
if (negative) {
sb.append(MINUS);
sb.append(MINUS_CHAR);
}

if (exponent < 0) {
@@ -224,15 +224,15 @@ public String toPlainString(final boolean includeDecimalPlaceholder) {
sb.append(digits.charAt(i));
}
if (i == 0) {
sb.append(ZERO);
sb.append(ZERO_CHAR);
}

// decimal separator
sb.append(DECIMAL_SEP);
sb.append(DECIMAL_SEP_CHAR);

// add placeholder fraction zeros if needed
for (int j = 0; j > diff; --j) {
sb.append(ZERO);
sb.append(ZERO_CHAR);
}

// fraction digits
@@ -241,12 +241,12 @@ public String toPlainString(final boolean includeDecimalPlaceholder) {
sb.append(digits);

for (int i = 0; i < exponent; ++i) {
sb.append(ZERO);
sb.append(ZERO_CHAR);
}

if (includeDecimalPlaceholder) {
sb.append(DECIMAL_SEP)
.append(ZERO);
sb.append(DECIMAL_SEP_CHAR)
.append(ZERO_CHAR);
}
}

@@ -305,7 +305,7 @@ private String toScientificString(final int wholeDigits, final boolean includeDe

final StringBuilder sb = new StringBuilder();
if (negative) {
sb.append(MINUS);
sb.append(MINUS_CHAR);
}

if (precision <= wholeDigits) {
@@ -314,24 +314,24 @@ private String toScientificString(final int wholeDigits, final boolean includeDe
sb.append(digits);

for (int i = precision; i < wholeDigits; ++i) {
sb.append(ZERO);
sb.append(ZERO_CHAR);
}

if (includeDecimalPlaceholder) {
sb.append(DECIMAL_SEP)
.append(ZERO);
sb.append(DECIMAL_SEP_CHAR)
.append(ZERO_CHAR);
}
} else {
// we'll need a fractional portion
sb.append(digits, 0, wholeDigits)
.append(DECIMAL_SEP)
.append(DECIMAL_SEP_CHAR)
.append(digits, wholeDigits, precision);
}

// add the exponent but only if non-zero
final int resultExponent = exponent + precision - wholeDigits;
if (resultExponent != 0) {
sb.append(EXPONENT)
sb.append(EXPONENT_CHAR)
.append(resultExponent);
}

@@ -371,7 +371,7 @@ public static ParsedDouble from(final double d) {
// extract the different portions of the string representation
// (since double is finite, str is guaranteed to not be empty and to contain a
// single decimal point according to the Double.toString() API)
final boolean negative = str.charAt(0) == MINUS;
final boolean negative = str.charAt(0) == MINUS_CHAR;
final int digitStartIdx = negative ? 1 : 0;

final StringBuilder digitStr = new StringBuilder(str.length());
@@ -383,9 +383,9 @@ public static ParsedDouble from(final double d) {
for (int i = digitStartIdx; i < str.length(); ++i) {
ch = str.charAt(i);

if (ch == DECIMAL_SEP) {
if (ch == DECIMAL_SEP_CHAR) {
decimalSepIdx = i;
} else if (ch == EXPONENT) {
} else if (ch == EXPONENT_CHAR) {
exponentIdx = i;
} else if (exponentIdx < 0) {
digitStr.append(ch);
@@ -423,7 +423,7 @@ private static int findFirstNonZero(final CharSequence seq) {
char ch;
for (int i = 0; i < seq.length(); ++i) {
ch = seq.charAt(i);
if (ch != ZERO) {
if (ch != ZERO_CHAR) {
return i;
}
}
@@ -441,7 +441,7 @@ private static int findLastNonZero(final CharSequence seq) {
char ch;
for (i = seq.length() - 1; i >= 0; --i) {
ch = seq.charAt(i);
if (ch != ZERO) {
if (ch != ZERO_CHAR) {
break;
}
}
@@ -455,7 +455,7 @@ private static int findLastNonZero(final CharSequence seq) {
* @return numeric value of the digit character, ex: '1' = 1
*/
private static int digitValue(final char ch) {
return ch - ZERO;
return ch - ZERO_CHAR;
}

/** Add one to the value of the integer represented by the given string, returning
@@ -469,7 +469,7 @@ private static String addOne(final String digitStr) {
final char[] digitChars = digitStr.toCharArray();
if (addOne(digitChars)) {
return new StringBuilder()
.append(ONE)
.append(ONE_CHAR)
.append(digitChars)
.toString();
}
@@ -488,10 +488,10 @@ private static boolean addOne(final char[] digitChars) {
int i;
char c;
for (i = digitChars.length - 1; i >= 0; --i) {
c = DIGITS.charAt((digitValue(digitChars[i]) + 1) % DIGITS.length());
c = DECIMAL_DIGITS.charAt((digitValue(digitChars[i]) + 1) % DECIMAL_DIGITS.length());
digitChars[i] = c;

if (c != ZERO) {
if (c != ZERO_CHAR) {
break; // no carry over; stop
}
}

0 comments on commit b60aee2

Please sign in to comment.