public class GuavaUtilTest {
public static void main(String[] args) {
cacheDemo();
executorDemo();
}
private static void executorDemo() {
ExecutorService executorService = Executors.newFixedThreadPool(3);
ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
ListenableFuture<Integer> listenableFuture = listeningExecutorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
if (new Random().nextInt(3) == 2) {
throw new NullPointerException();
}
return 1;
}
});
FutureCallback futureCallback = new FutureCallback<Integer>() {
@Override
public void onSuccess(Integer integer) {
System.out.println("----------" + integer);
}
@Override
public void onFailure(Throwable throwable) {
System.out.println("----------" + throwable.getMessage());
}
};
}
private static void cacheDemo() {
CacheLoader<Long, UserData> cacheLoader = new CacheLoader<Long, UserData>() {
@Override
public UserData load(Long aLong) throws Exception {
UserData userData = new UserData();
userData.setId(aLong);
return userData;
}
};
CacheBuilder.newBuilder()
.expireAfterAccess(2, TimeUnit.SECONDS)
.expireAfterWrite(2, TimeUnit.SECONDS)
.maximumSize(10000L)
.build(cacheLoader);
}
private static void checkedDemo(String name, int age, HashMap<Object, Object> map) {
Preconditions.checkNotNull(name, "name must");
Preconditions.checkArgument(age >= 18, "age must");
for (Map.Entry<Object, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
private static void predicateDemo() {
ArrayList<String> list = Lists.newArrayList("helloworld", "yes", "zhangsanfeng");
Collections2.filter(list, new Predicate<String>() {
@Override
public boolean apply(String s) {
return new StringBuilder(s).reverse().toString().equals(s);
}
});
for (String s : list) {
System.out.println(s);
}
}
private static void functionDemo() {
ArrayList<String> list = Lists.newArrayList("helloworld", "yes", "zhangsanfeng");
Function<String, String> f1 = new Function<String, String>() {
@Override
public String apply(String s) {
return s.length() <= 5 ? s : s.substring(0, 5);
}
};
Function<String, String> f2 = new Function<String, String>() {
@Override
public String apply(String s) {
return s.toUpperCase();
}
};
Function<String, String> f3 = Functions.compose(f1, f2);
Collection<String> transform = Collections2.transform(list, f3);
for (String s : transform) {
System.out.println(s);
}
}
private static void collectionDemo() {
List<String> list = Lists.newArrayList();
list.add("a");
list.add("b");
List<String> readList = Collections.unmodifiableList(new ArrayList<>(list));
ImmutableList<String> immutable = ImmutableList.of("a", "b", "c");
ImmutableList<String> immutable2 = ImmutableList.copyOf(list);
list.add("d");
System.out.println("list size:" + list.size() + "immutable2.size:" + immutable2.size());
ImmutableBiMap<String, String> immutableBiMap = ImmutableBiMap.of("name", "zhangsan", "sex", "man");
immutableBiMap.put("wife", "no...");
}
private static void arrayDemo() {
List<Integer> list = Ints.asList(1, 3, 5, 7, 9);
System.out.println(Ints.join("&", 1, 3, 1, 4));
int[] newIntArray = Ints.concat(new int[]{1, 2}, new int[]{2, 3, 4});
System.out.println(newIntArray.length);
System.out.println(Ints.max(newIntArray) + "," + Ints.min(newIntArray));
System.out.println(Ints.contains(newIntArray, 9));
int[] ints = Ints.toArray(list);
}
private static void charMatcherDemo() {
final CharMatcher charMatcherDigit = CharMatcher.digit();
final CharMatcher charMatcherAny = CharMatcher.any();
System.out.println(charMatcherDigit.retainFrom("abc2def134f~"));
System.out.println(charMatcherDigit.removeFrom("yes, i love u 1314"));
System.out.println(charMatcherAny.inRange('a', 'f').or(charMatcherAny.is('n')).replaceFrom("zhangfengzhe", "*"));
}
private static void splitterDemo() {
final Joiner joiner = Joiner.on(",").skipNulls();
final Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings();
String join = joiner.join(Lists.newArrayList("a", null, "b"));
System.out.println("join=" + join);
for (String tmp : splitter.split("a, ,b,,")) {
System.out.println("[" + tmp + "]");
}
}
private static void classToInstanceMapDemo() {
ClassToInstanceMap<Object> instanceMap = MutableClassToInstanceMap.create();
User user = new User("Hydra", 18);
Dept dept = new Dept("develop", 200);
instanceMap.putInstance(User.class, user);
instanceMap.putInstance(Dept.class, dept);
ClassToInstanceMap<Map> instanceMap2 = MutableClassToInstanceMap.create();
HashMap<String, Object> hashMap = new HashMap<>();
TreeMap<String, Object> treeMap = new TreeMap<>();
ArrayList<Object> list = new ArrayList<>();
instanceMap2.putInstance(HashMap.class, hashMap);
instanceMap2.putInstance(TreeMap.class, treeMap);
}
private static void rangeMapDemo() {
TreeRangeMap<Comparable, Object> rangeMap = TreeRangeMap.create();
rangeMap.put(Range.closedOpen(0, 60), "fail");
rangeMap.put(Range.closed(60, 90), "satisfactory");
rangeMap.put(Range.openClosed(90, 100), "excellent");
System.out.println(rangeMap.get(59));
System.out.println(rangeMap.get(60));
System.out.println(rangeMap.get(90));
System.out.println(rangeMap.get(91));
rangeMap.remove(Range.closed(70, 80));
System.out.println(rangeMap.get(75));
}
private static void multimapDemo() {
ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.put("day", 1);
multimap.put("day", 2);
multimap.put("day", 8);
multimap.put("month", 3);
System.out.println(multimap);
Collection<Integer> day = multimap.get("day");
List<Integer> dayList = multimap.get("day");
List<Integer> day3 = multimap.get("day");
List<Integer> year3 = multimap.get("year");
System.out.println(day3);
System.out.println(year3);
ArrayListMultimap<String, Integer> multimap2 = ArrayListMultimap.create();
multimap2.put("day", 1);
multimap2.put("day", 2);
multimap2.put("day", 8);
multimap2.put("month", 3);
List<Integer> day2 = multimap2.get("day");
List<Integer> month2 = multimap2.get("month");
day.remove(0);
month2.add(12);
System.out.println(multimap2);
Map<String, Collection<Integer>> map = multimap.asMap();
for (String key : map.keySet()) {
System.out.println(key + " : " + map.get(key));
}
map.get("day").add(20);
System.out.println(multimap);
System.out.println(multimap.size());
System.out.println(multimap.entries().size());
for (Map.Entry<String, Integer> entry : multimap.entries()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
}
private static void biMapDemo() {
HashBiMap<String, String> biMap = HashBiMap.create();
biMap.put("Hydra", "Programmer");
biMap.put("Tony", "IronMan");
biMap.put("Thanos", "Titan");
System.out.println(biMap.get("Tony"));
BiMap<String, String> inverse = biMap.inverse();
System.out.println(inverse.get("Titan"));
HashBiMap<String, String> biMap2 = HashBiMap.create();
biMap2.put("Hydra", "Programmer");
biMap2.put("Tony", "IronMan");
biMap2.put("Thanos", "Titan");
BiMap<String, String> inverse2 = biMap.inverse();
inverse.put("IronMan", "Stark");
System.out.println(biMap2);
Set<String> values = biMap.values();
HashBiMap<String, String> biMap3 = HashBiMap.create();
biMap3.put("Tony", "IronMan");
biMap3.put("Stark", "IronMan");
biMap3.forcePut("Stark", "IronMan");
}
private static void getTableDemo() {
Table<String, String, Integer> table = HashBasedTable.create();
table.put("Hydra", "Jan", 20);
table.put("Hydra", "Feb", 28);
table.put("Trunks", "Jan", 28);
table.put("Trunks", "Feb", 16);
Integer dayCount = table.get("Hydra", "Feb");
System.out.println(dayCount);
Set<String> rowKeySets = table.rowKeySet();
Set<String> columnKeySets = table.columnKeySet();
Collection<Integer> values = table.values();
System.out.println("rowKeySets:" + rowKeySets);
System.out.println("columnKeySets:" + columnKeySets);
System.out.println("values:" + values);
for (String key : rowKeySets) {
Set<Map.Entry<String, Integer>> rows = table.row(key).entrySet();
int total = 0;
for (Map.Entry<String, Integer> row : rows) {
total += row.getValue();
}
System.out.println(key + ": " + total);
}
Table<String, String, Integer> transpose = Tables.transpose(table);
Set<Table.Cell<String, String, Integer>> cells = transpose.cellSet();
cells.forEach(cell -> {
System.out.println(cell.getRowKey() + "," + cell.getColumnKey() + ":" + cell.getValue());
});
Map<String, Map<String, Integer>> rowMap = table.rowMap();
Map<String, Map<String, Integer>> columnMap = table.columnMap();
}
private static class User {
public User(String hydra, int i) {
}
}
private static class Dept {
public Dept(String develop, int i) {
}
}
@Data
private static class UserData {
private Long id;
}
}