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

javascalautils.RightProjection Maven / Gradle / Ivy

There is a newer version: 1.11.2
Show newest version
/**
 *  Copyright 2015 Peter Nerg
 *
 *  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 javascalautils;

import static javascalautils.EitherCompanion.Right;

import java.io.Serializable;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * This is a right-biased wrapper for an instance of {@link Either}.
 * 
 * @author Peter Nerg
 * @since 1.1
 */
public class RightProjection extends Projection implements Serializable {
    private static final long serialVersionUID = 4251047373391313192L;
    private final Either either;

    /**
     * Creates an instance of the projection.
     * 
     * @param either
     *            The instance to wrap.
     * @since 1.1
     */
    RightProjection(Either either) {
        this.either = either;
    }

    /**
     * Returns false if {@link Left} or returns the result of applying the predicate to the {@link Right} value.
     * 
     * @param predicate
     *            The predicate to apply
     * @return If this is a {@link Right} and the predicate matches the value
     * @since 1.1
     */
    public boolean exists(Predicate predicate) {
        return either.isRight() ? predicate.test(orNull()) : false;
    }

    /**
     * Returns the value if this is a {@link Right} else the value provided by the supplier.
     * 
     * @param supplier
     *            The supplier
     * @return The value
     * @since 1.1
     */
    @Override
    public R getOrElse(Supplier supplier) {
        return either.fold(v -> supplier.get(), v -> v);
    }

    /**
     * Returns a {@link Some} wrapping the {@link Either} if it's a {@link Right} and the value of the {@link Right} matches the predicate, else {@link None} .
     * 
     * @param predicate
     *            The predicate to apply
     * @return The resulting Option of the filter operation
     * @since 1.1
     */
    public Option> filter(Predicate predicate) {
        return Option.apply(exists(predicate) ? either : null);
    }

    /**
     * Returns true if {@link Left} or returns the result of applying the predicate to the {@link Right} value.
     * 
     * @param predicate
     *            The predicate to apply
     * @return If it is a match
     * @since 1.1
     */
    public boolean forAll(Predicate predicate) {
        return either.isRight() ? predicate.test(orNull()) : true;
    }

    /**
     * If this projection contains a {@link Right} then a new {@link Right} is returned containing the value from the original {@link Right} mapped via the
     * provided function, else the contained Either is returned as is.
     * 
     * @param 
     *            The type to return as the new {@link Right}
     * @param function
     *            The function
     * @return Mapped Either
     * @since 1.1
     */
    @SuppressWarnings("unchecked")
    public  Either map(Function function) {
        return either.isRight() ? Right(function.apply(get())) : (Either) either;
    }

    /**
     * If this projection contains a {@link Right} then a new {@link Right} is returned containing the value from the original {@link Right} mapped via the
     * provided function, else the contained Either is returned as is.
     * 
     * @param 
     *            The type to return as the new {@link Right}
     * @param function
     *            The function
     * @return Mapped Either
     * @since 1.2
     */
    @SuppressWarnings("unchecked")
    public  Either flatMap(Function> function) {
        return either.isRight() ? function.apply(get()) : (Either) either;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy