private static void testPrecondition() { try { Preconditions.checkNotNull(null, "neme爲null"); } catch (Exception e) { e.printStackTrace(); } try { String name = "mjx"; Preconditions.checkArgument( name.equals( "blackfox" ), "neme爲" + name ); } catch (Exception e) { e.printStackTrace(); } try { Preconditions.checkState( 3 < 0, "state:" + ( 3 < 0 ) ); } catch (Exception e) { e.printStackTrace(); } // 判斷數組使用 int[] ints = { 1, 2, 3 }; try { Preconditions.checkPositionIndex( 3, ints.length, " 越界" ); } catch (Exception e) { e.printStackTrace(); } try { Preconditions.checkPositionIndexes( 0, 9, ints.length ); } catch (Exception e) { e.printStackTrace(); } try { Preconditions.checkElementIndex( 9, ints.length, "越界了" ); } catch (Exception e) { e.printStackTrace(); } }
private static void testOptional() { Optional<Integer> possible = Optional.of( 6 ); Optional<Integer> absentOpt = Optional.absent(); Optional<Integer> NullableOpt = Optional.fromNullable( null ); Optional<Integer> NoNullableOpt = Optional.fromNullable( 10 ); System.out.println( "possible isPresent:" + possible.isPresent() ); System.out.println( "possible asSet:" + possible.asSet() ); System.out.println( "possible value:" + possible.get() ); System.out.println( "absentOpt isPresent:" + absentOpt.isPresent() ); System.out.println( "fromNullableOpt isPresent:" + NullableOpt.isPresent() ); System.out.println( "NoNullableOpt isPresent:" + NoNullableOpt.isPresent() ); }
參考:Guava學習筆記數組