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

org.fxmisc.richtext.model.TextOps Maven / Gradle / Ivy

There is a newer version: 0.11.3
Show newest version
package org.fxmisc.richtext.model;

import org.reactfx.util.Either;

import java.util.Optional;
import java.util.function.BiFunction;

/**
 * Extends {@link SegmentOps} by adding {@link #create(String)}, which can create a {@link SEG} segment from a given
 * {@link String}
 *
 * @param  the type of segment
 * @param  the type of style
 */
public interface TextOps extends SegmentOps {

    /**
     * Creates a segment using the given text. One could think of this as a mapping function from {@link String} to
     * {@link SEG}
     */
    public SEG create(String text);

    /**
     * Same as {@link SegmentOps#either(SegmentOps, SegmentOps, BiFunction)}, except that
     * {@link TextOps#create(String)} will use this object's {@code create(String)} method, not {@code rOps}' version.
     */
    public default  TextOps, S> _or(SegmentOps rOps, BiFunction> mergeStyle) {
        return eitherL(this, rOps, mergeStyle);
    }

    /**
     * Same as {@link SegmentOps#either(SegmentOps, SegmentOps, BiFunction)}, except that
     * {@link TextOps#create(String)} will use {@code lOps}' {@code create(String)} method, not {@code rOps}' version.
     */
    public static  TextOps, S> eitherL(TextOps lOps, SegmentOps rOps,
                                                             BiFunction> mergeStyle) {
        return new LeftTextOps<>(lOps, rOps, mergeStyle);
    }

    /**
     * Same as {@link SegmentOps#either(SegmentOps, SegmentOps, BiFunction)}, except that
     * {@link TextOps#create(String)} will use {@code rOps}' {@code create(String)} method, not {@code lOps}' version.
     */
    public static  TextOps, S> eitherR(SegmentOps lOps, TextOps rOps,
                                                             BiFunction> mergeStyle) {
        return new RightTextOps<>(lOps, rOps, mergeStyle);
    }

}

class LeftTextOps extends EitherSegmentOps implements TextOps, S> {

    private final TextOps lOps;

    LeftTextOps(TextOps lOps, SegmentOps rOps, BiFunction> mergeStyle) {
        super(lOps, rOps, mergeStyle);
        this.lOps = lOps;
    }

    @Override
    public Either create(String text) {
        return Either.left(lOps.create(text));
    }

}

class RightTextOps extends EitherSegmentOps implements TextOps, S> {

    private final TextOps rOps;

    RightTextOps(SegmentOps lOps, TextOps rOps, BiFunction> mergeStyle) {
        super(lOps, rOps, mergeStyle);
        this.rOps = rOps;
    }

    @Override
    public Either create(String text) {
        return Either.right(rOps.create(text));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy