org.knight.examples.guava.basic.UsingAvoidingNullExamples.java Source code

Java tutorial

Introduction

Here is the source code for org.knight.examples.guava.basic.UsingAvoidingNullExamples.java

Source

/*
 *
 *  * Copyright (C) 2014
 *  *
 *  * 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
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * 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 org.knight.examples.guava.basic;

import com.google.common.base.Optional;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import static org.knight.examples.guava.Utils.*;

/**
 * 1.1 Using/avoiding null examples.
 */
public class UsingAvoidingNullExamples {
    public void run() {
        Optional<String> o1 = Optional.of("Guava-library");
        if (o1.isPresent()) {
            log("o1: " + o1.get());
        } else {
            log("o1: " + o1.toString());
        }

        Optional<String> o2 = Optional.absent();
        try {
            //will cause a IllegalStateException
            log("o2: " + o2.get());
        } catch (IllegalStateException e) {
            log("o2 is absent, use get() will cause a IllegalStateException.");
        }

        Optional<String> o3 = Optional.fromNullable(null);
        log("o3 present = " + o3.isPresent());
        try {
            //will cause a IllegalStateException
            log("o3: " + o3.get());
        } catch (IllegalStateException e) {
            log("o3 is absent, use get() will cause a IllegalStateException.");
        }
        if (o3.orNull() == null) {
            log("o3 is absent, so orNull() returns null.");
        }

        Optional<String> o4 = Optional.fromNullable("Hello World");
        log("o4: " + o4.or("o4 is present, so this default value will not be printed."));

        Optional<Book> o5 = Optional.of(Book.generateBook());
        log("o5: " + o5.get().toString());
    }

    static class Book {
        String isbn;
        double price;
        double discount;
        String author;
        Date comeIntoMarketTime;

        public static Book generateBook() {
            Random r = new Random();
            Book b = new Book();
            b.isbn = randomString(TYPE_NUMBER, 12);
            b.price = r.nextDouble() * 100;
            b.discount = r.nextFloat() * 10;
            b.author = randomString(TYPE_LETTER_LOWER, 6);
            b.comeIntoMarketTime = Calendar.getInstance().getTime();
            return b;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("Book{");
            sb.append("isbn='").append(isbn).append('\'');
            sb.append(", price=").append(price);
            sb.append(", discount=").append(discount);
            sb.append(", author='").append(author).append('\'');
            sb.append(", comeIntoMarketTime=").append(comeIntoMarketTime);
            sb.append('}');
            return sb.toString();
        }
    }
}