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 24, 2018
1 parent 6f20cb8 commit 3853113
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 3 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.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,7 +198,7 @@
<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>
Expand Down
19 changes: 19 additions & 0 deletions 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 @@ -163,4 +166,20 @@ public static <T> boolean hasIntersection(Set<T> first, Set<T> second) {
}
return false;
}

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;
}
}
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
115 changes: 114 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,44 @@ public void testHasIntersectionBetweenSetAndSet() {
second.add(1);
Assert.assertTrue(CollectionUtil.hasIntersection(first, second));
}

@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()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.unit.util;

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.InsertionOrderUtil;
import com.google.common.collect.ImmutableList;

public class InsertionOrderUtilTest extends BaseUnitTest {

@Test
public void testSet() {
Set<Integer> set = InsertionOrderUtil.newSet();
set.add(4);
set.add(2);
set.add(5);
set.add(1);
set.add(3);

Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
ImmutableList.copyOf(set));
}

@Test
public void testList() {
List<Integer> list = InsertionOrderUtil.newList();
list.add(4);
list.add(2);
list.add(5);
list.add(1);
list.add(3);

Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
ImmutableList.copyOf(list));
}

@Test
public void testMap() {
Map<Integer, Integer> map = InsertionOrderUtil.newMap();
map.put(4, 4);
map.put(2, 2);
map.put(5, 5);
map.put(1, 1);
map.put(3, 3);

Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
ImmutableList.copyOf(map.keySet()));
}
}

0 comments on commit 3853113

Please sign in to comment.