Skip to content

Commit

Permalink
使用Guava的Ordering,代替自己写的ComparatorUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Jan 16, 2017
1 parent 5223dc4 commit 2d4b8f2
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
* 通用函数没有针对数据结构进行优化,效率不高,所以没有太多的封装.
*
* 关于List, Map, Queue, Set的特殊工具集,另见特定的Utils
*
* 另JDK中缺少ComparableComparator和NullComparator,直到JDK8才补上。
*
* 因此平时请使用guava的Ordering,fluentable的API更好用,可以设置nullFirst,nullLast,reverse
*
* @see com.google.common.collect.Ordering
*/
@Beta
public abstract class CollectionUtil {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static <T extends Comparable> SortedArrayList<T> createSortedArrayList()
*
* from Jodd的新类型,插入时排序,有几个方法不支持
*/
public static <T> SortedArrayList<T> createSortedArrayList(Comparator<T> c) {
public static <T> SortedArrayList<T> createSortedArrayList(Comparator<? super T> c) {
return new SortedArrayList<T>(c);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public class SortedArrayList<E> extends ArrayList<E> {

private static final long serialVersionUID = -8301136559614447593L;

protected final Comparator<E> comparator;
protected final Comparator<? super E> comparator;

/**
* Constructs a new <code>SortedArrayList</code>.
*/
public SortedArrayList(Comparator<E> c) {
public SortedArrayList(Comparator<? super E> c) {
comparator = c;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class ThreadLocalContext {
private static ThreadLocal<Map<String, Object>> contextMap = new ThreadLocal<Map<String, Object>>() {
@Override
protected Map<String, Object> initialValue() {
return new HashMap<String, Object>();
// 降低loadFactory减少冲突
return new HashMap<String, Object>(16, 0.5f);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.springside.modules.utils.number;

import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

Expand Down Expand Up @@ -217,7 +219,40 @@ public static Long hexToLongObject(String str, Long defaultValue) {
}
}

/////////// 不同精度的数字类型互转 ///////
/////// toString (定义了原子类型与对象类型的参数,保证不会用错函数) ///////

public static String toString(int i) {
return Integer.toString(i);
}

public static String toString(Integer i) {
return i.toString();
}

public static String toString(long l) {
return Long.toString(l);
}

public static String toString(Long l) {
return l.toString();
}

public static String toString(double d) {
return Double.toString(d);
}

public static String toString(Double d) {
return d.toString();
}

/**
* 输出格式化为小数后两位的double字符串
*/
public static String to2DigitString(double d) {
return String.format(Locale.ROOT, "%.2f", d);
}

/////////// 杂项 ///////

/**
* 安全的将小于Integer.MAX的long转为int,否则抛出错误
Expand All @@ -227,4 +262,5 @@ public static int toInt32(long x) throws IllegalArgumentException {
return (int) x;
throw new IllegalArgumentException("Int " + x + " out of range");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.junit.Test;

import com.google.common.collect.Ordering;

public class CollectionUtilTest {

@Test
Expand Down Expand Up @@ -55,15 +57,15 @@ public void minAndMax() {
List<Integer> list = ListUtil.newArrayList(4, 1, 9, 100, 20, 101, 40);

assertThat(CollectionUtil.min(list)).isEqualTo(1);
assertThat(CollectionUtil.min(list,ComparatorUtil.natural())).isEqualTo(1);
assertThat(CollectionUtil.min(list,Ordering.natural())).isEqualTo(1);
assertThat(CollectionUtil.max(list)).isEqualTo(101);
assertThat(CollectionUtil.max(list,ComparatorUtil.natural())).isEqualTo(101);
assertThat(CollectionUtil.max(list,Ordering.natural())).isEqualTo(101);

assertThat(CollectionUtil.minAndMax(list).getLeft()).isEqualTo(1);
assertThat(CollectionUtil.minAndMax(list).getRight()).isEqualTo(101);

assertThat(CollectionUtil.minAndMax(list,ComparatorUtil.natural()).getLeft()).isEqualTo(1);
assertThat(CollectionUtil.minAndMax(list,ComparatorUtil.natural()).getRight()).isEqualTo(101);
assertThat(CollectionUtil.minAndMax(list,Ordering.natural()).getLeft()).isEqualTo(1);
assertThat(CollectionUtil.minAndMax(list,Ordering.natural()).getRight()).isEqualTo(101);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.junit.Test;

import com.google.common.collect.Ordering;

public class ListUtilTest {
@Test
public void guavaBuildList() {
Expand Down Expand Up @@ -106,34 +108,28 @@ public void sortAndSearch() {
ListUtil.shuffle(list, new Random());
System.out.println("shuffle list:" + list);


ListUtil.sort(list, ComparatorUtil.NATUAL);
ListUtil.sort(list, Ordering.natural());

assertThat(list).hasSize(7).containsExactly("a", "b", "c", "d", "e", "g", "i");

assertThat(ListUtil.binarySearch(list, "b")).isEqualTo(1);
assertThat(ListUtil.binarySearch(list, "b", ComparatorUtil.NATUAL)).isEqualTo(1);
assertThat(ListUtil.binarySearch(list, "b", Ordering.natural())).isEqualTo(1);
assertThat(ListUtil.binarySearch(list, "x")).isEqualTo(-8);


//reverse
// reverse
List list8 = ListUtil.reverse(list);
assertThat(list8).hasSize(7).containsExactly("i", "g", "e", "d", "c", "b", "a");



//sortReverse

// sortReverse
ListUtil.shuffle(list8);
ListUtil.sortReverse(list8);
assertThat(list8).hasSize(7).containsExactly("i", "g", "e", "d", "c", "b", "a");

ListUtil.shuffle(list8);
ListUtil.sortReverse(list8,ComparatorUtil.NATUAL);
ListUtil.sortReverse(list8, Ordering.natural());
assertThat(list8).hasSize(7).containsExactly("i", "g", "e", "d", "c", "b", "a");
}



@Test
public void listCompare() {
List<String> list1 = ArrayUtil.asList("d", "a", "c", "b", "e", "i", "g");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.junit.Test;
import org.springside.modules.utils.collection.MapUtil.ValueCreator;

import com.google.common.collect.Ordering;

public class MapUtilTest {

@Test
Expand Down Expand Up @@ -69,7 +71,7 @@ public void guavaBuildMap() {

TreeMap<String, Integer> map6 = MapUtil.newSortedMap();

TreeMap<String, Integer> map7 = MapUtil.newSortedMap(ComparatorUtil.NATUAL);
TreeMap<String, Integer> map7 = MapUtil.newSortedMap(Ordering.natural());

ConcurrentMap map8 = MapUtil.newConcurrentHashMap();
ConcurrentSkipListMap map9 = MapUtil.newConcurrentSortedMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.junit.Test;
import org.springside.modules.utils.collection.type.ConcurrentHashSet;

import com.google.common.collect.Ordering;

public class SetUtilTest {

@Test
Expand All @@ -26,7 +28,7 @@ public void guavaBuildSet() {

TreeSet<String> set5 = SetUtil.newSortedSet();

TreeSet<String> set6 = SetUtil.newSortedSet(ComparatorUtil.NATUAL);
TreeSet<String> set6 = SetUtil.newSortedSet(Ordering.natural());

ConcurrentHashSet set7 = SetUtil.newConcurrentHashSet();
}
Expand Down

This file was deleted.

Loading

0 comments on commit 2d4b8f2

Please sign in to comment.