com.osbolab.commons.MoreOptions.java Source code

Java tutorial

Introduction

Here is the source code for com.osbolab.commons.MoreOptions.java

Source

/*
 * #%L
 * Osbolab Commons
 * %%
 * Copyright (C) 2014 Osbolab LLC
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package com.osbolab.commons;

import com.google.common.base.Strings;
import com.google.common.base.Supplier;

import com.atlassian.fugue.Option;
import com.osbolab.commons.function.Do;
import com.osbolab.commons.function.F;

import java.util.concurrent.locks.Lock;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;

/**
 *
 */
@Immutable
@ParametersAreNonnullByDefault
public final class MoreOptions {

    /**
     * {@code null} is interpreted as None.
     */
    public static <A, B extends A> Option<A> fromNullable(@Nullable B o) {
        return o != null ? Option.<A>some(o) : Option.<A>none();
    }

    /**
     * {@code null} and empty strings are interpreted as None.
     */
    public static Option<String> some(@Nullable String s) {
        return fromNullable(Strings.emptyToNull(s));
    }

    public static <T, R> R doIn(Lock lock, final Supplier<Option<T>> supplier, final F<T, R> present,
            final Supplier<R> notPresent) {

        return Do.in(lock, new Supplier<T>() {
            @Override
            public T get() {
                Option<T> to = supplier.get();
                return to != null ? to.getOrNull() : null;
            }
        }, new F<T, R>() {
            @Override
            public R apply(T t) {
                return present.apply(t);
            }
        }, new Supplier<R>() {
            @Override
            public R get() {
                return notPresent.get();
            }
        });
    }

    public static <T, R> R doInterruptibly(Lock lock, final Supplier<Option<T>> supplier, final F<T, R> present,
            final Supplier<R> notPresent) throws InterruptedException {

        return Do.interruptibly(lock, new Supplier<T>() {
            @Override
            public T get() {
                Option<T> to = supplier.get();
                return to != null ? to.getOrNull() : null;
            }
        }, new F<T, R>() {
            @Override
            public R apply(T t) {
                return present.apply(t);
            }
        }, new Supplier<R>() {
            @Override
            public R get() {
                return notPresent.get();
            }
        });
    }
}