org.fxmisc.richtext.model.SegmentOps Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
Rich-text area for JavaFX
package org.fxmisc.richtext.model;
import java.util.Optional;
import java.util.function.BiFunction;
import org.reactfx.util.Either;
/**
* Defines the operations which are supported on a specific segment type.
*
* @param The segment type
* @param The style type for the segment
*/
public interface SegmentOps {
public int length(SEG seg);
public char charAt(SEG seg, int index);
public String getText(SEG seg);
public SEG subSequence(SEG seg, int start, int end);
public SEG subSequence(SEG seg, int start);
/**
* Joins two consecutive segments together into one or {@link Optional#empty()} if they cannot be joined.
*/
public Optional joinSeg(SEG currentSeg, SEG nextSeg);
/**
* Joins two consecutive styles together into one or {@link Optional#empty()} if they cannot be joined. By default,
* returns {@link Optional#empty()}.
*/
default Optional joinStyle(S currentStyle, S nextStyle) {
return Optional.empty();
}
/**
* Creates an empty segment. This method should return the same object for better performance and memory usage.
*/
public SEG createEmptySeg();
/**
* Creates a {@link TextOps} specified for a {@link String} segment that never merges consecutive styles
*/
public static TextOps styledTextOps() {
return styledTextOps((s1, s2) -> Optional.empty());
}
/**
* Creates a {@link TextOps} specified for a {@link String}
*/
public static TextOps styledTextOps(BiFunction> mergeStyle) {
return new TextOpsBase("") {
@Override
public char realCharAt(String s, int index) {
return s.charAt(index);
}
@Override
public String realGetText(String s) {
return s;
}
@Override
public String realSubSequence(String s, int start, int end) {
return s.substring(start, end);
}
@Override
public String create(String text) {
return text;
}
@Override
public int length(String s) {
return s.length();
}
@Override
public Optional joinSeg(String currentSeg, String nextSeg) {
return Optional.of(currentSeg + nextSeg);
}
@Override
public Optional joinStyle(S currentStyle, S nextStyle) {
return mergeStyle.apply(currentStyle, nextStyle);
}
};
}
/**
* Returns a {@link SegmentOps} that specifies its segment type to be an {@link Either}
* whose {@link Either#left(Object) left} value is this segment type and
* whose {@link Either#right(Object) right} value is {@code rOps}' segment type.
*/
public default SegmentOps, S> or(SegmentOps rOps) {
return either(this, rOps);
}
/**
* Returns a {@link SegmentOps}
* that specifies its segment type to be an {@link Either}
* whose {@link Either#left(Object) left} value is this segment type and
* whose {@link Either#right(Object) right} value is {@code rOps}' segment type, and
* that specifies its style type to be {@link Either}
* whose {@link Either#left(Object) left} value is this style type and
* whose {@link Either#right(Object) right} value is {@code rOps}' style type.
*/
public default SegmentOps, Either> orStyled(
SegmentOps rOps
) {
return eitherStyles(this, rOps);
}
/**
* Returns a {@link SegmentOps}
* that specifies its segment type to be an {@link Either}
* whose {@link Either#left(Object) left} value is {@code lOps}' segment type and
* whose {@link Either#right(Object) right} value is {@code rOps}' segment type, and
* that specifies its style type to be {@link Either}
* whose {@link Either#left(Object) left} value is {@code lOps}' style type and
* whose {@link Either#right(Object) right} value is {@code rOps}' style type.
*
* Note: consecutive styles will not be merged.
*/
public static SegmentOps, Either> eitherStyles(
SegmentOps lOps,
SegmentOps rOps) {
return new EitherStyledSegmentOps<>(lOps, rOps);
}
/**
* Returns a {@link SegmentOps} that specifies its segment type to be an {@link Either}
* whose {@link Either#left(Object) left} value is {@code lOps}' segment type and
* whose {@link Either#right(Object) right} value is {@code rOps}' segment type.
*
* Note: consecutive styles will not be merged.
*/
public static SegmentOps, Style> either(SegmentOps lOps,
SegmentOps rOps) {
return either(lOps, rOps, (leftStyle, rightStyle) -> Optional.empty());
}
/**
* Returns a {@link SegmentOps} that specifies its segment type to be an {@link Either}
* whose {@link Either#left(Object) left} value is {@code lOps}' segment type and
* whose {@link Either#right(Object) right} value is {@code rOps}' segment type.
*/
public static SegmentOps, Style> either(
SegmentOps lOps, SegmentOps rOps,
BiFunction