(一)Optional
//返回指定引用的Optional实例,若指定引用为null,抛出NullPointerException Optional<Integer> optional1 = Optional.of(5); Optional<Object> optional2 = Optional.of(null); //抛出NullPointerException //返回引用缺失的Optional实例 Optional<Object> optional3 = Optional.absent(); //返回指定引用的Optional实例,引用可以为null Optional<Integer> optional4 = Optional.fromNullable(5); Optional<Object> optional5 = Optional.fromNullable(null); //判断Optional中是否包含非null的引用,如果有返回true boolean present1 = optional1.isPresent(); //true boolean present3 = optional3.isPresent(); //false boolean present4 = optional4.isPresent(); //true boolean present5 = optional5.isPresent(); //false //返回Optional中的引用,若没有非null的引用抛出IllegalStateException Integer get1 = optional1.get(); //5 Object get2 = optional3.get(); //抛出IllegalStateException //返回Optional中的引用,若没有非null的引用则返回指定引用 Integer or1 = optional1.or(6); //5 Object or2 = optional3.or(6); //6 //返回Optional中的引用,若没有非null的引用则返回null Integer orNull1 = optional1.orNull(); //5 Object orNull2 = optional3.orNull(); //null //返回Optional中的引用成一个只有单一元素set,若没有非null的引用则返回空set Set<Integer> integers = optional1.asSet(); Set<Object> objects = optional3.asSet();
(二)Preconditions
//检查一个boolean是否为true,若不是,抛出IllegalArgumentException,并且可以指定异常描述 Preconditions.checkArgument(true); Preconditions.checkArgument(false); //抛出IllegalArgumentException Preconditions.checkArgument(false,"校验失败"); //抛出IllegalArgumentException:校验失败 //检查value是否为null,并且返回value,若value为null抛出IllegalArgumentException,并且可以指定异常描述 Integer integer = Preconditions.checkNotNull(5); //5 Object o1 = Preconditions.checkNotNull(null); //抛出NullPointerException Object o2 = Preconditions.checkNotNull(null, "值为空"); //抛出NullPointerException:值为空 Object o2 = Preconditions.checkNotNull(null, "值为空,应该是%s", 5); //抛出NullPointerException:值为空,应该是5 //检查一个boolean的状态是否为true,若不是,抛出IllegalStateException,并且可以指定异常描述 Preconditions.checkState(true); Preconditions.checkState(false); //抛出IllegalStateException Preconditions.checkState(false,"状态错误"); //抛出IllegalStateException:状态错误 //检查index做为索引是否满足:index >= 0 && index < size,若满足返回index,不满足则抛出IndexOutOfBoundsException,并且可以指定异常描述 int index1 = Preconditions.checkElementIndex(1, 2); //1 int index2 = Preconditions.checkElementIndex(2, 2); //抛出IndexOutOfBoundsException int index3 = Preconditions.checkElementIndex(2, 2,"超出范围"); //抛出IndexOutOfBoundsException: 超出范围 //检查index作为位置是否满足:index >= 0 && index <= size,若满足返回index,不满足则抛出IndexOutOfBoundsException,并且可以指定异常描述 int index4 = Preconditions.checkPositionIndex(2, 2); //2 int index5 = Preconditions.checkPositionIndex(3, 2); //抛出IndexOutOfBoundsException int index6 = Preconditions.checkPositionIndex(3, 2,"超出范围"); //抛出IndexOutOfBoundsException: 超出范围 //检查start end是否满足:start >= 0 && start <= end && end <= size,不满足则抛出IndexOutOfBoundsException Preconditions.checkPositionIndexes(1, 5,5); Preconditions.checkPositionIndexes(1, 6,5); //抛出IndexOutOfBoundsException Preconditions.checkPositionIndexes(-1, 3,5); //抛出IndexOutOfBoundsException
源码:
public static void checkArgument(boolean expression, @Nullable Object errorMessage) { if (!expression) { throw new IllegalArgumentException(String.valueOf(errorMessage)); } } public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) { if (reference == null) { throw new NullPointerException(String.valueOf(errorMessage)); } return reference; } public static void checkState(boolean expression, @Nullable Object errorMessage) { if (!expression) { throw new IllegalStateException(String.valueOf(errorMessage)); } } public static int checkElementIndex(int index, int size, @Nullable String desc) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(badElementIndex(index, size, desc)); } return index; } public static int checkPositionIndex(int index, int size, @Nullable String desc) { // Carefully optimized for execution by hotspot (explanatory comment above) if (index < 0 || index > size) { throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc)); } return index; } public static void checkPositionIndexes(int start, int end, int size) { if (start < 0 || end < start || end > size) { throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size)); } }
(三) Ordering
import com.google.common.collect.Ordering; import com.google.common.primitives.Ints; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class OrderingTest { public static void main(String args[]) { List<Integer> list = new ArrayList<>(); list.add(new Integer(5)); list.add(new Integer(3)); list.add(new Integer(4)); list.add(new Integer(1)); list.add(new Integer(2)); //对可排序类型做自然排序,如数字按大小,日期按先后排序 Ordering ordering = Ordering.natural(); System.out.println(list); //[5, 3, 4, 1, 2] Collections.sort(list, ordering); System.out.println(list); //[1, 2, 3, 4, 5] //判断对象是否按排序器排序,允许有相等元素 System.out.println(ordering.isOrdered(list)); //true //返回最小元素,如果对象中没有元素,则抛出NoSuchElementException System.out.println(ordering.min(list)); //1 //返回最大元素,如果对象中没有元素,则抛出NoSuchElementException System.out.println(ordering.max(list)); //5 //获取可迭代对象中最大的k个元素 System.out.println(ordering.greatestOf(list, 3)); //[5, 4, 3] //获取可迭代对象中最小的k个元素 System.out.println(ordering.leastOf(list, 3)); //[1, 2, 3] //获取相反的排序器 Collections.sort(list, ordering.reverse()); System.out.println(list); //[5, 4, 3, 2, 1] list.add(null); System.out.println(list); //[5, 4, 3, 2, 1, null] //使用当前排序器,但把null排到最前面 Collections.sort(list, ordering.nullsFirst()); System.out.println(list); //[null, 1, 2, 3, 4, 5] List<String> strings = new ArrayList<>(); strings.add("dd"); strings.add("aaa"); strings.add("cccc"); strings.add(null); strings.add("b"); System.out.println(strings); //[dd, aaa, cccc, null, b] //按对象的字符串形式做字典排序 Ordering<Object> usingToStringOrdering = Ordering.usingToString(); Collections.sort(strings, usingToStringOrdering.nullsFirst().reverse()); System.out.println(strings); //[dd, cccc, b, aaa, null] //把给定的Comparator转化为排序器 Ordering<String> fromOrdering = Ordering.from(new Comparator<String>() { @Override public int compare(String s1, String s2) { return Ints.compare(s1.length(), s2.length()); } }); Collections.sort(strings, fromOrdering.nullsFirst().reverse()); System.out.println(strings); //[cccc, aaa, dd, b, null] } }
(四)Objects
import com.google.common.base.MoreObjects; import com.google.common.base.Objects; import com.google.common.collect.ComparisonChain; class Student implements Comparable<Student> { private String name; private int age; private String address; public Student(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } @Override public boolean equals(Object object) { if (this == object) { return true; } if (object == null) { return false; } Student student = (Student) object; return Objects.equal(this.name, student.name) && Objects.equal(this.age, student.age) && Objects.equal(this.address, student.address); } @Override public int hashCode() { return Objects.hashCode(name, age, address); } @Override public String toString() { return MoreObjects.toStringHelper(this).omitNullValues() .add("name", this.name) .add("age", this.age) .add("address", this.address).toString(); } @Override public int compareTo(Student s) { return ComparisonChain.start() .compare(this.name, s.name) .compare(this.age, s.age) .compare(this.address, s.address) .result(); } }