Guava,官方解释:「Google core libraries for Java」,Guava 是一组核心库,包括新的集合类型(如 multimap 和multiset),不可变集合,图形库,函数类型,内存缓存,以及用于并发,I / O,哈希,原始类型数据,反射,字符串处理等 API 。
我按照其 wiki 文档,阅读其源代码,整理出几篇列表,方便使用。这篇是 guava 基本的实用功能以及事件通信组件 EventBus。
关于 null 的处理 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 if (obj == null ) { return ; } if (!expression) { return false ; } checkNotNull(value, "value is null" ); checkArgument(i >= 0 , "Argument was %s but expected nonnegative" , i); checkState(expression); string == null || string.isEmpty(); isNullOrEmpty(String string) nullToEmpty(String string) emptyToNull(String string) List resList = Optional.fromNullable(list).or(new ArrayList<String>()); boolean present = Optional.fromNullable(list).isPresent(); MoreObjects.firstNonNull(list1,list2);
Object Method 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 public class Person implements Comparable <Person > { private String lastName; private String firstName; private int zipCode; @Override public int compareTo (Person other) { int cmp = lastName.compareTo(other.lastName); if (cmp != 0 ) { return cmp; } cmp = firstName.compareTo(other.firstName); if (cmp != 0 ) { return cmp; } return Integer.compare(zipCode, other.zipCode); } } @Override public int compareTo (Person other) { return ComparisonChain.start() .compare(lastName, other.lastName) .compare(firstName, other.firstName) .compare(zipCode, other.zipCode) .result(); } MoreObjects.toStringHelper(this ).add("x" , 1 ).toString(); MoreObjects.toStringHelper("MyObject" ).add("x" , 1 ).toString();
Ordering guava 提供的排序功能,这里更推荐 Java 8 中的 Stream() 和 Comparator 里面提供的比较器来代替。
1 2 3 4 5 list.stream().sorted(); list.stream().sorted(Comparator.reverseOrder()); list.stream().sorted(Comparator.comparing(Student::getAge)); list.stream().sorted(Comparator.comparing(Student::getAge).reversed());
Throwables 个人没理解 guava 的作者们对 Throwables 进行 Propagation 的意义,暂不整理。可参见其官方 wiki :https://github.com/google/guava/wiki/ThrowablesExplained
EventBus
EventBus 在设计上,没有使用单例,允许实例化多个 bus 。
使用 @Subscribe 注解来标记事件订阅的处理方法;并将这些方法缓存;该方法只能有一个参数。
register(obj) 和 post(obj) 参数都是 Object 类型,register 的 obj 需包含带有注解的事件处理方法,以及具体的事件对象参数,post 的 obj 只需要具体的事件对象。We see this as a feature, not a bug :)
如果 register 事件,但没有任何处理函数,那么什么也不会发生;可以使用 DeadEvent,在里面注册一个处理没有订阅的方法,简单打印下没注册的事件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class EventBusChangeRecorder { @Subscribe public void recordCustomerChange (ChangeEvent e) { recordChange(e.getChange()); } } eventBus.register(new EventBusChangeRecorder()); public void changeCustomer () ChangeEvent event = getChangeEvent(); eventBus.post(event); }
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 public static void main (String args[]) { EventBus bus = new EventBus(); bus.register((UserListener) userInfoEvent -> { String userInfo = userInfoEvent.getUserInfo(); System.out.println(userInfo); }); bus.post(new UserInfoEvent("jay" )); bus.post(new UserInfoEvent("kit" )); bus.post(new UserInfoEvent("luck" )); } interface UserListener { @Subscribe void refreshUser (UserInfoEvent userInfo) ; } static class UserInfoEvent { String userInfo; public UserInfoEvent (String userInfo) { this .userInfo = userInfo; } public String getUserInfo () { return userInfo; } }
Title: Guava 1 - Basic Utilities & EventBus
Author: mjd507
Date: 2018-05-12
Last Update: 2024-01-27
Blog Link: https://mjd507.github.io/2018/05/12/Guava-Guide-1/
Copyright Declaration: This station is mainly used to sort out incomprehensible knowledge. I have not fully mastered most of the content. Please refer carefully.