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

javascalautils.Right 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 java.io.Serializable;
import java.util.function.Function;

import static javascalautils.EitherCompanion.Left;

/**
 * Represents the {@link Right} side of an {@link Either}.
 * 
 * @author Peter Nerg
 * @since 1.1
 * @param 
 *            The type for the {@link Left} side (not used for this class)
 * @param 
 *            The type for the {@link Right} side
 */
public final class Right implements Either, Serializable {

    private static final long serialVersionUID = -2119790544433508346L;
    private final R value;

    /**
     * Creates an instance wrapping the provide value.
     * 
     * @param value
     *            The value wrapped by this instance
     * @since 1.1
     */
    public Right(R value) {
        this.value = value;
    }

    /**
     * Applies the provided func_right to the wrapped value and returns the result.
     * 
     * @since 1.1
     */
    @Override
    public  T fold(Function func_left, Function func_right) {
        return func_right.apply(value);
    }

    /**
     * Always returns true.
     * 
     * @since 1.1
     */
    @Override
    public boolean isRight() {
        return true;
    }

    /**
     * Returns a {@link Left} containing the value for this instance.
     * 
     * @since 1.1
     */
    @Override
    public Either swap() {
        return Left(value);
    }

    /**
     * Returns a String representation of the instance.
     * 
     * @since 1.1
     */
    @Override
    public String toString() {
        return "Right:" + value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy