-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuavaTest.java
166 lines (143 loc) · 6.26 KB
/
GuavaTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package medium;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import com.google.common.primitives.Ints;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class GuavaTest {
@Test
public void testOptional() throws Exception {
Optional<Integer> optInt = Optional.of(5);
assertEquals(true, optInt.isPresent());
assertEquals(new Integer(5), optInt.get());
}
@Test
public void testStaticOrdering(){
List list = Lists.newArrayList("peida","jerry","harry","eva","jhon","neron");
Ordering naturalOrdering = Ordering.natural();
Ordering usingToStringOrdering = Ordering.usingToString();
assertEquals("[eva, harry, jerry, jhon, neron, peida]", naturalOrdering.sortedCopy(list).toString());
assertTrue(naturalOrdering.isOrdered(naturalOrdering.sortedCopy(list)));
assertFalse(naturalOrdering.reverse().isOrdered(naturalOrdering.sortedCopy(list)));
assertEquals("[eva, harry, jerry, jhon, neron, peida]", usingToStringOrdering.sortedCopy(list).toString());
assertEquals("[eva, harry, jerry]", naturalOrdering.leastOf(list, 3).toString());
assertEquals("[peida, neron, jhon]", naturalOrdering.greatestOf(list, 3).toString());
}
@Test
public void testCreateCustomizeOrdering() throws Exception {
Ordering<String> byLengthOrdering = new Ordering<String>() {
public int compare(String left, String right) {
return Ints.compare(left.length(), right.length());
}
};
List list = Lists.newArrayList("peida","jerry","harry","eva","jhon","neron");
assertEquals("[eva, jhon, peida, jerry, harry, neron]", byLengthOrdering.sortedCopy(list).toString());
assertEquals("[peida, jerry, harry, neron, jhon, eva]", byLengthOrdering.reverse().sortedCopy(list).toString());
Ordering<String> compoundOrdering = byLengthOrdering.compound(Ordering.usingToString());
assertEquals("[eva, jhon, harry, jerry, neron, peida]", compoundOrdering.sortedCopy(list).toString());
}
@Test
public void testOrderingOnResultOf() throws Exception {
Ordering<Foo> ordering = Ordering.natural().onResultOf(new Function<Foo, String>() {
@Override
public String apply( Foo input) {
return input.sortBy;
}
});
List<Foo> foos = Lists.newArrayList(new Foo("b",1), new Foo("c",2), new Foo("a",3));
List<Foo> fooList = ordering.sortedCopy(foos);
assertEquals("a", fooList.get(0).sortBy);
assertEquals("b", fooList.get(1).sortBy);
assertEquals("c", fooList.get(2).sortBy);
}
@Test
public void testObjectsEqualsMethod() throws Exception {
assertTrue(Objects.equal("a", "a"));
assertFalse(Objects.equal(null, "a"));
}
@Test
public void testMultiSet() throws Exception {
Multiset multiset = HashMultiset.create();
String sentences = "this is a story, there is a good girl in the story.";
Iterable<String> words = Splitter.onPattern("[^a-z]{1,}").omitEmptyStrings().trimResults().split(sentences);
for (String word : words) {
multiset.add(word);
}
assertEquals(2, multiset.count("story"));
assertEquals(2, multiset.count("is"));
}
@Test
public void testBiMap() throws Exception {
BiMap<String,String> weekNameMap = HashBiMap.create();
weekNameMap.put("星期一","Monday");
weekNameMap.put("星期二","Tuesday");
weekNameMap.put("星期三","Wednesday");
weekNameMap.put("星期四","Thursday");
weekNameMap.put("星期五","Friday");
weekNameMap.put("星期六","Saturday");
weekNameMap.put("星期日","Sunday");
assertEquals("Sunday",weekNameMap.get("星期日"));
assertEquals("星期日", weekNameMap.inverse().get("Sunday"));
}
@Test
public void testMultiMap() throws Exception {
Multimap<String, String> myMultimap = ArrayListMultimap.create();
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
assertEquals(4, myMultimap.size());
assertEquals(3, myMultimap.get("Fruits").size());
assertEquals(1, myMultimap.get("Vegetables").size());
myMultimap.remove("Fruits","Pear");
assertEquals(2, myMultimap.get("Fruits").size());
myMultimap.removeAll("Fruits");
assertEquals(0, myMultimap.get("Fruits").size());
}
@Test
public void testGuavaTable() throws Exception {
Table<Integer, Integer, String> table = HashBasedTable.create();
table.put(1,2,"a");
table.put(2,3,"b");
table.put(3,4,"c");
assertEquals(3, table.size());
assertEquals("a", table.row(1).get(2));
}
@Test
public void testIterators() throws Exception {
List<String> list = Lists.newArrayList("Apple","Pear","Peach","Banana");
Predicate<String> condition = new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("P");
}
};
boolean allIsStartsWithP = Iterators.all(list.iterator(), condition);
assertFalse(allIsStartsWithP);
assertEquals("Peach", Iterators.get(list.iterator(),2));
assertEquals("Pear", Iterators.filter(list.iterator(),condition).next());
assertEquals("Pear", Iterators.find(list.iterator(),condition));
Iterator<Integer> countIterator = Iterators.transform(list.iterator(), new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return input.length();
}
});
assertEquals(new Integer(5), countIterator.next());
}
}
class Foo {
String sortBy;
int notSortBy;
public Foo(String sortBy, int notSortBy) {
this.sortBy = sortBy;
}
}