Java tutorial
/** * Copyright 2016 www.xling123.com * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.shufudong.GuavaExample.collect; import com.google.common.base.Optional; import java.util.Set; /** * @author <a href="mailto:shufudong@gmail.com"></a> * @ClassName OptionalExample * @category Optional * @since 2016-05-12 */ public class OptionalExample { public static void main(String[] args) { System.out.println(OptionalExample.fromNullable(".")); System.out.println(OptionalExample.fromNullable(null)); try { OptionalExample.testOptional2(); } catch (Exception e) { e.printStackTrace(); } OptionalExample.testMethodReturn(); } /** * @param object * @return * @category fromNullable?object?? * ,?fromNullable??? * @throw */ public static Object fromNullable(Object object) { return Optional.fromNullable(object).or("?object,??."); } /** * @return * @category <p>??</p> * <ol> * <li>Optional.of(T)Optional??nullT?T=null</li> * <li>Optional.absent()Optional?</li> * <li>Optional.fromNullable(T)T?OptionalT???[Optional.fromNullable(null)Optional.absent()</li> * </ol> * @throw */ public static void testOptional2() throws Exception { Optional<Integer> possible = Optional.of(6); Optional<Integer> absentOpt = Optional.absent(); Optional<Integer> NullableOpt = Optional.fromNullable(null); Optional<Integer> NoNullableOpt = Optional.fromNullable(10); if (possible.isPresent()) { System.out.println("possible isPresent:" + possible.isPresent()); System.out.println("possible value:" + possible.get()); } if (absentOpt.isPresent()) { System.out.println("absentOpt isPresent:" + absentOpt.isPresent()); } if (NullableOpt.isPresent()) { System.out.println("fromNullableOpt isPresent:" + NullableOpt.isPresent()); } if (NoNullableOpt.isPresent()) { System.out.println("NoNullableOpt isPresent:" + NoNullableOpt.isPresent()); } } /** * @return * @category <p></p> * <ol> * <li> boolean isPresent()Optional?T?nulltrueTnullfalse</li> * <li>T get()Optional?TT???nullOptionalget()IllegalStateException</li> * <li>T or(T)Optional?T?Optional?T?T</li> * <li>T orNull()Optional??TOptional?null?fromNullable()</li> * <li>Set<T> asSet()??SetSet?Optional??TSet?T??Optional?T??Set</li> * </ol> * @throw */ public static void testMethodReturn() { Optional<Long> value = method(); if (value.isPresent() == true) { System.out.println(": " + value.get()); } else { System.out.println(": " + value.or(-12L)); } System.out.println(" orNull: " + value.orNull()); Optional<Long> valueNoNull = methodNoNull(); if (valueNoNull.isPresent() == true) { Set<Long> set = valueNoNull.asSet(); System.out.println(" set size : " + set.size()); System.out.println(": " + valueNoNull.get()); } else { System.out.println(": " + valueNoNull.or(-12L)); } System.out.println(" orNull: " + valueNoNull.orNull()); } private static Optional<Long> method() { return Optional.fromNullable(null); } private static Optional<Long> methodNoNull() { return Optional.fromNullable(15L); } }