package org.fxmisc.richtext.demo.richtext;

import java.util.Optional;

import org.fxmisc.richtext.model.SegmentOps;

public class LinkedImageOps<S> implements SegmentOps<LinkedImage<S>, S> {

    private final EmptyLinkedImage<S> emptySeg = new EmptyLinkedImage<>();

    @Override
    public int length(LinkedImage<S> seg) {
        return seg == emptySeg ? 0 : 1;
    }

    @Override
    public char charAt(LinkedImage<S> seg, int index) {
        return seg == emptySeg ? '\0' : '\ufffc';
    }

    @Override
    public String getText(LinkedImage<S> seg) {
        return seg == emptySeg ? "" : "\ufffc";
    }

    @Override
    public LinkedImage<S> subSequence(LinkedImage<S> seg, int start, int end) {
        if (start < 0) {
            throw new IllegalArgumentException("Start cannot be negative. Start = " + start);
        }
        if (end > length(seg)) {
            throw new IllegalArgumentException("End cannot be greater than segment's length");
        }
        return start == 0 && end == 1
                ? seg
                : emptySeg;
    }

    @Override
    public LinkedImage<S> subSequence(LinkedImage<S> seg, int start) {
        if (start < 0) {
            throw new IllegalArgumentException("Start cannot be negative. Start = " + start);
        }
        return start == 0
                ? seg
                : emptySeg;
    }

    @Override
    public S getStyle(LinkedImage<S> seg) {
        return seg.getStyle();
    }

    @Override
    public LinkedImage<S> setStyle(LinkedImage<S> seg, S style) {
        return seg == emptySeg ? emptySeg : seg.setStyle(style);
    }

    @Override
    public Optional<LinkedImage<S>> join(LinkedImage<S> currentSeg, LinkedImage<S> nextSeg) {
        return Optional.empty();
    }

    @Override
    public LinkedImage<S> createEmpty() {
        return emptySeg;
    }
}

@@ -41,36 +41,8 @@
 t;%0A%0A
-import java.util.Optional;%0A%0A
 impo
@@ -70,16 +70,20 @@
 t.model.
+Node
 SegmentO
@@ -84,16 +84,20 @@
 gmentOps
+Base
 ;%0A%0Apubli
@@ -126,19 +126,20 @@
 %3CS%3E 
-implement
+extend
 s 
+Node
 Segm
@@ -144,16 +144,20 @@
 gmentOps
+Base
 %3CLinkedI
@@ -180,115 +180,14 @@
    p
-rivate final EmptyLinkedImage%3CS%3E emptySeg = new EmptyLinkedImage%3C%3E();%0A%0A    @Override%0A    public int length(
+ublic 
 Link
@@ -185,39 +185,36 @@
 blic LinkedImage
-%3CS%3E seg
+Ops(
 ) %7B%0A        retu
@@ -213,1326 +213,217 @@
     
-return seg == emptySeg ? 0 : 1;%0A    %7D%0A%0A    @Override%0A    public char charAt(LinkedImage%3CS%3E seg, int index) %7B%0A        return seg == emptySeg ? '%5C0' : '%5Cufffc';%0A    %7D%0A%0A    @Override%0A    public String getText(LinkedImage%3CS%3E seg) %7B%0A        return seg == emptySeg ? %22%22 : %22%5Cufffc%22;%0A    %7D%0A%0A    @Override%0A    public LinkedImage%3CS%3E subSequence(LinkedImage%3CS%3E seg, int start, int end) %7B%0A        if (start %3C 0) %7B%0A            throw new IllegalArgumentException(%22Start cannot be negative. Start = %22 + start);%0A        %7D%0A        if (end %3E length(seg)) %7B%0A            throw new IllegalArgumentException(%22End cannot be greater than segment's length%22);%0A        %7D%0A        return start == 0 && end == 1%0A                ? seg%0A                : emptySeg;%0A    %7D%0A%0A    @Override%0A    public LinkedImage%3CS%3E subSequence(LinkedImage%3CS%3E seg, int start) %7B%0A        if (start %3C 0) %7B%0A            throw new IllegalArgumentException(%22Start cannot be negative. Start = %22 + start);%0A        %7D%0A        return start == 0%0A                ? seg%0A                : emptySeg;%0A    %7D%0A%0A    @Override%0A    public S getStyle(LinkedImage%3CS%3E seg) %7B%0A        return seg.getStyle();%0A    %7D%0A%0A    @Override%0A    public LinkedImage%3CS%3E setStyle(LinkedImage%3CS%3E seg, S style) %7B%0A        return seg == emptySeg ? emptySeg : seg.setStyle(style);%0A    %7D%0A%0A    @Override%0A    public Optional%3C
+super(new EmptyLinkedImage%3C%3E());%0A    %7D%0A%0A    @Override%0A    public S realGetStyle(LinkedImage%3CS%3E linkedImage) %7B%0A        return linkedImage.getStyle();%0A    %7D%0A%0A    @Override%0A    public LinkedImage%3CS%3E realSetStyle(
 Link
@@ -436,16 +436,10 @@
 e%3CS%3E
-%3E join(L
+ l
 inke
@@ -448,175 +448,69 @@
 mage
-%3CS%3E currentSeg, LinkedImage%3CS%3E nextSeg) %7B%0A        return Optional.empty();%0A    %7D%0A%0A    @Override%0A    public LinkedImage%3CS%3E createEmpty() %7B%0A        return emptySeg
+, S style) %7B%0A        return linkedImage.setStyle(style)
 ;%0A    %7D%0A
 %7D%0A
@@ -507,10 +507,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Extend base class to reduce boilerplate code