Skip to content

Commit

Permalink
Add CollectionUtil.sortByValue()
Browse files Browse the repository at this point in the history
Change-Id: I9152cc7d8fc5a23e3407118ed09c9b6ac116eed9
  • Loading branch information
Linary committed Dec 25, 2018
1 parent 6f20cb8 commit 4892ced
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jdk:

install: mvn compile -Dmaven.javadoc.skip=true

script: mvn test -Dtest=UnitTestSuite && mvn cobertura:cobertura
script: mvn test -Dtest=UnitTestSuite

after_success:
- bash <(curl -s https://codecov.io/bash)
35 changes: 23 additions & 12 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.5.4</version>
<version>1.5.5</version>

<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
Expand Down Expand Up @@ -198,23 +198,34 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.5.4.0</Implementation-Version>
<Implementation-Version>1.5.5.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check />
</configuration>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
37 changes: 36 additions & 1 deletion src/main/java/com/baidu/hugegraph/util/CollectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class CollectionUtil {
Expand Down Expand Up @@ -65,7 +68,7 @@ public static <T> boolean prefixOf(List<T> prefix, List<T> all) {
return true;
}

public static boolean allUnique(Collection<?> collection){
public static boolean allUnique(Collection<?> collection) {
return collection.stream().allMatch(new HashSet<>()::add);
}

Expand Down Expand Up @@ -163,4 +166,36 @@ public static <T> boolean hasIntersection(Set<T> first, Set<T> second) {
}
return false;
}

public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(
Map<K, V> map, boolean incr) {
List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
if (incr) {
list.sort(Map.Entry.comparingByKey());
} else {
list.sort(Collections.reverseOrder(Map.Entry.comparingByKey()));
}

Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(
Map<K, V> map, boolean incr) {
List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
if (incr) {
list.sort(Map.Entry.comparingByValue());
} else {
list.sort(Collections.reverseOrder(Map.Entry.comparingByValue()));
}

Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
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.5.4");
"1.5.5");
}
24 changes: 0 additions & 24 deletions src/main/resources/log4j.properties

This file was deleted.

2 changes: 2 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.baidu.hugegraph.unit.util.BytesTest;
import com.baidu.hugegraph.unit.util.CollectionUtilTest;
import com.baidu.hugegraph.unit.util.HashUtilTest;
import com.baidu.hugegraph.unit.util.InsertionOrderUtilTest;
import com.baidu.hugegraph.unit.util.VersionUtilTest;

@RunWith(Suite.class)
Expand All @@ -46,6 +47,7 @@
BytesTest.class,
CollectionUtilTest.class,
HashUtilTest.class,
InsertionOrderUtilTest.class,
VersionUtilTest.class
})
public class UnitTestSuite {
Expand Down
155 changes: 154 additions & 1 deletion src/test/java/com/baidu/hugegraph/unit/util/CollectionUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,89 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.unit.BaseUnitTest;
import com.baidu.hugegraph.util.CollectionUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

public class CollectionUtilTest {
public class CollectionUtilTest extends BaseUnitTest {

@Test
public void testToList() {
Object array1 = new Integer[]{1, 2, 3};
Assert.assertEquals(ImmutableList.of(1, 2, 3),
CollectionUtil.toList(array1));

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

Assert.assertThrows(NullPointerException.class, () -> {
CollectionUtil.toList(null);
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
CollectionUtil.toList("123");
});
}

@Test
public void testPrefixOf() {
List<Integer> list = ImmutableList.of(1, 2, 3);

List<Integer> list1 = ImmutableList.of();
Assert.assertTrue(CollectionUtil.prefixOf(list1, list));

List<Integer> list2 = ImmutableList.of(1, 2);
Assert.assertTrue(CollectionUtil.prefixOf(list2, list));

List<Integer> list3 = ImmutableList.of(1, 2, 3);
Assert.assertTrue(CollectionUtil.prefixOf(list3, list));

List<Integer> list4 = ImmutableList.of(1, 2, 3, 4);
Assert.assertFalse(CollectionUtil.prefixOf(list4, list));
}

@Test
public void testAllUnique() {
List<Integer> list = ImmutableList.of();
Assert.assertTrue(CollectionUtil.allUnique(list));

list = ImmutableList.of(1, 2, 3, 2, 3);
Assert.assertFalse(CollectionUtil.allUnique(list));

list = ImmutableList.of(1, 2, 3, 4, 5);
Assert.assertTrue(CollectionUtil.allUnique(list));

list = ImmutableList.of(1, 1, 1, 1, 1);
Assert.assertFalse(CollectionUtil.allUnique(list));
}

@Test
public void testSubSet() {
Set<Integer> originSet = ImmutableSet.of(1, 2, 3, 4, 5);

Set<Integer> subSet = CollectionUtil.subSet(originSet, 1, 1);
Assert.assertEquals(ImmutableSet.of(), subSet);

subSet = CollectionUtil.subSet(originSet, 2, 4);
Assert.assertEquals(ImmutableSet.of(3, 4), subSet);

subSet = CollectionUtil.subSet(originSet, 2, 5);
Assert.assertEquals(ImmutableSet.of(3, 4, 5), subSet);

subSet = CollectionUtil.subSet(originSet, 0, 5);
Assert.assertEquals(ImmutableSet.of(1, 2, 3, 4, 5), subSet);
}

@Test
public void testUnion() {
Expand Down Expand Up @@ -137,4 +210,84 @@ public void testHasIntersectionBetweenSetAndSet() {
second.add(1);
Assert.assertTrue(CollectionUtil.hasIntersection(first, second));
}

@Test
public void testMapSortByStringKey() {
Map<String, Integer> unordered = new HashMap<>();
unordered.put("D", 1);
unordered.put("B", 2);
unordered.put("E", 3);
unordered.put("A", 4);
unordered.put("C", 5);

Map<String, Integer> incrOrdered = CollectionUtil.sortByKey(unordered,
true);
Assert.assertEquals(ImmutableList.of("A", "B", "C", "D", "E"),
ImmutableList.copyOf(incrOrdered.keySet()));

Map<String, Integer> decrOrdered = CollectionUtil.sortByKey(unordered,
false);
Assert.assertEquals(ImmutableList.of("E", "D", "C", "B", "A"),
ImmutableList.copyOf(decrOrdered.keySet()));
}

@Test
public void testMapSortByIntegerKey() {
Map<Integer, String> unordered = new HashMap<>();
unordered.put(4, "A");
unordered.put(2, "B");
unordered.put(5, "C");
unordered.put(1, "D");
unordered.put(3, "E");

Map<Integer, String> incrOrdered = CollectionUtil.sortByKey(unordered,
true);
Assert.assertEquals(ImmutableList.of(1, 2, 3, 4, 5),
ImmutableList.copyOf(incrOrdered.keySet()));

Map<Integer, String> decrOrdered = CollectionUtil.sortByKey(unordered,
false);
Assert.assertEquals(ImmutableList.of(5, 4, 3, 2, 1),
ImmutableList.copyOf(decrOrdered.keySet()));
}

@Test
public void testMapSortByIntegerValue() {
Map<String, Integer> unordered = new HashMap<>();
unordered.put("A", 4);
unordered.put("B", 2);
unordered.put("C", 5);
unordered.put("D", 1);
unordered.put("E", 3);

Map<String, Integer> incrOrdered = CollectionUtil.sortByValue(unordered,
true);
Assert.assertEquals(ImmutableList.of(1, 2, 3, 4, 5),
ImmutableList.copyOf(incrOrdered.values()));

Map<String, Integer> decrOrdered = CollectionUtil.sortByValue(unordered,
false);
Assert.assertEquals(ImmutableList.of(5, 4, 3, 2, 1),
ImmutableList.copyOf(decrOrdered.values()));
}

@Test
public void testMapSortByStringValue() {
Map<Integer, String> unordered = new HashMap<>();
unordered.put(1, "D");
unordered.put(2, "B");
unordered.put(3, "E");
unordered.put(4, "A");
unordered.put(5, "C");

Map<Integer, String> incrOrdered = CollectionUtil.sortByValue(unordered,
true);
Assert.assertEquals(ImmutableList.of("A", "B", "C", "D", "E"),
ImmutableList.copyOf(incrOrdered.values()));

Map<Integer, String> decrOrdered = CollectionUtil.sortByValue(unordered,
false);
Assert.assertEquals(ImmutableList.of("E", "D", "C", "B", "A"),
ImmutableList.copyOf(decrOrdered.values()));
}
}
Loading

0 comments on commit 4892ced

Please sign in to comment.