Java - Objects工具类( 二 )

输出结果:
int compare 1:-1int compare 2:1

  • requireNonNull方法
如果object为null , 将会抛出NullPointerException , 否则返回它自身 。
/*** Since 1.7*/public static <T> T requireNonNull(T obj) {if (obj == null)throw new NullPointerException();return obj;}/*** Since 1.7*/public static <T> T requireNonNull(T obj, String message) {if (obj == null)throw new NullPointerException(message);return obj;}/*** Since 1.8*/public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {if (obj == null)throw new NullPointerException(messageSupplier == null ?null : messageSupplier.get());return obj;}例子:
private static void testRequireNotNull() {System.out.println("not null:" + Objects.requireNonNull("ABC"));try {System.out.println("null:" + Objects.requireNonNull(null, "input is required1"));}catch(NullPointerException e){System.out.println("exception:" + e.getMessage());}try {System.out.println("null:" + Objects.requireNonNull(null, ()->"input is required2"));}catch(NullPointerException e){System.out.println("exception:" + e.getMessage());}}输出结果:
not null:ABCexception:input is required1exception:input is required2
  • requireNonNullElse方法
如果object为null , 返回默认值 。
/* since 9 */public static <T> T requireNonNullElse(T obj, T defaultObj) {return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");}/* since 9 */public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) {return (obj != null) ? obj: requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()");}例子:
private static void testRequireNotNullElse() {System.out.println("null else:" + Objects.requireNonNullElse(null,"CEFG"));System.out.println("null else get:" + Objects.requireNonNullElseGet(null,()->"HYZ"));}输出结果:
null else:CEFGnull else get:HYZ
  • isNull和notNull方法
判断是否为null 。
/* since 1.8 */public static boolean isNull(Object obj) {return obj == null;}/* since 1.8 */public static boolean nonNull(Object obj) {return obj != null;}例子:
private static void testNullAndNotNull() {System.out.println("null 1:" + Objects.isNull(null));System.out.println("null 2:" + Objects.nonNull(null));System.out.println("not null 1:" + Objects.isNull("ABC"));System.out.println("not null 2:" + Objects.nonNull("ABC"));}输出结果:
null 1:truenull 2:falsenot null 1:falsenot null 2:true
  • checkIndex方法
检查index是否在[0,length) , 如果是 , 返回index , 否则抛出IndexOutOfBoundsException 。
/* since 9 */public static int checkIndex(int index, int length) {return Preconditions.checkIndex(index, length, null);}例子:
private static void testCheckIndex() {System.out.println("index in:" + Objects.checkIndex(5, 100));try{System.out.println("index in:" + Objects.checkIndex(100, 100));}catch (IndexOutOfBoundsException e){System.out.println("exception:" + e.getMessage());}}输出结果:
index in:5exception:Index 100 out of bounds for length 100
  • checkFromToIndex方法
检查[fromIndex,toIndex) 是否在[0, length)范围内 , 如果是 , 返回fromIndex , 否则抛出IndexOutOfBoundsException异常 。
/* Since 9 */public static int checkFromToIndex(int fromIndex, int toIndex, int length) {return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null);}例子:
private static void testCheckFromToIndex() {System.out.println("index in:" + Objects.checkFromToIndex(5, 10, 100));try{System.out.println("index out:" + Objects.checkFromToIndex(5,101, 100));}catch (IndexOutOfBoundsException e){System.out.println("exception:" + e.getMessage());}}输出结果:
index in:5exception:Range [5, 101) out of bounds for length 100
  • checkFromIndexSize方法
检查[fromIndex,fromIndex+size) 是否在[0, length)范围内 , 如果是 , 返回fromIndex , 否则抛出IndexOutOfBoundsException异常 。
/* Since 9 */public static int c(int fromIndex, int size, int length) {return Preconditions.checkFromIndexSize(fromIndex, size, length, null);}例子:
private static void testCheckFromIndexSize() {System.out.println("index in:" + Objects.checkFromIndexSize(5, 10, 100));try{System.out.println("index out:" + Objects.checkFromIndexSize(5,101, 100));}catch (IndexOutOfBoundsException e){System.out.println("exception:" + e.getMessage());}}


推荐阅读