All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.internal.request.SphereResultRaw Maven / Gradle / Ivy

There is a newer version: 0.72.1
Show newest version
package io.sphere.internal.request;

import com.google.common.base.Function;
import io.sphere.client.exceptions.SphereBackendException;
import io.sphere.internal.util.Validation;
import net.jcip.annotations.Immutable;

import javax.annotation.Nonnull;

/** Raw result of a request to the Sphere backend, used internally by the Java client. */
@Immutable
public final class SphereResultRaw extends Validation {
    private SphereResultRaw(T value, SphereBackendException exception) {
        super(value, exception);
    }

    /** Creates a new successful result. */
    public static  SphereResultRaw success(T value) {
        return new SphereResultRaw(value, null);
    }

    /** Creates a new erroneous result. */
    public static  SphereResultRaw error(SphereBackendException exception) {
        return new SphereResultRaw(null, exception);
    }

    @Override public T getValue() {
        if (!isSuccess()) throw getError();
        return super.getValue();
    }

    /** If successful, transforms the success value. Otherwise does nothing. */
    public  SphereResultRaw transform(@Nonnull Function successFunc) {
        return isSuccess() ?
                SphereResultRaw.success(successFunc.apply(getValue())) :
                SphereResultRaw.error(getError());
    }

    /** If error, creates a new error result of given type (doesn't transform anything, just changes the type).
     *  Otherwise throws an exception */
   public  SphereResultRaw castError() {
        if (isSuccess()) throw new IllegalStateException("Can't call castError on a successful result.");
        return SphereResultRaw.error(this.getError());
   }

   @Override
   public String toString() {
       return this.getClass().getCanonicalName() + "{" + super.toString() + "}";
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy