Skip to content

Commit

Permalink
[Improvement] modify code to avoid raw use of parameterized class (#46)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
1. reduce the raw use of parameterized class
2. use `Collections.singletonList` instead of `Arrays.asList` for single element
3. add type annotation for some method signature and variable declaration

### Why are the changes needed?
This makes javac and IntelliJ's compiler happy


### Does this PR introduce _any_ user-facing change?
NO

### How was this patch tested?
Existing tests should be sufficient.
  • Loading branch information
advancedxy committed Jul 10, 2022
1 parent ae674a6 commit 1be8451
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,20 @@ public static class TypedConfigOptionBuilder<T> {
this.converter = converter;
}

public ListConfigOptionBuilder asList() {
public ListConfigOptionBuilder<T> asList() {
return new ListConfigOptionBuilder<T>(key, clazz, converter);
}

// todo: errorMsg shouldn't contain key
public TypedConfigOptionBuilder checkValue(Function<T, Boolean> checkValue, String errMsg) {
public TypedConfigOptionBuilder<T> checkValue(Function<T, Boolean> checkValue, String errMsg) {
Function<Object, T> newConverter = (v) -> {
T newValue = this.converter.apply(v);
if (!checkValue.apply(newValue)) {
throw new IllegalArgumentException(errMsg);
}
return newValue;
};
return new TypedConfigOptionBuilder(key, clazz, newConverter);
return new TypedConfigOptionBuilder<>(key, clazz, newConverter);
}

/**
Expand Down Expand Up @@ -250,7 +250,7 @@ public ListConfigOptionBuilder(String key, Class<E> clazz, Function<Object, E> a
};
}

public ListConfigOptionBuilder checkValue(Function<E, Boolean> checkValueFunc, String errMsg) {
public ListConfigOptionBuilder<E> checkValue(Function<E, Boolean> checkValueFunc, String errMsg) {
final Function<Object, List<E>> listConverFunc = asListConverter;
Function<Object, List<E>> newConverter = (v) -> {
List<E> list = listConverFunc.apply(v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ static Double convertToDouble(Object o) {
return Double.parseDouble(o.toString());
}

public static List<ConfigOption> getAllConfigOptions(Class confClass) {
List<ConfigOption> configOptionList = Lists.newArrayList();
public static List<ConfigOption<Object>> getAllConfigOptions(Class confClass) {
List<ConfigOption<Object>> configOptionList = Lists.newArrayList();
try {
Field[] fields = confClass.getFields();
for (Field field : fields) {
int modifiers = field.getModifiers();
if (isStatic(modifiers) && isPublic(modifiers)
&& isFinal(modifiers) && field.getType().isAssignableFrom(ConfigOption.class)) {
configOptionList.add((ConfigOption) field.get(null));
configOptionList.add((ConfigOption<Object>) field.get(null));
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public boolean loadCommonConf(Map<String, String> properties) {
return false;
}

List<ConfigOption> configOptions = ConfigUtils.getAllConfigOptions(RssBaseConf.class);
List<ConfigOption<Object>> configOptions = ConfigUtils.getAllConfigOptions(RssBaseConf.class);
properties.forEach((k, v) -> {
configOptions.forEach(config -> {
if (config.key().equalsIgnoreCase(k)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
Expand Down Expand Up @@ -153,26 +153,26 @@ public void getMetricNameForHostNameTest() {

@Test
public void testLoadExtentions() {
List<String> exts = Arrays.asList("Dummy");
List<String> exts = Collections.singletonList("Dummy");
try {
RssUtils.loadExtensions(RssUtilTestDummy.class, exts, 1);
} catch (RuntimeException e) {
assertTrue(e.getMessage().startsWith("java.lang.ClassNotFoundException: Dummy"));
}
exts = Arrays.asList("org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummyFailNotSub");
exts = Collections.singletonList("org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummyFailNotSub");
try {
RssUtils.loadExtensions(RssUtilTestDummy.class, exts, 1);
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains("RssUtilTestDummyFailNotSub is not subclass of "
+ "org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummy"));
}
exts = Arrays.asList("org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummyNoConstructor");
exts = Collections.singletonList("org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummyNoConstructor");
try {
RssUtils.loadExtensions(RssUtilTestDummy.class, exts, "Test");
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains("RssUtilTestDummyNoConstructor.<init>()"));
}
exts = Arrays.asList("org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummySuccess");
exts = Collections.singletonList("org.apache.uniffle.common.util.RssUtilsTest$RssUtilTestDummySuccess");
String testStr = String.valueOf(new Random().nextInt());
List<RssUtilTestDummy> extsObjs = RssUtils.loadExtensions(RssUtilTestDummy.class, exts, testStr);
assertEquals(1, extsObjs.size());
Expand All @@ -197,6 +197,7 @@ public String get() {
}
}

@SuppressWarnings("unchecked")
public static void setEnv(String key, String value) {
try {
Map<String, String> env = System.getenv();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public boolean loadConfFromFile(String fileName) {

loadCommonConf(properties);

List<ConfigOption> configOptions = ConfigUtils.getAllConfigOptions(CoordinatorConf.class);
List<ConfigOption<Object>> configOptions = ConfigUtils.getAllConfigOptions(CoordinatorConf.class);
properties.forEach((k, v) -> {
configOptions.forEach(config -> {
if (config.key().equalsIgnoreCase(k)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PartitionRangeAssignmentTest {

@Test
public void test() {
SortedMap sortedMap = new TreeMap();
SortedMap<PartitionRange, List<ServerNode>> sortedMap = new TreeMap<>();
for (int i = 0; i < 9; i = i + 3) {
PartitionRange range = new PartitionRange(i, i + 2);
List<ServerNode> nodes = Collections.singletonList(new ServerNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public boolean loadConfFromFile(String fileName) {

loadCommonConf(properties);

List<ConfigOption> configOptions = ConfigUtils.getAllConfigOptions(ShuffleServerConf.class);
List<ConfigOption<Object>> configOptions = ConfigUtils.getAllConfigOptions(ShuffleServerConf.class);

properties.forEach((k, v) -> {
configOptions.forEach(config -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ List<ShuffleFileInfo> selectShuffleFiles(int num, boolean forceUpload) {
return shuffleFileInfoList;
}

private List getNotUploadedPartitions(String key) {
private List<Integer> getNotUploadedPartitions(String key) {
RoaringBitmap bitmap = localStorage.getNotUploadedPartitions(key);
List<Integer> partitionList = Lists.newArrayList();
for (int p : bitmap) {
Expand Down

0 comments on commit 1be8451

Please sign in to comment.