Skip to content

Commit 16932b3

Browse files
authored
MINOR: Adopt some Java 17 idioms for readability (#3912)
* Use Java 17 instanceof assignments * Use Java 17 instanceof assignments * Use Java 17 switch idiom
1 parent 29b3e2c commit 16932b3

9 files changed

Lines changed: 222 additions & 357 deletions

File tree

‎parquet-column/src/main/java/org/apache/parquet/column/ColumnDescriptor.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public int hashCode() {
125125
@Override
126126
public boolean equals(Object other) {
127127
if (other == this) return true;
128-
if (!(other instanceof ColumnDescriptor)) return false;
129-
ColumnDescriptor descriptor = (ColumnDescriptor) other;
128+
if (!(other instanceof ColumnDescriptor descriptor)) return false;
130129
return Arrays.equals(path, descriptor.path);
131130
}
132131

‎parquet-column/src/main/java/org/apache/parquet/column/ParquetProperties.java‎

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -251,44 +251,33 @@ public boolean isByteStreamSplitEnabled() {
251251
}
252252

253253
public boolean isByteStreamSplitEnabled(ColumnDescriptor column) {
254-
switch (column.getPrimitiveType().getPrimitiveTypeName()) {
255-
case FLOAT:
256-
case DOUBLE:
257-
return byteStreamSplitEnabled.getValue(column) != ByteStreamSplitMode.NONE;
258-
case INT32:
259-
case INT64:
260-
case FIXED_LEN_BYTE_ARRAY:
261-
return byteStreamSplitEnabled.getValue(column) == ByteStreamSplitMode.EXTENDED;
262-
default:
263-
return false;
264-
}
254+
return switch (column.getPrimitiveType().getPrimitiveTypeName()) {
255+
case FLOAT, DOUBLE -> byteStreamSplitEnabled.getValue(column) != ByteStreamSplitMode.NONE;
256+
case INT32, INT64, FIXED_LEN_BYTE_ARRAY ->
257+
byteStreamSplitEnabled.getValue(column) == ByteStreamSplitMode.EXTENDED;
258+
default -> false;
259+
};
265260
}
266261

267262
public ByteBufferAllocator getAllocator() {
268263
return allocator;
269264
}
270265

271266
public ColumnWriteStore newColumnWriteStore(MessageType schema, PageWriteStore pageStore) {
272-
switch (writerVersion) {
273-
case PARQUET_1_0:
274-
return new ColumnWriteStoreV1(schema, pageStore, this);
275-
case PARQUET_2_0:
276-
return new ColumnWriteStoreV2(schema, pageStore, this);
277-
default:
278-
throw new IllegalArgumentException("unknown version " + writerVersion);
279-
}
267+
return switch (writerVersion) {
268+
case PARQUET_1_0 -> new ColumnWriteStoreV1(schema, pageStore, this);
269+
case PARQUET_2_0 -> new ColumnWriteStoreV2(schema, pageStore, this);
270+
default -> throw new IllegalArgumentException("unknown version " + writerVersion);
271+
};
280272
}
281273

282274
public ColumnWriteStore newColumnWriteStore(
283275
MessageType schema, PageWriteStore pageStore, BloomFilterWriteStore bloomFilterWriteStore) {
284-
switch (writerVersion) {
285-
case PARQUET_1_0:
286-
return new ColumnWriteStoreV1(schema, pageStore, bloomFilterWriteStore, this);
287-
case PARQUET_2_0:
288-
return new ColumnWriteStoreV2(schema, pageStore, bloomFilterWriteStore, this);
289-
default:
290-
throw new IllegalArgumentException("unknown version " + writerVersion);
291-
}
276+
return switch (writerVersion) {
277+
case PARQUET_1_0 -> new ColumnWriteStoreV1(schema, pageStore, bloomFilterWriteStore, this);
278+
case PARQUET_2_0 -> new ColumnWriteStoreV2(schema, pageStore, bloomFilterWriteStore, this);
279+
default -> throw new IllegalArgumentException("unknown version " + writerVersion);
280+
};
292281
}
293282

294283
public int getMinRowCountForPageSizeCheck() {

‎parquet-column/src/main/java/org/apache/parquet/schema/LogicalTypeAnnotation.java‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,9 @@ protected String typeParametersAsString() {
592592

593593
@Override
594594
public boolean equals(Object obj) {
595-
if (!(obj instanceof DecimalLogicalTypeAnnotation)) {
595+
if (!(obj instanceof DecimalLogicalTypeAnnotation other)) {
596596
return false;
597597
}
598-
DecimalLogicalTypeAnnotation other = (DecimalLogicalTypeAnnotation) obj;
599598
return scale == other.scale && precision == other.precision;
600599
}
601600

@@ -713,10 +712,9 @@ public boolean isAdjustedToUTC() {
713712

714713
@Override
715714
public boolean equals(Object obj) {
716-
if (!(obj instanceof TimeLogicalTypeAnnotation)) {
715+
if (!(obj instanceof TimeLogicalTypeAnnotation other)) {
717716
return false;
718717
}
719-
TimeLogicalTypeAnnotation other = (TimeLogicalTypeAnnotation) obj;
720718
return isAdjustedToUTC == other.isAdjustedToUTC && unit == other.unit;
721719
}
722720

@@ -792,10 +790,9 @@ public boolean isAdjustedToUTC() {
792790

793791
@Override
794792
public boolean equals(Object obj) {
795-
if (!(obj instanceof TimestampLogicalTypeAnnotation)) {
793+
if (!(obj instanceof TimestampLogicalTypeAnnotation other)) {
796794
return false;
797795
}
798-
TimestampLogicalTypeAnnotation other = (TimestampLogicalTypeAnnotation) obj;
799796
return (isAdjustedToUTC == other.isAdjustedToUTC) && (unit == other.unit);
800797
}
801798

@@ -881,10 +878,9 @@ public boolean isSigned() {
881878

882879
@Override
883880
public boolean equals(Object obj) {
884-
if (!(obj instanceof IntLogicalTypeAnnotation)) {
881+
if (!(obj instanceof IntLogicalTypeAnnotation other)) {
885882
return false;
886883
}
887-
IntLogicalTypeAnnotation other = (IntLogicalTypeAnnotation) obj;
888884
return (bitWidth == other.bitWidth) && (isSigned == other.isSigned);
889885
}
890886

@@ -1220,11 +1216,10 @@ protected String typeParametersAsString() {
12201216

12211217
@Override
12221218
public boolean equals(Object obj) {
1223-
if (!(obj instanceof VariantLogicalTypeAnnotation)) {
1219+
if (!(obj instanceof VariantLogicalTypeAnnotation other)) {
12241220
return false;
12251221
}
12261222

1227-
VariantLogicalTypeAnnotation other = (VariantLogicalTypeAnnotation) obj;
12281223
return specVersion == other.specVersion;
12291224
}
12301225
}
@@ -1266,10 +1261,9 @@ public String getCrs() {
12661261

12671262
@Override
12681263
public boolean equals(Object obj) {
1269-
if (!(obj instanceof GeometryLogicalTypeAnnotation)) {
1264+
if (!(obj instanceof GeometryLogicalTypeAnnotation other)) {
12701265
return false;
12711266
}
1272-
GeometryLogicalTypeAnnotation other = (GeometryLogicalTypeAnnotation) obj;
12731267
return Objects.equals(crs, other.crs);
12741268
}
12751269

@@ -1329,10 +1323,9 @@ public EdgeInterpolationAlgorithm getAlgorithm() {
13291323

13301324
@Override
13311325
public boolean equals(Object obj) {
1332-
if (!(obj instanceof GeographyLogicalTypeAnnotation)) {
1326+
if (!(obj instanceof GeographyLogicalTypeAnnotation other)) {
13331327
return false;
13341328
}
1335-
GeographyLogicalTypeAnnotation other = (GeographyLogicalTypeAnnotation) obj;
13361329
return Objects.equals(crs, other.crs) && Objects.equals(algorithm, other.algorithm);
13371330
}
13381331

‎parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,16 @@ public static int readIntLittleEndianOnThreeBytes(InputStream in) throws IOExcep
124124
public static int readIntLittleEndianPaddedOnBitWidth(InputStream in, int bitWidth) throws IOException {
125125

126126
int bytesWidth = paddedByteCountFromBits(bitWidth);
127-
switch (bytesWidth) {
128-
case 0:
129-
return 0;
130-
case 1:
131-
return BytesUtils.readIntLittleEndianOnOneByte(in);
132-
case 2:
133-
return BytesUtils.readIntLittleEndianOnTwoBytes(in);
134-
case 3:
135-
return BytesUtils.readIntLittleEndianOnThreeBytes(in);
136-
case 4:
137-
return BytesUtils.readIntLittleEndian(in);
138-
default:
127+
return switch (bytesWidth) {
128+
case 0 -> 0;
129+
case 1 -> BytesUtils.readIntLittleEndianOnOneByte(in);
130+
case 2 -> BytesUtils.readIntLittleEndianOnTwoBytes(in);
131+
case 3 -> BytesUtils.readIntLittleEndianOnThreeBytes(in);
132+
case 4 -> BytesUtils.readIntLittleEndian(in);
133+
default ->
139134
throw new IOException(
140135
String.format("Encountered bitWidth (%d) that requires more than 4 bytes", bitWidth));
141-
}
136+
};
142137
}
143138

144139
public static void writeIntLittleEndianOnOneByte(OutputStream out, int v) throws IOException {

‎parquet-encoding/src/main/java/org/apache/parquet/column/values/bitpacking/BitPacking.java‎

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,18 @@ private BitPacking() {}
7373
* @return the correct implementation for the width
7474
*/
7575
public static BitPackingWriter getBitPackingWriter(int bitLength, OutputStream out) {
76-
switch (bitLength) {
77-
case 0:
78-
return new ZeroBitPackingWriter();
79-
case 1:
80-
return new OneBitPackingWriter(out);
81-
case 2:
82-
return new TwoBitPackingWriter(out);
83-
case 3:
84-
return new ThreeBitPackingWriter(out);
85-
case 4:
86-
return new FourBitPackingWriter(out);
87-
case 5:
88-
return new FiveBitPackingWriter(out);
89-
case 6:
90-
return new SixBitPackingWriter(out);
91-
case 7:
92-
return new SevenBitPackingWriter(out);
93-
case 8:
94-
return new EightBitPackingWriter(out);
95-
default:
96-
throw new UnsupportedOperationException("only support up to 8 for now");
97-
}
76+
return switch (bitLength) {
77+
case 0 -> new ZeroBitPackingWriter();
78+
case 1 -> new OneBitPackingWriter(out);
79+
case 2 -> new TwoBitPackingWriter(out);
80+
case 3 -> new ThreeBitPackingWriter(out);
81+
case 4 -> new FourBitPackingWriter(out);
82+
case 5 -> new FiveBitPackingWriter(out);
83+
case 6 -> new SixBitPackingWriter(out);
84+
case 7 -> new SevenBitPackingWriter(out);
85+
case 8 -> new EightBitPackingWriter(out);
86+
default -> throw new UnsupportedOperationException("only support up to 8 for now");
87+
};
9888
}
9989

10090
/**
@@ -104,28 +94,18 @@ public static BitPackingWriter getBitPackingWriter(int bitLength, OutputStream o
10494
* @return the correct implementation for the width
10595
*/
10696
public static BitPackingReader createBitPackingReader(int bitLength, InputStream in, long valueCount) {
107-
switch (bitLength) {
108-
case 0:
109-
return new ZeroBitPackingReader();
110-
case 1:
111-
return new OneBitPackingReader(in);
112-
case 2:
113-
return new TwoBitPackingReader(in);
114-
case 3:
115-
return new ThreeBitPackingReader(in, valueCount);
116-
case 4:
117-
return new FourBitPackingReader(in);
118-
case 5:
119-
return new FiveBitPackingReader(in, valueCount);
120-
case 6:
121-
return new SixBitPackingReader(in, valueCount);
122-
case 7:
123-
return new SevenBitPackingReader(in, valueCount);
124-
case 8:
125-
return new EightBitPackingReader(in);
126-
default:
127-
throw new UnsupportedOperationException("only support up to 8 for now");
128-
}
97+
return switch (bitLength) {
98+
case 0 -> new ZeroBitPackingReader();
99+
case 1 -> new OneBitPackingReader(in);
100+
case 2 -> new TwoBitPackingReader(in);
101+
case 3 -> new ThreeBitPackingReader(in, valueCount);
102+
case 4 -> new FourBitPackingReader(in);
103+
case 5 -> new FiveBitPackingReader(in, valueCount);
104+
case 6 -> new SixBitPackingReader(in, valueCount);
105+
case 7 -> new SevenBitPackingReader(in, valueCount);
106+
case 8 -> new EightBitPackingReader(in);
107+
default -> throw new UnsupportedOperationException("only support up to 8 for now");
108+
};
129109
}
130110
}
131111

‎parquet-hadoop/src/main/java/org/apache/parquet/filter2/dictionarylevel/DictionaryFilter.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ private static boolean isNaNLiteral(ColumnChunkMetaData meta, Object value) {
103103
PrimitiveTypeName type = meta.getPrimitiveType().getPrimitiveTypeName();
104104
switch (type) {
105105
case FLOAT:
106-
return value instanceof Float && Float.isNaN((Float) value);
106+
return value instanceof Float floatValue && Float.isNaN(floatValue);
107107
case DOUBLE:
108-
return value instanceof Double && Double.isNaN((Double) value);
108+
return value instanceof Double doubleValue && Double.isNaN(doubleValue);
109109
case FIXED_LEN_BYTE_ARRAY:
110110
return isFloat16(meta)
111-
&& value instanceof Binary
112-
&& ((Binary) value).length() == 2
113-
&& Float16.isNaN(((Binary) value).get2BytesLittleEndian());
111+
&& value instanceof Binary binaryValue
112+
&& binaryValue.length() == 2
113+
&& Float16.isNaN(binaryValue.get2BytesLittleEndian());
114114
default:
115115
return false;
116116
}

0 commit comments

Comments
 (0)