Skip to content

Commit 923ba77

Browse files
authored
HBASE-23626 Reduced number of Checkstyle violations in tests in hbase-common
Signed-off-by: Viraj Jasani <vjasani@apache.org>
1 parent c6a9a4d commit 923ba77

10 files changed

Lines changed: 355 additions & 335 deletions

hbase-common/src/test/java/org/apache/hadoop/hbase/net/TestAddress.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
*/
1818
package org.apache.hadoop.hbase.net;
1919

20+
import static org.junit.Assert.assertEquals;
21+
2022
import org.apache.hadoop.hbase.HBaseClassTestRule;
2123
import org.apache.hadoop.hbase.testclassification.MiscTests;
2224
import org.apache.hadoop.hbase.testclassification.SmallTests;
2325
import org.junit.ClassRule;
2426
import org.junit.Test;
2527
import org.junit.experimental.categories.Category;
2628

27-
import static org.junit.Assert.assertEquals;
28-
2929
@Category({ MiscTests.class, SmallTests.class })
3030
public class TestAddress {
3131
@ClassRule

hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStruct.java

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@
4747
@RunWith(Parameterized.class)
4848
@Category({MiscTests.class, SmallTests.class})
4949
public class TestStruct {
50-
5150
@ClassRule
5251
public static final HBaseClassTestRule CLASS_RULE =
5352
HBaseClassTestRule.forClass(TestStruct.class);
5453

55-
@Parameterized.Parameter(value = 0)
54+
@Parameterized.Parameter()
5655
public Struct generic;
5756

5857
@SuppressWarnings("rawtypes")
@@ -88,16 +87,21 @@ public static Collection<Object[]> params() {
8887
return Arrays.asList(params);
8988
}
9089

91-
static final Comparator<byte[]> NULL_SAFE_BYTES_COMPARATOR =
92-
new Comparator<byte[]>() {
93-
@Override
94-
public int compare(byte[] o1, byte[] o2) {
95-
if (o1 == o2) return 0;
96-
if (null == o1) return -1;
97-
if (null == o2) return 1;
98-
return Bytes.compareTo(o1, o2);
99-
}
100-
};
90+
static final Comparator<byte[]> NULL_SAFE_BYTES_COMPARATOR = (o1, o2) -> {
91+
if (o1 == o2) {
92+
return 0;
93+
}
94+
95+
if (null == o1) {
96+
return -1;
97+
}
98+
99+
if (null == o2) {
100+
return 1;
101+
}
102+
103+
return Bytes.compareTo(o1, o2);
104+
};
101105

102106
/**
103107
* A simple object to serialize.
@@ -134,7 +138,7 @@ public int compareTo(Pojo1 o) {
134138
if (cmp != 0) {
135139
return cmp;
136140
}
137-
cmp = Integer.valueOf(intFieldAsc).compareTo(Integer.valueOf(o.intFieldAsc));
141+
cmp = Integer.compare(intFieldAsc, o.intFieldAsc);
138142
if (cmp != 0) {
139143
return cmp;
140144
}
@@ -173,13 +177,10 @@ public boolean equals(Object obj) {
173177
return false;
174178
}
175179
if (stringFieldAsc == null) {
176-
if (other.stringFieldAsc != null) {
177-
return false;
178-
}
179-
} else if (!stringFieldAsc.equals(other.stringFieldAsc)) {
180-
return false;
180+
return other.stringFieldAsc == null;
181+
} else {
182+
return stringFieldAsc.equals(other.stringFieldAsc);
181183
}
182-
return true;
183184
}
184185
}
185186

@@ -225,16 +226,17 @@ public int compareTo(Pojo2 o) {
225226
if (cmp != 0) {
226227
return cmp;
227228
}
229+
228230
if (null == stringFieldDsc) {
229231
cmp = 1;
230-
}
231-
else if (null == o.stringFieldDsc) {
232+
} else if (null == o.stringFieldDsc) {
232233
cmp = -1;
233-
}
234-
else if (stringFieldDsc.equals(o.stringFieldDsc)) {
234+
} else if (stringFieldDsc.equals(o.stringFieldDsc)) {
235235
cmp = 0;
236+
} else {
237+
cmp = -stringFieldDsc.compareTo(o.stringFieldDsc);
236238
}
237-
else cmp = -stringFieldDsc.compareTo(o.stringFieldDsc);
239+
238240
if (cmp != 0) {
239241
return cmp;
240242
}
@@ -274,56 +276,58 @@ public boolean equals(Object obj) {
274276
return false;
275277
}
276278
if (stringFieldDsc == null) {
277-
if (other.stringFieldDsc != null) {
278-
return false;
279-
}
280-
} else if (!stringFieldDsc.equals(other.stringFieldDsc)) {
281-
return false;
279+
return other.stringFieldDsc == null;
280+
} else {
281+
return stringFieldDsc.equals(other.stringFieldDsc);
282282
}
283-
return true;
284283
}
285284
}
286285

287286
/**
288287
* A custom data type implementation specialized for {@link Pojo1}.
289288
*/
290289
private static class SpecializedPojo1Type1 implements DataType<Pojo1> {
291-
292290
private static final RawStringTerminated stringField = new RawStringTerminated("/");
293291
private static final RawInteger intField = new RawInteger();
294292
private static final RawDouble doubleField = new RawDouble();
295293

296294
/**
297295
* The {@link Struct} equivalent of this type.
298296
*/
299-
public static Struct GENERIC =
300-
new StructBuilder().add(stringField)
301-
.add(intField)
302-
.add(doubleField)
303-
.toStruct();
297+
public static Struct GENERIC = new StructBuilder().add(stringField).add(intField)
298+
.add(doubleField).toStruct();
304299

305300
@Override
306-
public boolean isOrderPreserving() { return true; }
301+
public boolean isOrderPreserving() {
302+
return true;
303+
}
307304

308305
@Override
309-
public Order getOrder() { return null; }
306+
public Order getOrder() {
307+
return null;
308+
}
310309

311310
@Override
312-
public boolean isNullable() { return false; }
311+
public boolean isNullable() {
312+
return false;
313+
}
313314

314315
@Override
315-
public boolean isSkippable() { return true; }
316+
public boolean isSkippable() {
317+
return true;
318+
}
316319

317320
@Override
318321
public int encodedLength(Pojo1 val) {
319-
return
320-
stringField.encodedLength(val.stringFieldAsc) +
322+
return stringField.encodedLength(val.stringFieldAsc) +
321323
intField.encodedLength(val.intFieldAsc) +
322324
doubleField.encodedLength(val.doubleFieldAsc);
323325
}
324326

325327
@Override
326-
public Class<Pojo1> encodedClass() { return Pojo1.class; }
328+
public Class<Pojo1> encodedClass() {
329+
return Pojo1.class;
330+
}
327331

328332
@Override
329333
public int skip(PositionedByteRange src) {
@@ -355,7 +359,6 @@ public int encode(PositionedByteRange dst, Pojo1 val) {
355359
* A custom data type implementation specialized for {@link Pojo2}.
356360
*/
357361
private static class SpecializedPojo2Type1 implements DataType<Pojo2> {
358-
359362
private static RawBytesTerminated byteField1 = new RawBytesTerminated("/");
360363
private static RawBytesTerminated byteField2 =
361364
new RawBytesTerminated(Order.DESCENDING, "/");
@@ -366,36 +369,41 @@ private static class SpecializedPojo2Type1 implements DataType<Pojo2> {
366369
/**
367370
* The {@link Struct} equivalent of this type.
368371
*/
369-
public static Struct GENERIC =
370-
new StructBuilder().add(byteField1)
371-
.add(byteField2)
372-
.add(stringField)
373-
.add(byteField3)
374-
.toStruct();
372+
public static Struct GENERIC = new StructBuilder().add(byteField1).add(byteField2)
373+
.add(stringField).add(byteField3).toStruct();
375374

376375
@Override
377-
public boolean isOrderPreserving() { return true; }
376+
public boolean isOrderPreserving() {
377+
return true;
378+
}
378379

379380
@Override
380-
public Order getOrder() { return null; }
381+
public Order getOrder() {
382+
return null;
383+
}
381384

382385
@Override
383-
public boolean isNullable() { return false; }
386+
public boolean isNullable() {
387+
return false;
388+
}
384389

385390
@Override
386-
public boolean isSkippable() { return true; }
391+
public boolean isSkippable() {
392+
return true;
393+
}
387394

388395
@Override
389396
public int encodedLength(Pojo2 val) {
390-
return
391-
byteField1.encodedLength(val.byteField1Asc) +
397+
return byteField1.encodedLength(val.byteField1Asc) +
392398
byteField2.encodedLength(val.byteField2Dsc) +
393399
stringField.encodedLength(val.stringFieldDsc) +
394400
byteField3.encodedLength(val.byteField3Dsc);
395401
}
396402

397403
@Override
398-
public Class<Pojo2> encodedClass() { return Pojo2.class; }
404+
public Class<Pojo2> encodedClass() {
405+
return Pojo2.class;
406+
}
399407

400408
@Override
401409
public int skip(PositionedByteRange src) {

hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestUnion2.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.hadoop.hbase.types;
1919

2020
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
2221

2322
import org.apache.hadoop.hbase.HBaseClassTestRule;
2423
import org.apache.hadoop.hbase.testclassification.MiscTests;
@@ -30,9 +29,8 @@
3029
import org.junit.Test;
3130
import org.junit.experimental.categories.Category;
3231

33-
@Category({MiscTests.class, SmallTests.class})
32+
@Category({ MiscTests.class, SmallTests.class })
3433
public class TestUnion2 {
35-
3634
@ClassRule
3735
public static final HBaseClassTestRule CLASS_RULE =
3836
HBaseClassTestRule.forClass(TestUnion2.class);
@@ -41,7 +39,6 @@ public class TestUnion2 {
4139
* An example <code>Union</code>
4240
*/
4341
private static class SampleUnion1 extends Union2<Integer, String> {
44-
4542
private static final byte IS_INTEGER = 0x00;
4643
private static final byte IS_STRING = 0x01;
4744

@@ -79,13 +76,19 @@ public int encodedLength(Object val) {
7976
String s = null;
8077
try {
8178
i = (Integer) val;
82-
} catch (ClassCastException e) {}
79+
} catch (ClassCastException ignored) {}
8380
try {
8481
s = (String) val;
85-
} catch (ClassCastException e) {}
82+
} catch (ClassCastException ignored) {}
83+
84+
if (null != i) {
85+
return 1 + typeA.encodedLength(i);
86+
}
87+
88+
if (null != s) {
89+
return 1 + typeB.encodedLength(s);
90+
}
8691

87-
if (null != i) return 1 + typeA.encodedLength(i);
88-
if (null != s) return 1 + typeB.encodedLength(s);
8992
throw new IllegalArgumentException("val is not a valid member of this union.");
9093
}
9194

@@ -95,42 +98,42 @@ public int encode(PositionedByteRange dst, Object val) {
9598
String s = null;
9699
try {
97100
i = (Integer) val;
98-
} catch (ClassCastException e) {}
101+
} catch (ClassCastException ignored) {}
99102
try {
100103
s = (String) val;
101-
} catch (ClassCastException e) {}
104+
} catch (ClassCastException ignored) {}
102105

103106
if (null != i) {
104107
dst.put(IS_INTEGER);
105108
return 1 + typeA.encode(dst, i);
106109
} else if (null != s) {
107110
dst.put(IS_STRING);
108111
return 1 + typeB.encode(dst, s);
109-
}
110-
else
112+
} else {
111113
throw new IllegalArgumentException("val is not of a supported type.");
114+
}
112115
}
113116
}
114117

115118
@Test
116119
public void testEncodeDecode() {
117-
Integer intVal = Integer.valueOf(10);
120+
Integer intVal = 10;
118121
String strVal = "hello";
119122
PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
120123
SampleUnion1 type = new SampleUnion1();
121124

122125
type.encode(buff, intVal);
123126
buff.setPosition(0);
124-
assertTrue(0 == intVal.compareTo(type.decodeA(buff)));
127+
assertEquals(0, intVal.compareTo(type.decodeA(buff)));
125128
buff.setPosition(0);
126129
type.encode(buff, strVal);
127130
buff.setPosition(0);
128-
assertTrue(0 == strVal.compareTo(type.decodeB(buff)));
131+
assertEquals(0, strVal.compareTo(type.decodeB(buff)));
129132
}
130133

131134
@Test
132135
public void testSkip() {
133-
Integer intVal = Integer.valueOf(10);
136+
Integer intVal = 10;
134137
String strVal = "hello";
135138
PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
136139
SampleUnion1 type = new SampleUnion1();

0 commit comments

Comments
 (0)