Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance toList and add toSet in CollectionUtil #25

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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