Skip to content

Commit

Permalink
Enhance toList and add toSet in CollectionUtil (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Linary authored and zhoney committed May 29, 2019
1 parent 0822dd9 commit 1a66a60
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.6.2</version>
<version>1.6.3</version>

<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
Expand Down Expand Up @@ -212,7 +212,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.6.2.0</Implementation-Version>
<Implementation-Version>1.6.3.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
36 changes: 25 additions & 11 deletions src/main/java/com/baidu/hugegraph/util/CollectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,33 @@

public final class CollectionUtil {

public static <T> Set<T> toSet(Object object) {
E.checkNotNull(object, "object");
Set<T> set = InsertionOrderUtil.newSet();
fillCollection(set, object);
return set;
}

public static <T> List<T> toList(Object object) {
E.checkNotNull(object, "object");
List<T> list = new ArrayList<>();
fillCollection(list, object);
return list;
}

@SuppressWarnings("unchecked")
public static <T> List<T> toList(Object array) {
E.checkNotNull(array, "array");
E.checkArgument(array.getClass().isArray(),
"The parameter of toList() must be an array: '%s'",
array.getClass().getSimpleName());

int length = Array.getLength(array);
List<T> list = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
list.add((T) Array.get(array, i));
private static <T> void fillCollection(Collection<T> collection,
Object object) {
if (object.getClass().isArray()) {
int length = Array.getLength(object);
for (int i = 0; i < length; i++) {
collection.add((T) Array.get(object, i));
}
} else if (object instanceof Collection) {
collection.addAll((Collection<T>) object);
} else {
collection.add((T) object);
}
return list;
}

public static <T> boolean prefixOf(List<T> prefix, List<T> all) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public class CommonVersion {

// The second parameter of Version.of() is for all-in-one JAR
public static final Version VERSION = Version.of(CommonVersion.class,
"1.6.2");
"1.6.3");
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,37 @@

public class CollectionUtilTest extends BaseUnitTest {

@Test
public void testToSet() {
Assert.assertThrows(NullPointerException.class, () -> {
CollectionUtil.toSet(null);
});

Object array1 = new Integer[]{1, 2, 3};
Assert.assertEquals(ImmutableSet.of(1, 2, 3),
CollectionUtil.toSet(array1));

Object array2 = new String[]{"1", "2", "3"};
Assert.assertEquals(ImmutableSet.of("1", "2", "3"),
CollectionUtil.toSet(array2));

Set<Integer> set = ImmutableSet.of(1, 2, 3);
Assert.assertEquals(ImmutableSet.of(1, 2, 3),
CollectionUtil.toSet(set));

List<Integer> list = ImmutableList.of(1, 2, 3);
Assert.assertEquals(ImmutableSet.of(1, 2, 3),
CollectionUtil.toSet(list));

Assert.assertEquals(ImmutableSet.of(1), CollectionUtil.toSet(1));
}

@Test
public void testToList() {
Assert.assertThrows(NullPointerException.class, () -> {
CollectionUtil.toList(null);
});

Object array1 = new Integer[]{1, 2, 3};
Assert.assertEquals(ImmutableList.of(1, 2, 3),
CollectionUtil.toList(array1));
Expand All @@ -47,13 +76,16 @@ public void testToList() {
Assert.assertEquals(ImmutableList.of("1", "2", "3"),
CollectionUtil.toList(array2));

Assert.assertThrows(NullPointerException.class, () -> {
CollectionUtil.toList(null);
});
Set<Integer> set = ImmutableSet.of(1, 2, 3);
Assert.assertEquals(ImmutableList.of(1, 2, 3),
CollectionUtil.toList(set));

Assert.assertThrows(IllegalArgumentException.class, () -> {
CollectionUtil.toList("123");
});
List<Integer> list = ImmutableList.of(1, 2, 3);
Assert.assertEquals(ImmutableList.of(1, 2, 3),
CollectionUtil.toList(list));

Assert.assertEquals(ImmutableList.of("123"),
CollectionUtil.toList("123"));
}

@Test
Expand Down

0 comments on commit 1a66a60

Please sign in to comment.