com.tinspx.util.concurrent.FutureResult.java Source code

Java tutorial

Introduction

Here is the source code for com.tinspx.util.concurrent.FutureResult.java

Source

/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tinspx.util.concurrent;

import com.google.common.base.Optional;
import static com.google.common.base.Preconditions.*;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import javax.annotation.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
 * Simple {@link FutureCallback} that stores the result or error.
 *
 * @param <T> the result type
 * @author Ian
 */
@Getter
@EqualsAndHashCode
@ToString
public class FutureResult<T> implements FutureCallback<T> {

    @SuppressWarnings({ "ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown" })
    public static <T> FutureResult<T> withError() {
        return new FutureResult<T>(null, new Exception());
    }

    @SuppressWarnings({ "ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown" })
    public static <T> FutureResult<T> withError(String msg) {
        return new FutureResult<T>(null, new Exception(msg));
    }

    @SuppressWarnings("ThrowableResultIgnored")
    public static <T> FutureResult<T> withError(Throwable t) {
        return new FutureResult<T>(null, checkNotNull(t));
    }

    public static <T> FutureResult<T> withResult(@Nullable T result) {
        return new FutureResult<T>(result, null);
    }

    public static <T> FutureResult<T> of(ListenableFuture<T> future) {
        return new FutureResult<T>(future);
    }

    @Nullable
    private volatile T result;
    @Nullable
    private volatile Throwable error;
    private volatile boolean complete;

    @SuppressWarnings("LeakingThisInConstructor")
    private FutureResult(ListenableFuture<T> future) {
        Futures.addCallback(future, this, MoreExecutors.directExecutor());
    }

    private FutureResult(@Nullable T result, @Nullable Throwable t) {
        this.result = result;
        this.error = t;
        this.complete = true;
    }

    @Override
    public void onSuccess(T result) {
        this.result = result;
        complete = true;
    }

    @Override
    public void onFailure(Throwable t) {
        this.error = t;
        complete = true;
    }

    public boolean hasResult() {
        return result != null;
    }

    public boolean hasError() {
        return error != null;
    }

    public Optional<String> getStackTrace() {
        return error != null ? Optional.of(Throwables.getStackTraceAsString(error)) : Optional.<String>absent();
    }

    public Optional<T> asOptional() {
        return Optional.fromNullable(result);
    }
}