You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/11/20 20:40:02 UTC
[commons-imaging] branch master updated: Use a switch instead of a
cascading if-else.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git
The following commit(s) were added to refs/heads/master by this push:
new b9976ba Use a switch instead of a cascading if-else.
b9976ba is described below
commit b9976baec93dade6388a2f1ee6f21a9bee84d0f0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Nov 20 15:39:57 2020 -0500
Use a switch instead of a cascading if-else.
---
.../commons/imaging/common/BasicCParser.java | 144 +++++++++++++--------
.../imaging/common/itu_t4/T4AndT6Compression.java | 38 +++---
.../imaging/formats/bmp/PixelParserBitFields.java | 15 ++-
.../imaging/formats/jpeg/JpegImageParser.java | 59 ++++++---
.../imaging/formats/jpeg/decoder/JpegDecoder.java | 12 +-
.../formats/tiff/datareaders/BitInputStream.java | 26 ++--
.../formats/tiff/write/TiffImageWriterBase.java | 12 +-
7 files changed, 193 insertions(+), 113 deletions(-)
diff --git a/src/main/java/org/apache/commons/imaging/common/BasicCParser.java b/src/main/java/org/apache/commons/imaging/common/BasicCParser.java
index 29c8349..059e158 100644
--- a/src/main/java/org/apache/commons/imaging/common/BasicCParser.java
+++ b/src/main/java/org/apache/commons/imaging/common/BasicCParser.java
@@ -48,21 +48,26 @@ public class BasicCParser {
final StringBuilder token = new StringBuilder();
for (int c = is.read(); c != -1; c = is.read()) {
if (inString) {
- if (c == '\\') {
+ switch (c) {
+ case '\\':
token.append('\\');
hadBackSlash = !hadBackSlash;
- } else if (c == '"') {
+ break;
+ case '"':
token.append('"');
if (!hadBackSlash) {
return token.toString();
}
hadBackSlash = false;
- } else if (c == '\r' || c == '\n') {
+ break;
+ case '\r':
+ case '\n':
throw new ImageReadException(
"Unterminated string in XPM file");
- } else {
+ default:
token.append((char) c);
hadBackSlash = false;
+ break;
}
} else if (inIdentifier) {
if (Character.isLetterOrDigit(c) || c == '_') {
@@ -141,7 +146,8 @@ public class BasicCParser {
}
}
} else if (inSingleQuotes) {
- if (c == '\\') {
+ switch (c) {
+ case '\\':
if (hadBackSlash) {
out.write('\\');
out.write('\\');
@@ -149,7 +155,8 @@ public class BasicCParser {
} else {
hadBackSlash = true;
}
- } else if (c == '\'') {
+ break;
+ case '\'':
if (hadBackSlash) {
out.write('\\');
hadBackSlash = false;
@@ -157,17 +164,21 @@ public class BasicCParser {
inSingleQuotes = false;
}
out.write('\'');
- } else if (c == '\r' || c == '\n') {
+ break;
+ case '\r':
+ case '\n':
throw new ImageReadException("Unterminated single quote in file");
- } else {
+ default:
if (hadBackSlash) {
out.write('\\');
hadBackSlash = false;
}
out.write(c);
+ break;
}
} else if (inString) {
- if (c == '\\') {
+ switch (c) {
+ case '\\':
if (hadBackSlash) {
out.write('\\');
out.write('\\');
@@ -175,7 +186,8 @@ public class BasicCParser {
} else {
hadBackSlash = true;
}
- } else if (c == '"') {
+ break;
+ case '"':
if (hadBackSlash) {
out.write('\\');
hadBackSlash = false;
@@ -183,14 +195,17 @@ public class BasicCParser {
inString = false;
}
out.write('"');
- } else if (c == '\r' || c == '\n') {
+ break;
+ case '\r':
+ case '\n':
throw new ImageReadException("Unterminated string in file");
- } else {
+ default:
if (hadBackSlash) {
out.write('\\');
hadBackSlash = false;
}
out.write(c);
+ break;
}
} else if (inDirective) {
if (c == '\r' || c == '\n') {
@@ -210,38 +225,44 @@ public class BasicCParser {
directiveBuffer.append((char) c);
}
} else {
- if (c == '/') {
+ switch (c) {
+ case '/':
if (hadSlash) {
out.write('/');
}
hadSlash = true;
- } else if (c == '*') {
+ break;
+ case '*':
if (hadSlash) {
inComment = true;
hadSlash = false;
} else {
out.write(c);
}
- } else if (c == '\'') {
+ break;
+ case '\'':
if (hadSlash) {
out.write('/');
}
hadSlash = false;
out.write(c);
inSingleQuotes = true;
- } else if (c == '"') {
+ break;
+ case '"':
if (hadSlash) {
out.write('/');
}
hadSlash = false;
out.write(c);
inString = true;
- } else if (c == '#') {
+ break;
+ case '#':
if (defines == null) {
throw new ImageReadException("Unexpected preprocessor directive");
}
inDirective = true;
- } else {
+ break;
+ default:
if (hadSlash) {
out.write('/');
}
@@ -251,6 +272,7 @@ public class BasicCParser {
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
seenFirstComment = true;
}
+ break;
}
}
}
@@ -326,42 +348,58 @@ public class BasicCParser {
+ "hex constant invalid", nfe);
}
stringBuilder.append((char) constant);
- } else if (c == '0' || c == '1' || c == '2' || c == '3'
- || c == '4' || c == '5' || c == '6' || c == '7') {
- int length = 1;
- if (i + 1 < string.length() && '0' <= string.charAt(i + 1)
- && string.charAt(i + 1) <= '7') {
- ++length;
- }
- if (i + 2 < string.length() && '0' <= string.charAt(i + 2)
- && string.charAt(i + 2) <= '7') {
- ++length;
- }
- int constant = 0;
- for (int j = 0; j < length; j++) {
- constant *= 8;
- constant += (string.charAt(i + j) - '0');
+ } else
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ int length = 1;
+ if (i + 1 < string.length() && '0' <= string.charAt(i + 1)
+ && string.charAt(i + 1) <= '7') {
+ ++length;
+ }
+ if (i + 2 < string.length() && '0' <= string.charAt(i + 2)
+ && string.charAt(i + 2) <= '7') {
+ ++length;
+ }
+ int constant = 0;
+ for (int j = 0; j < length; j++) {
+ constant *= 8;
+ constant += (string.charAt(i + j) - '0');
+ }
+ i += length - 1;
+ stringBuilder.append((char) constant);
+ break;
+ case 'a':
+ stringBuilder.append((char) 0x07);
+ break;
+ case 'b':
+ stringBuilder.append((char) 0x08);
+ break;
+ case 'f':
+ stringBuilder.append((char) 0x0c);
+ break;
+ case 'n':
+ stringBuilder.append((char) 0x0a);
+ break;
+ case 'r':
+ stringBuilder.append((char) 0x0d);
+ break;
+ case 't':
+ stringBuilder.append((char) 0x09);
+ break;
+ case 'v':
+ stringBuilder.append((char) 0x0b);
+ break;
+ default:
+ throw new ImageReadException("Parsing XPM file failed, "
+ + "invalid escape sequence");
}
- i += length - 1;
- stringBuilder.append((char) constant);
- } else if (c == 'a') {
- stringBuilder.append((char) 0x07);
- } else if (c == 'b') {
- stringBuilder.append((char) 0x08);
- } else if (c == 'f') {
- stringBuilder.append((char) 0x0c);
- } else if (c == 'n') {
- stringBuilder.append((char) 0x0a);
- } else if (c == 'r') {
- stringBuilder.append((char) 0x0d);
- } else if (c == 't') {
- stringBuilder.append((char) 0x09);
- } else if (c == 'v') {
- stringBuilder.append((char) 0x0b);
- } else {
- throw new ImageReadException("Parsing XPM file failed, "
- + "invalid escape sequence");
- }
hadBackSlash = false;
} else {
if (c == '\\') {
diff --git a/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java b/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java
index 66bf31b..6b9bc10 100644
--- a/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java
+++ b/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java
@@ -247,21 +247,29 @@ public final class T4AndT6Compression {
final int a1b1 = a1 - b1;
if (-3 <= a1b1 && a1b1 <= 3) {
T4_T6_Tables.Entry entry;
- if (a1b1 == -3) {
- entry = T4_T6_Tables.VL3;
- } else if (a1b1 == -2) {
- entry = T4_T6_Tables.VL2;
- } else if (a1b1 == -1) {
- entry = T4_T6_Tables.VL1;
- } else if (a1b1 == 0) {
- entry = T4_T6_Tables.V0;
- } else if (a1b1 == 1) {
- entry = T4_T6_Tables.VR1;
- } else if (a1b1 == 2) {
- entry = T4_T6_Tables.VR2;
- } else {
- entry = T4_T6_Tables.VR3;
- }
+ switch (a1b1) {
+ case -3:
+ entry = T4_T6_Tables.VL3;
+ break;
+ case -2:
+ entry = T4_T6_Tables.VL2;
+ break;
+ case -1:
+ entry = T4_T6_Tables.VL1;
+ break;
+ case 0:
+ entry = T4_T6_Tables.V0;
+ break;
+ case 1:
+ entry = T4_T6_Tables.VR1;
+ break;
+ case 2:
+ entry = T4_T6_Tables.VR2;
+ break;
+ default:
+ entry = T4_T6_Tables.VR3;
+ break;
+ }
entry.writeBits(outputStream);
return a1;
diff --git a/src/main/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFields.java b/src/main/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFields.java
index 5512f28..1862ae6 100644
--- a/src/main/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFields.java
+++ b/src/main/java/org/apache/commons/imaging/formats/bmp/PixelParserBitFields.java
@@ -80,19 +80,24 @@ class PixelParserBitFields extends PixelParserSimple {
public int getNextRGB() throws ImageReadException, IOException {
int data;
- if (bhi.bitsPerPixel == 8) {
+ switch (bhi.bitsPerPixel) {
+ case 8:
data = 0xff & imageData[bytecount + 0];
bytecount += 1;
- } else if (bhi.bitsPerPixel == 24) {
+ break;
+ case 24:
data = read3Bytes("Pixel", is, "BMP Image Data", ByteOrder.LITTLE_ENDIAN);
bytecount += 3;
- } else if (bhi.bitsPerPixel == 32) {
+ break;
+ case 32:
data = read4Bytes("Pixel", is, "BMP Image Data", ByteOrder.LITTLE_ENDIAN);
bytecount += 4;
- } else if (bhi.bitsPerPixel == 16) {
+ break;
+ case 16:
data = read2Bytes("Pixel", is, "BMP Image Data", ByteOrder.LITTLE_ENDIAN);
bytecount += 2;
- } else {
+ break;
+ default:
throw new ImageReadException("Unknown BitsPerPixel: " + bhi.bitsPerPixel);
}
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
index 2191e5b..4bd6d72 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
@@ -172,24 +172,32 @@ public class JpegImageParser extends ImageParser implements XmpEmbeddable {
return true;
}
- if (marker == JpegConstants.JPEG_APP13_MARKER) {
+ switch (marker) {
+ case JpegConstants.JPEG_APP13_MARKER:
// Debug.debug("app 13 segment data", segmentData.length);
result.add(new App13Segment(parser, marker, segmentData));
- } else if (marker == JpegConstants.JPEG_APP14_MARKER) {
+ break;
+ case JpegConstants.JPEG_APP14_MARKER:
result.add(new App14Segment(marker, segmentData));
- } else if (marker == JpegConstants.JPEG_APP2_MARKER) {
+ break;
+ case JpegConstants.JPEG_APP2_MARKER:
result.add(new App2Segment(marker, segmentData));
- } else if (marker == JpegConstants.JFIF_MARKER) {
+ break;
+ case JpegConstants.JFIF_MARKER:
result.add(new JfifSegment(marker, segmentData));
- } else if (Arrays.binarySearch(sofnSegments, marker) >= 0) {
- result.add(new SofnSegment(marker, segmentData));
- } else if (marker == JpegConstants.DQT_MARKER) {
- result.add(new DqtSegment(marker, segmentData));
- } else if ((marker >= JpegConstants.JPEG_APP1_MARKER)
- && (marker <= JpegConstants.JPEG_APP15_MARKER)) {
- result.add(new UnknownSegment(marker, segmentData));
- } else if (marker == JpegConstants.COM_MARKER) {
- result.add(new ComSegment(marker, segmentData));
+ break;
+ default:
+ if (Arrays.binarySearch(sofnSegments, marker) >= 0) {
+ result.add(new SofnSegment(marker, segmentData));
+ } else if (marker == JpegConstants.DQT_MARKER) {
+ result.add(new DqtSegment(marker, segmentData));
+ } else if ((marker >= JpegConstants.JPEG_APP1_MARKER)
+ && (marker <= JpegConstants.JPEG_APP15_MARKER)) {
+ result.add(new UnknownSegment(marker, segmentData));
+ } else if (marker == JpegConstants.COM_MARKER) {
+ result.add(new ComSegment(marker, segmentData));
+ }
+ break;
}
if (returnAfterFirst) {
@@ -822,16 +830,22 @@ public class JpegImageParser extends ImageParser implements XmpEmbeddable {
// JFIF is meant to win but in them APP14 is clearly right, so make it win.
if (app14Segment != null && app14Segment.isAdobeJpegSegment()) {
final int colorTransform = app14Segment.getAdobeColorTransform();
- if (colorTransform == App14Segment.ADOBE_COLOR_TRANSFORM_UNKNOWN) {
+ switch (colorTransform) {
+ case App14Segment.ADOBE_COLOR_TRANSFORM_UNKNOWN:
if (numberOfComponents == 3) {
colorType = ImageInfo.ColorType.RGB;
} else if (numberOfComponents == 4) {
colorType = ImageInfo.ColorType.CMYK;
}
- } else if (colorTransform == App14Segment.ADOBE_COLOR_TRANSFORM_YCbCr) {
+ break;
+ case App14Segment.ADOBE_COLOR_TRANSFORM_YCbCr:
colorType = ImageInfo.ColorType.YCbCr;
- } else if (colorTransform == App14Segment.ADOBE_COLOR_TRANSFORM_YCCK) {
+ break;
+ case App14Segment.ADOBE_COLOR_TRANSFORM_YCCK:
colorType = ImageInfo.ColorType.YCCK;
+ break;
+ default:
+ break;
}
} else if (jfifSegment != null) {
if (numberOfComponents == 1) {
@@ -840,12 +854,16 @@ public class JpegImageParser extends ImageParser implements XmpEmbeddable {
colorType = ImageInfo.ColorType.YCbCr;
}
} else {
- if (numberOfComponents == 1) {
+ switch (numberOfComponents) {
+ case 1:
colorType = ImageInfo.ColorType.GRAYSCALE;
- } else if (numberOfComponents == 2) {
+ break;
+ case 2:
colorType = ImageInfo.ColorType.GRAYSCALE;
transparent = true;
- } else if (numberOfComponents == 3 || numberOfComponents == 4) {
+ break;
+ case 3:
+ case 4:
boolean have1 = false;
boolean have2 = false;
boolean have3 = false;
@@ -942,6 +960,9 @@ public class JpegImageParser extends ImageParser implements XmpEmbeddable {
}
}
}
+ break;
+ default:
+ break;
}
}
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java
index 857d6c1..7722067 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java
@@ -110,17 +110,20 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor {
final int[] preds = new int[sofnSegment.numberOfComponents];
ColorModel colorModel;
WritableRaster raster;
- if (sofnSegment.numberOfComponents == 4) {
+ switch (sofnSegment.numberOfComponents) {
+ case 4:
colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
int bandMasks[] = new int[] { 0x00ff0000, 0x0000ff00, 0x000000ff };
raster = Raster.createPackedRaster(DataBuffer.TYPE_INT, sofnSegment.width, sofnSegment.height, bandMasks, null);
- } else if (sofnSegment.numberOfComponents == 3) {
+ break;
+ case 3:
colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00,
0x000000ff);
raster = Raster.createPackedRaster(DataBuffer.TYPE_INT,
sofnSegment.width, sofnSegment.height, new int[] {
0x00ff0000, 0x0000ff00, 0x000000ff }, null);
- } else if (sofnSegment.numberOfComponents == 1) {
+ break;
+ case 1:
colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00,
0x000000ff);
raster = Raster.createPackedRaster(DataBuffer.TYPE_INT,
@@ -132,7 +135,8 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor {
// Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
// raster = colorModel.createCompatibleWritableRaster(
// sofnSegment.width, sofnSegment.height);
- } else {
+ break;
+ default:
throw new ImageReadException(sofnSegment.numberOfComponents
+ " components are invalid or unsupported");
}
diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/BitInputStream.java b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/BitInputStream.java
index 045643f..5d21798 100644
--- a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/BitInputStream.java
+++ b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/BitInputStream.java
@@ -95,35 +95,33 @@ class BitInputStream extends InputStream {
* (may be all) of the files will be of Little Endian.
*/
if (byteOrder == ByteOrder.BIG_ENDIAN) {
- if (count == 16) {
+ switch (count) {
+ case 16:
bytesRead += 2;
return (is.read() << 8) | (is.read() << 0);
- }
-
- if (count == 24) {
+ case 24:
bytesRead += 3;
return (is.read() << 16) | (is.read() << 8) | (is.read() << 0);
- }
-
- if (count == 32) {
+ case 32:
bytesRead += 4;
return (is.read() << 24) | (is.read() << 16) | (is.read() << 8)
| (is.read() << 0);
+ default:
+ break;
}
} else {
- if (count == 16) {
+ switch (count) {
+ case 16:
bytesRead += 2;
return ((is.read() << 0) | (is.read() << 8));
- }
-
- if (count == 24) {
+ case 24:
bytesRead += 3;
return ((is.read() << 0) | (is.read() << 8) | (is.read() << 16));
- }
-
- if (count == 32) {
+ case 32:
bytesRead += 4;
return ((is.read() << 0) | (is.read() << 8) | (is.read() << 16) | (is.read() << 24));
+ default:
+ break;
}
}
diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java
index 43c8da4..23d8d60 100644
--- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java
+++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java
@@ -513,19 +513,25 @@ public abstract class TiffImageWriterBase {
directory.add(TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL,
(short) samplesPerPixel);
- if (samplesPerPixel == 3) {
+ switch (samplesPerPixel) {
+ case 3:
directory.add(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE,
(short) bitsPerSample, (short) bitsPerSample,
(short) bitsPerSample);
- }else if (samplesPerPixel == 4) {
+ break;
+ case 4:
directory.add(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE,
(short) bitsPerSample, (short) bitsPerSample,
(short) bitsPerSample, (short) bitsPerSample);
directory.add(TiffTagConstants.TIFF_TAG_EXTRA_SAMPLES,
(short)TiffTagConstants.EXTRA_SAMPLE_UNASSOCIATED_ALPHA);
- } else if (samplesPerPixel == 1) {
+ break;
+ case 1:
directory.add(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE,
(short) bitsPerSample);
+ break;
+ default:
+ break;
}
// {
// stripOffsetsField = new WriteField(TIFF_TAG_STRIP_OFFSETS,