@SuppressWarnings("NonSynchronizedMethodOverridesSynchronizedMethod")
public abstract class BytesInputStream
    extends ByteArrayInputStream {

    protected BytesInputStream() { super(new byte[0]); }

    public static BytesInputStream create(final byte[] bin) {
        return CommonsIOImpl.isAvailable() ? new CommonsIOImpl(bin) : new JDKImpl(bin);
    }

    public static InputStream create(final ByteArrayOutputStream baos) {
        return (baos instanceof BytesOutputStream)
            ? ((BytesOutputStream) baos).toInputStream()
            : create(baos.toByteArray());
    }

    private static class JDKImpl
        extends BytesInputStream {

        private final ByteArrayInputStream bais;

        public JDKImpl(final byte[] bin) { this.bais = new ByteArrayInputStream(bin); }

        @Override
        public int available() { return this.bais.available(); }

        @Override
        public int read() { return this.bais.read(); }

        @Override
        public int read(final byte[] b) { return eval(() -> this.bais.read(b)); }

        @Override
        public int read(final byte[] b, final int off, final int len) { return this.bais.read(b, off, len); }

        @Override
        public long skip(final long n) { return this.bais.skip(n); }

        @Override
        public boolean markSupported() { return this.bais.markSupported(); }

        @Override
        public void mark(final int readlimit) { this.bais.mark(readlimit); }

        @Override
        public void reset() { this.bais.reset(); }

        @Override
        public byte[] readAllBytes() { return this.bais.readAllBytes(); }

        @Override
        public byte[] readNBytes(final int len) {
            try { return this.bais.readNBytes(len); } //
            catch (final IOException e) { throw new RuntimeException(e); }
        }

        @Override
        public int readNBytes(final byte[] b, final int off, final int len) {
            return this.bais.readNBytes(b, off, len);
        }

        @Override
        public long transferTo(final OutputStream out) throws IOException { return this.bais.transferTo(out); }

        @Override
        public void close() {}
    }

    private static class CommonsIOImpl
        extends BytesInputStream {

        private static final boolean avail =
            ObjFactory.isPresent("org.apache.commons.io.output.UnsynchronizedByteArrayInputStream");

        public static boolean isAvailable() { return avail; }

        private final UnsynchronizedByteArrayInputStream bais;

        public CommonsIOImpl(final byte[] bin) { this.bais = new UnsynchronizedByteArrayInputStream(bin); }

        @Override
        public int available() { return this.bais.available(); }

        @Override
        public int read() { return this.bais.read(); }

        @Override
        public int read(final byte[] b) { return this.bais.read(b); }

        @Override
        public int read(final byte[] b, final int off, final int len) { return this.bais.read(b, off, len); }

        @Override
        public long skip(final long n) { return this.bais.skip(n); }

        @Override
        public boolean markSupported() { return this.bais.markSupported(); }

        @Override
        public void mark(final int readlimit) { this.bais.mark(readlimit); }

        @Override
        public void reset() { this.bais.reset(); }

        @Override
        public byte[] readAllBytes() {
            try { return this.bais.readAllBytes(); } //
            catch (final IOException e) { throw new RuntimeException(e); }
        }

        @Override
        public byte[] readNBytes(final int len) {
            try { return this.bais.readNBytes(len); } //
            catch (final IOException e) { throw new RuntimeException(e); }
        }

        @Override
        public int readNBytes(final byte[] b, final int off, final int len) {
            try { return this.bais.readNBytes(b, off, len); } //
            catch (final IOException e) { throw new RuntimeException(e); }
        }

        @Override
        public long transferTo(final OutputStream out) throws IOException { return this.bais.transferTo(out); }

        @Override
        public void close() {}
    }
} public abstract class BytesOutputStream
    extends ByteArrayOutputStream {

    final OutputStream sink;

    protected BytesOutputStream(final OutputStream sink) { this.sink = sink; }

    @Override
    public void write(final byte[] b, final int off, final int len) { let(() -> this.sink.write(b, off, len)); }

    @Override
    public void write(final int b) { let(() -> this.sink.write(b)); }

    @Override
    public void close() { let(() -> this.sink.close()); }

    @Override
    public void write(final byte[] b) { let(() -> this.sink.write(b)); }

    @Override
    public void flush() { let(() -> this.sink.flush()); }

    @Override
    @Deprecated
    public String toString() { return this.sink.toString(); }

    public abstract int write(final InputStream in) throws IOException;

    public abstract InputStream toInputStream();

    public static BytesOutputStream create() {
        return CommonsIOImpl.isAvailable() ? new CommonsIOImpl() : new JDKImpl();
    }

    public static BytesOutputStream create(final int len) {
        return CommonsIOImpl.isAvailable() ? new CommonsIOImpl(len) : new JDKImpl(len);
    }

    @SuppressWarnings("NonSynchronizedMethodOverridesSynchronizedMethod")
    private static class JDKImpl
        extends BytesOutputStream {

        private final ByteArrayOutputStream baos;

        public JDKImpl() {
            super(new ByteArrayOutputStream());
            this.baos = cast(this.sink);
        }

        public JDKImpl(final int len) {
            super(new ByteArrayOutputStream(len));
            this.baos = cast(this.sink);
        }

        private byte[] buf;

        @Override
        public int write(final InputStream in) throws IOException { return (int) in.transferTo(this); }

        @Override
        public void writeTo(final OutputStream out) throws IOException { this.baos.writeTo(out); }

        @Override
        public int size() { return this.baos.size(); }

        @Override
        public InputStream toInputStream() { return new ByteArrayInputStream(this.toByteArray()); }

        @Override
        public byte[] toByteArray() { return this.baos.toByteArray(); }

        @Override
        public void reset() { this.baos.reset(); }

        @Override
        public String toString(final String enc) throws UnsupportedEncodingException { return this.baos.toString(enc); }

        @Override
        public String toString(final Charset charset) { return this.baos.toString(charset); }
    }

    @SuppressWarnings("NonSynchronizedMethodOverridesSynchronizedMethod")
    private static class CommonsIOImpl
        extends BytesOutputStream {

        private static final boolean avail =
            ObjFactory.isPresent("org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream");

        public static boolean isAvailable() { return avail; }

        private final UnsynchronizedByteArrayOutputStream baos;

        public CommonsIOImpl() {
            super(new UnsynchronizedByteArrayOutputStream());
            this.baos = cast(this.sink);
        }

        public CommonsIOImpl(final int len) {
            super(new UnsynchronizedByteArrayOutputStream(len));
            this.baos = cast(this.sink);
        }

        @Override
        public int write(final InputStream in) throws IOException { return this.baos.write(in); }

        @Override
        public void writeTo(final OutputStream out) throws IOException { this.baos.writeTo(out); }

        @Override
        public int size() { return this.baos.size(); }

        @Override
        public void reset() { this.baos.reset(); }

        @Override
        public InputStream toInputStream() { return this.baos.toInputStream(); }

        @Override
        public byte[] toByteArray() { return this.baos.toByteArray(); }

        @Override
        public String toString(final String enc) throws UnsupportedEncodingException { return this.baos.toString(enc); }

        @Override
        public String toString(final Charset charset) { return this.baos.toString(charset); }
    }
} public abstract class ObjFactory {

    private static volatile ObjFactory instance;

    static { init(); }

    private static void init() {

        try {
            instance = new EclipseObjFactory();
            return;
        } catch (final UnsupportedOperationException ignored) {}

        try {
            instance = new FastutilObjFactory();
            return;
        } catch (final UnsupportedOperationException ignored) {}

        try {
            instance = new KolobokeObjFactory();
            return;
        } catch (final UnsupportedOperationException ignored) {}

        instance = new JdkObjFactory();
    }

    public static boolean isPresent(final String fqcn) {
        //noinspection UnusedCatchParameter
        try {
            return Class.forName(fqcn) != null;
        } catch (final ClassNotFoundException e) {
            return false;
        }
    }

    /**
     * Get default instance.
     *
     * @return default instance
     */
    public static ObjFactory getInstance() {
        return instance;
    }

    /**
     * Set default instance.
     *
     * @param ofactory default instance
     */
    public static void setInstance(final ObjFactory ofactory) {
        instance = Objects.requireNonNull(ofactory);
    }

    /**
     * Create new map instance.
     *
     * @return new map instance
     */
    public <K extends Comparable<K>, V> SortedMap<K, V> newSortedMap() {
        return this.newSortedMap(Comparator.naturalOrder());
    }

    /**
     * Create new map instance.
     *
     * @param comp comparator
     * @return new map instance
     */
    public abstract <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp);

    /**
     * Create new set instance.
     *
     * @return new set instance
     */
    public <V extends Comparable<V>> SortedSet<V> newSortedSet() {
        return this.newSortedSet(Comparator.naturalOrder());
    }

    /**
     * Create new set instance.
     *
     * @param comp comparator
     * @return new set instance
     */
    public abstract <V> SortedSet<V> newSortedSet(Comparator<? super V> comp);

    /**
     * Create new map instance.
     *
     * @return new map instance
     */
    public abstract <K, V> Map<K, V> newFifoMap();

    /**
     * Create new set instance.
     *
     * @return new set instance
     */
    public abstract <V> Set<V> newFifoSet();

    /**
     * Create new map instance.
     *
     * @return new map instance
     */
    public abstract <K, V> Map<K, V> newMap();

    /**
     * Create new set instance.
     *
     * @return new set instance
     */
    public abstract <V> Set<V> newSet();

    /**
     * Create new list instance.
     *
     * @return new list instance
     */
    public abstract <V> List<V> newList();
} public interface Promise<T> {

    T resolve();

    $$<T, None> resolve(long timeout);

    boolean cancel();

    boolean cancelled();

    boolean done();

    boolean failed();

    <U> Promise<U> thenAsync(Function<? super T, ? extends U> f);

    default <U> Promise<U> then(final Function<? super T, ? extends U> f) { return this.thenAsync(f); }

    default Promise<T> then(final Consumer<? super T> f) {
        return this.then(x -> {
            f.accept(x);
            return x;
        });
    }

    default Promise<T> then(final Runnable f) {
        return this.then(x -> {
            f.run();
            return x;
        });
    }

    Promise<T> fail(Function<? super Exception, ? extends T> f);

    <U> Promise<U> last(BiFunction<? super T, Exception, ? extends U> f);

    default Promise<T> last(final BiConsumer<? super T, Exception> f) {
        return this.last((x, e) -> {
            f.accept(x, e);
            return x;
        });
    }

    static <T> Promise<T> resolve(final T val) { return of(CompletableFuture.completedFuture(val)); }

    static <T> Promise<T> reject(final Exception e) { return of(CompletableFuture.failedFuture(requireNonNull(e))); }

    static <T> Promise<List<T>> all(final Iterable<? extends Promise<? extends T>> promises) {
        return of(CompletableFuture.supplyAsync(() -> list(promises).map(x -> x.resolve())));
    }

    @SuppressWarnings("ConstantConditions")
    static <T> Promise<T> any(final Iterable<? extends Promise<? extends T>> promises) {
        return cast(of(CompletableFuture.anyOf(cast(promises))));
    }

    static <T> Promise<T> of(final CompletableFuture<T> f) { return new PromiseCFuture<>(f); }

    static <T> Promise<T> none() { return resolve(null); }
}
 public class Promissory {

    /** non private for subtyping. */
    protected Promissory() { }

    private static Executor executor() { return ForkJoinPool.commonPool(); }

    public static Promise<Void> async(final RunnableE<? super Exception> run) { return async(run, executor()); }

    public static Promise<Void> async(final RunnableE<? super Exception> run, final Executor exec) {
        return async(() -> {
            run.run();
            return null;
        }, exec);
    }

    public static <T> Promise<T> async(final Callable<? extends T> run) { return async(run, executor()); }

    public static <T> Promise<T> async(final Callable<? extends T> run, final Executor exec) {
        return new PromiseCFuture<T>(CompletableFuture.supplyAsync(() -> {
            try { return run.call(); } //
            catch (final Exception e) { return raise(e); }
        }, exec));
    }

    public static <T> T await(final Promise<? extends T> promise) { return promise.resolve(); }

    public static <T> $$<T, $$.None> await(final Promise<? extends T> promise, final long timeout) {
        return cast(promise.resolve(timeout));
    }

    public static <T> T await(final Future<? extends T> promise) {
        try { return promise.get(); } //
        catch (final InterruptedException e) { return raise(e); } //
        catch (final ExecutionException e) { return raise(e.getCause()); }
    }

    public static <T> $$<T, $$.None> await(final Future<? extends T> promise, final long timeout) {
        try { return left(promise.get(timeout, TimeUnit.MILLISECONDS)); } //
        catch (final TimeoutException e) { return right(NONE); } //
        catch (final InterruptedException e) { return raise(e); } //
        catch (final ExecutionException e) { return raise(e.getCause()); }
    }
} @FunctionalInterface
public interface RunnableE<E extends Exception> {

    default Runnable as() { return () -> let(() -> this.run()); }

    void run() throws E;
} @FunctionalInterface
public interface Statement
    extends Runnable {

    Statement NOP = () -> {};

    /**
     * Perform this operation.
     *
     * @throws Exception any exception which this statement would throw
     */
    void exec() throws Exception;

    /**
     * Perform this operation.
     */
    @Override
    default void run() {
        try {
            this.exec();
        } catch (final Exception e) {
            Expressive.raise(e);
        }
    }

    /**
     * Returns composed statement which execute this statement then execute {@code after}.
     *
     * @param after statement which execute after this statement
     * @return composed statement
     */
    default Statement andThen(final Statement after) {

        Objects.requireNonNull(after);

        return () -> {
            this.run();
            after.run();
        };
    }
} @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public final class $<T>
    implements Serializable, Supplier<T> {

    @SuppressWarnings("PublicField")
    public final Optional<T> opt; // NOPMD

    public static <T> $<T> of(final T val) { return (val == null) ? none() : new $<>(val); }

    @SuppressWarnings("OptionalAssignedToNull")
    public static <T> $<T> of(final Optional<? extends T> val) {
        return (val == null) || val.isEmpty() ? none() : new $<>(Indolently.cast(val));
    }

    private static final $<?> NONE = new $<>(Optional.empty());

    public static <T> $<T> none() { return Indolently.cast(NONE); }

    private $(final T val) { this(Optional.ofNullable(val)); }

    private $(final Optional<T> opt) { this.opt = requireNonNull(opt); }

    @SuppressWarnings("OptionalGetWithoutIsPresent")
    @Override
    public T get() { return this.opt.get(); }

    public boolean empty() { return this == NONE || this.opt.isEmpty(); }

    public boolean present() { return !this.empty(); }

    public $<T> when(final Predicate<? super T> f) {
        return this.empty() ? none() : this.opt.filter(f).map($::of).orElse(none());
    }

    // alias
    public $<T> filter(final Predicate<? super T> f) { return this.when(f); }

    public $<T> tap(final Consumer<? super T> f) { return this.tapTry(f::accept); }

    public $<T> tap(final Consumer<? super T> action, final Runnable orAction) {
        return this.tapTry(action::accept, orAction::run);
    }

    // alias
    public $<T> then(final Consumer<? super T> f) { return this.tap(f); }

    // alias
    public $<T> then(final Consumer<? super T> action, final Runnable orAction) { return this.tap(action, orAction); }

    private static final $<Boolean> T = of(true);

    private static final $<Boolean> F = of(false);

    public $<Boolean> exam(final Predicate<? super T> f) { return this.empty() ? none() : this.test(f) ? T : F; }

    public boolean test(final Predicate<? super T> f) { return !this.empty() && this.when(f).present(); }

    public <U> $<U> cast(final Class<U> type) { return this.when(type::isInstance).map(type::cast); }

    public <U> $<U> map(final Function<? super T, ? extends U> f) { return this.mapTry(f::apply); }

    public <U> $<U> fmap(final Function<? super T, ? extends $<? extends U>> f) { return this.fmapTry(f::apply); }

    public $<T> fold(final Function<? super T, ? extends $<? extends T>> f) { return this.foldTry(f::apply); }

    public <S> $<$2<T, S>> and$(final Supplier<? extends $<? extends S>> f) { return this.and$Try(f::get); }

    public <S> $<$2<T, S>> and$(final $<? extends S> and) { return this.and$(() -> and); }

    public $<T> or$(final Supplier<? extends $<? extends T>> f) { return this.or$Try(f::get); }

    public $<T> or$(final $<? extends T> or) { return this.present() ? this : Indolently.cast(or); }

    public Stream<T> stream() { return this.opt.stream(); }

    public T or(final T or) { return this.orElse(or); }

    public T orElse(final T or) { return this.opt.orElse(or); }

    public T orNull() { return this.orElse(null); }

    public T or(final Supplier<? extends T> f) { return this.orElseGet(f); }

    public T orElseGet(final Supplier<? extends T> f) { return this.opt.orElseGet(f); }

    public <E extends Exception> T orTry(final SupplierE<? extends T, E> f) throws E { return this.orElseTry(f); }

    public T orFail() { return this.opt.orElseThrow(); }

    public <X extends Throwable> T orFail(final Supplier<? extends X> f) throws X { return this.opt.orElseThrow(f); }

    @SuppressWarnings("OptionalGetWithoutIsPresent")
    public <U, E extends Exception> $<T> tapTry(final ConsumerE<? super T, E> f) throws E {
        if (this.empty()) return none();
        f.accept(this.opt.get());
        return this;
    }

    @SuppressWarnings("OptionalGetWithoutIsPresent")
    public <U, E extends Exception> $<T> tapTry(final ConsumerE<? super T, E> f, final RunnableE<E> orAction) throws E {
        if (this.empty()) {
            orAction.run();
            return none();
        } else {
            f.accept(this.opt.get());
            return this;
        }
    }

    @SuppressWarnings("OptionalGetWithoutIsPresent")
    public <U, E extends Exception> $<U> mapTry(final FunctionE<? super T, ? extends U, E> f) throws E {
        return this.empty() ? none() : of(f.apply(this.opt.get()));
    }

    @SuppressWarnings("OptionalGetWithoutIsPresent")
    public <U, E extends Exception> $<U> fmapTry(final FunctionE<? super T, ? extends $<? extends U>, E> f) throws E {
        if (this.empty()) return none();
        final var x = f.apply(this.opt.get());
        return (x == null) ? none() : Indolently.cast(x);
    }

    public <E extends Exception> $<T> foldTry(final FunctionE<? super T, ? extends $<? extends T>, E> f) throws E {
        final var ret = this.fmapTry(f::apply);
        return ret.empty() ? this : Indolently.cast(ret);
    }

    public <E extends Exception> $<T> or$Try(final SupplierE<? extends $<? extends T>, E> f) throws E {
        if (this.present()) return this;
        final var x = f.get();
        return (x == null) ? none() : of(x.opt);
    }

    public <S, E extends Exception> $<$2<T, S>> and$Try(final SupplierE<? extends $<? extends S>, E> f) throws E {
        if (this.empty()) return none();
        final var x = f.get();
        return (x == null) ? none() : x.map(y -> tuple(this.get(), y));
    }

    @SuppressWarnings("OptionalGetWithoutIsPresent")
    public <E extends Exception> T orElseTry(final SupplierE<? extends T, E> f) throws E {
        return this.present() ? this.opt.get() : f.get();
    }

    @Override
    public int hashCode() { return Objects.hash(this.getClass(), this.opt); }

    public boolean eq(final T that) { return this.test(x -> x.equals(that)); }

    public boolean equals(final $<? extends T> that) { return this.equals0(that); }

    @Deprecated
    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof $)) return false;

        return this.equals0(Indolently.cast(o));
    }

    private boolean equals0(final $<? extends T> that) {
        return (this == that) || ((that != null) && equiv(this.opt, that.opt));
    }

    @Override
    public String toString() { return this.present() ? "$(" + this.get() + ")" : "$<empty>"; }
} public final class $$<L, R> {

    public enum None { NONE }

    private final $<L> l;

    private final $<R> r;

    private $$(final L l, final R r) {
        assert (l == null) ^ (r == null);
        this.l = $.of(l);
        this.r = $.of(r);
    }

    public static <L, R> $$<L, R> left(final L l) { return new $$<>(l, null); }

    public static <L, R> $$<L, R> right(final R r) { return new $$<>(null, r); }

    public $<L> l() { return this.l; }

    public $<R> r() { return this.r; }

    public boolean isL() { return this.l.present(); }

    public boolean isR() { return this.r.present(); }

    public <T> T map(final Function<L, ? extends T> lfn, final Function<R, ? extends T> rfn) {
        return this.l.map(lfn).or(() -> cast(this.r.map(rfn).get()));
    }

    public $$<L, R> tap(final Consumer<L> lfn, final Consumer<R> rfn) {
        this.l.tap(lfn);
        this.r.tap(rfn);
        return this;
    }

    public <T> $<T> lmap(final Function<L, T> fn) { return this.l.map(fn); }

    public <T> $<T> rmap(final Function<R, T> fn) { return this.r.map(fn); }

    @Override
    public String toString() { return String.format("Either[%s,%s]", tostr(this.l), tostr(this.r)); }

    private static Object tostr(final $<?> x) { return x.empty() ? "_" : x.get(); }
} public class $2<T1, T2>
    implements Serializable, BiConsumer<T1, T2>, Supplier<$2<T1, T2>>, Consumer<$2<T1, T2>> {

    private static final long serialVersionUID = 4058877644750960140L;

    /** first element */
    @SuppressWarnings("PublicField")
    public T1 _1; // NOPMD

    /** second element */
    @SuppressWarnings("PublicField")
    public T2 _2; // NOPMD

    @Destructive
    @Override
    public void accept(final $2<T1, T2> that) {
        this._1 = that._1;
        this._2 = that._2;
    }

    /**
     * set elements then return {@code this} instance.
     *
     * @param that the element supplier
     * @return {@code this} instance
     */
    @Destructive
    public $2<T1, T2> set(final $2<? extends T1, ? extends T2> that) {
        return (that == null) ? this.set(null, null) : this.set(that._1, that._2);
    }

    /**
     * set elements then return {@code this} instance.
     *
     * @param _1 first element
     * @param _2 second element
     * @return {@code this} instance
     */
    @Destructive
    public $2<T1, T2> set(final T1 _1, final T2 _2) {
        this.accept(_1, _2);
        return this;
    }

    @Override
    public $2<T1, T2> get() { return this; }

    @Destructive
    @Override
    public void accept(final T1 _1, final T2 _2) {
        this._1 = _1;
        this._2 = _2;
    }

    /**
     * get 1st element
     *
     * @return 1st element
     */
    public T1 _1() { return this._1; }

    /**
     * set 1st element
     *
     * @param _1 1st element
     * @return {@code this}
     */
    @Destructive
    public $2<T1, T2> _1(final T1 _1) {
        this._1 = _1;
        return this;
    }

    /**
     * get 2nd element
     *
     * @return 2nd element
     */
    public T2 _2() { return this._2; }

    /**
     * set 2nd element
     *
     * @param _2 2st element
     * @return {@code this}
     */
    @Destructive
    public $2<T1, T2> _2(final T2 _2) {
        this._2 = _2;
        return this;
    }

    /**
     * create order reversed tuple.
     *
     * @return newly constructed reversed tuple
     */
    public $2<T2, T1> _21() { return tuple(this._2, this._1); }

    @Override
    public int hashCode() { return Objects.hash(this.getClass(), this._1, this._2); }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof $2)) return false;

        final $2<?, ?> that = cast(o);

        return equiv(this._1, that._1) && equiv(this._2, that._2);
    }

    @Override
    public String toString() { return String.format("(%s, %s)", this._1, this._2); }
} public class $3<T1, T2, T3>
    implements Serializable, Consumer3<T1, T2, T3>, Supplier<$3<T1, T2, T3>>, Consumer<$3<T1, T2, T3>> {

    private static final long serialVersionUID = 1387913510813532191L;

    /** first element */
    @SuppressWarnings("PublicField")
    public T1 _1; // NOPMD

    /** second element */
    @SuppressWarnings("PublicField")
    public T2 _2; // NOPMD

    /** third element */
    @SuppressWarnings("PublicField")
    public T3 _3; // NOPMD

    @Destructive
    @Override
    public void accept(final $3<T1, T2, T3> that) {
        this._1 = that._1;
        this._2 = that._2;
        this._3 = that._3;
    }

    /**
     * set elements then return {@code this} instance.
     *
     * @param that the element supplier
     * @return {@code this} instance
     */
    @Destructive
    public $3<T1, T2, T3> set(final $3<? extends T1, ? extends T2, ? extends T3> that) {
        return (that == null) ? this.set(null, null, null) : this.set(that._1, that._2, that._3);
    }

    /**
     * set elements then return {@code this} instance.
     *
     * @param _1 first element
     * @param _2 second element
     * @param _3 third element
     * @return {@code this} instance
     */
    @Destructive
    public $3<T1, T2, T3> set(final T1 _1, final T2 _2, final T3 _3) {
        this.accept(_1, _2, _3);
        return this;
    }

    @Override
    public $3<T1, T2, T3> get() { return this;}

    @Destructive
    @Override
    public void accept(final T1 _1, final T2 _2, final T3 _3) {
        this._1 = _1;
        this._2 = _2;
        this._3 = _3;
    }

    /**
     * get 1st element
     *
     * @return 1st element
     */
    public T1 _1() { return this._1; }

    /**
     * set 1st element
     *
     * @param _1 1st element
     * @return {@code this}
     */
    @Destructive
    public $3<T1, T2, T3> _1(final T1 _1) {
        this._1 = _1;
        return this;
    }

    /**
     * get 2nd element
     *
     * @return 2nd element
     */
    public T2 _2() { return this._2; }

    /**
     * set 2nd element
     *
     * @param _2 2st element
     * @return {@code this}
     */
    @Destructive
    public $3<T1, T2, T3> _2(final T2 _2) {
        this._2 = _2;
        return this;
    }

    /**
     * get 3rt element
     *
     * @return 1st element
     */
    public T3 _3() { return this._3; }

    /**
     * set 3rd element
     *
     * @param _3 3rd element
     * @return {@code this}
     */
    @Destructive
    public $3<T1, T2, T3> _3(final T3 _3) {
        this._3 = _3;
        return this;
    }

    /**
     * expand to all combination of two element tuples.
     *
     * @return all combination of two element tuples
     */
    public $3<$2<T1, T2>, $2<T2, T3>, $2<T1, T3>> _12_23_13() { return tuple(this._12(), this._23(), this._13()); }

    /**
     * get duo of first and second element.
     *
     * @return duo of first and second element
     */
    public $2<T1, T2> _12() { return tuple(this._1, this._2); }

    /**
     * get duo of second and third element.
     *
     * @return duo of second and third element
     */
    public $2<T2, T3> _23() { return tuple(this._2, this._3); }

    /**
     * get duo of first and third element.
     *
     * @return duo of first and third element
     */
    public $2<T1, T3> _13() { return tuple(this._1, this._3); }

    /**
     * set first and second element the return {@code this} instance.
     *
     * @param t first and second element
     * @return {@code this} instance
     */
    @Destructive
    public $3<T1, T2, T3> _12(final $2<T1, T2> t) {
        this._1 = t._1;
        this._2 = t._2;
        return this;
    }

    /**
     * set second and third element the return {@code this} instance.
     *
     * @param t second and third element
     * @return {@code this} instance
     */
    @Destructive
    public $3<T1, T2, T3> _23(final $2<T2, T3> t) {
        this._2 = t._1;
        this._3 = t._2;
        return this;
    }

    /**
     * set first and third element the return {@code this} instance.
     *
     * @param t first and third element
     * @return {@code this} instance
     */
    @Destructive
    public $3<T1, T2, T3> _13(final $2<T1, T3> t) {
        this._1 = t._1;
        this._3 = t._2;
        return this;
    }

    /**
     * create order reversed tuple.
     *
     * @return newly constructed reversed tuple
     */
    public $3<T3, T2, T1> _321() { return tuple(this._3, this._2, this._1); }

    /**
     * rotate this tuple.
     *
     * @return rotated tuple
     */
    public $3<T3, T1, T2> _312() { return tuple(this._3, this._1, this._2); }

    @Override
    public int hashCode() { return Objects.hash(this.getClass(), this._1, this._2, this._3); }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof $3)) return false;

        final $3<?, ?, ?> that = cast(o);

        return equiv(this._1, that._1) && equiv(this._2, that._2) && equiv(this._3, that._3);
    }

    @Override
    public String toString() { return String.format("(%s, %s, %s)", this._1, this._2, this._3); }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $bool
    extends AbstractRef<Boolean, $bool>
    implements Comparable<$bool>, BooleanSupplier {

    private static final long serialVersionUID = 8087914133902951131L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public boolean $; // NOPMD

    /**
     * constructor.
     */
    protected $bool() { this(false); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $bool(final boolean $) { this.$ = $; }

    @Override
    public void accept(final Boolean $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $bool set(final boolean $) {
        this.$ = $;
        return this;
    }

    @Override
    public Boolean get() { return this.$; }

    @Override
    public boolean getAsBoolean() { return this.$; }

    @Override
    public int compareTo(final $bool that) { return this.compareTo(that.$); }

    public int compareTo(final boolean that) { return Boolean.compare(this.$, that); }

    /**
     * execute the procedure then negate the value if and only if the condition satisfied.
     *
     * @param cond condition
     * @param f a procedure
     */
    public void negateIf(final boolean cond, final Statement f) { this.negateIf(x -> x == cond, f); }

    /**
     * execute the procedure then negate the value if and only if the condition satisfied.
     *
     * @param cond condition
     * @param f a procedure
     */
    public void negateIf(final Predicate<Boolean> cond, final Statement f) {
        this.ifThen(cond, () -> {
            this.$ = !this.$;
            f.run();
        });
    }

    /**
     * execute the procedure if and only if the condition satisfied.
     *
     * @param cond condition
     * @param f a procedure
     */
    public void ifThen(final boolean cond, final Statement f) { this.ifThen(x -> x == cond, f); }

    /**
     * execute the procedure if and only if the condition satisfied.
     *
     * @param cond condition
     * @param f a procedure
     */
    public void ifThen(final Predicate<Boolean> cond, final Statement f) {
        synchronized (this) {
            if (cond.test(this.$)) {
                f.run();
            }
        }
    }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $byte
    extends AbstractRef<Byte, $byte>
    implements NumberRef<Byte, $byte>, Comparable<$byte> {

    private static final long serialVersionUID = -6479571510369831870L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public byte $; // NOPMD

    /**
     * constructor.
     */
    protected $byte() { this((byte) 0); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $byte(final byte $) { this.$ = $; }

    @Override
    public void accept(final Byte ＄) { this.$ = ＄; } @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $char
    extends AbstractRef<Character, $char>
    implements Comparable<$char> {

    private static final long serialVersionUID = 6739224434382670429L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public char $; // NOPMD

    /**
     * constructor.
     */
    protected $char() { this((char) 0); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $char(final char $) { this.$ = $; }

    @Override
    public void accept(final Character $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $char set(final char $) {
        this.$ = $;
        return this;
    }

    @Override
    public Character get() { return this.$; }

    @Override
    public int compareTo(final $char that) { return this.compareTo(that.$); }

    public int compareTo(final char that) { return Character.compare(this.$, that); }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $double
    extends AbstractRef<Double, $double>
    implements NumberRef<Double, $double>, Comparable<$double>, DoubleSupplier, DoubleConsumer {

    private static final long serialVersionUID = -3198217652103277386L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public double $; // NOPMD

    /**
     * constructor.
     */
    protected $double() { this(0); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $double(final double $) { this.$ = $; }

    @Override
    public void accept(final Double $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $double set(final double $) {
        this.$ = $;
        return this;
    }

    @Override
    public Double get() { return this.$; }

    @Override
    public void accept(final double $) { this.$ = $; }

    @Override
    public double getAsDouble() { return this.$; }

    @Override
    public int compareTo(final $double that) { return this.compareTo(that.$); }

    public int compareTo(final double that) { return Double.compare(this.$, that); }

    @Override
    public $double add(final Double $) {
        this.$ += $;
        return this;
    }

    @Override
    public $double mul(final Double $) {
        this.$ *= $;
        return this;
    }

    @Override
    public $double div(final Double $) {
        this.$ /= $;
        return this;
    }

    @Override
    public $double negate() {
        this.$ = -this.$;
        return this;
    }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $float
    extends AbstractRef<Float, $float>
    implements NumberRef<Float, $float>, Comparable<$float> {

    private static final long serialVersionUID = -6479571510369831870L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public float $; // NOPMD

    /**
     * constructor.
     */
    protected $float() { this(0); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $float(final float $) { this.$ = $; }

    @Override
    public void accept(final Float $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $float set(final float $) {
        this.$ = $;
        return this;
    }

    @Override
    public Float get() { return this.$; }

    @Override
    public int compareTo(final $float that) { return this.compareTo(that.$); }

    public int compareTo(final float that) { return Float.compare(this.$, that); }

    @Override
    public $float add(final Float $) {
        this.$ += $;
        return this;
    }

    @Override
    public $float mul(final Float $) {
        this.$ *= $;
        return this;
    }

    @Override
    public $float div(final Float $) {
        this.$ /= $;
        return this;
    }

    @Override
    public $float negate() {
        this.$ = -this.$;
        return this;
    }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $int
    extends AbstractRef<Integer, $int>
    implements NumberRef<Integer, $int>, Comparable<$int>, IntSupplier, IntConsumer {

    private static final long serialVersionUID = -5827553121494604722L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public int $; // NOPMD

    /**
     * constructor.
     */
    protected $int() { this(0); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $int(final int $) { this.$ = $; }

    @Override
    public void accept(final Integer $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $int set(final int $) {
        this.$ = $;
        return this;
    }

    @Override
    public Integer get() { return this.$; }

    @Override
    public int getAsInt() { return this.$; }

    @Override
    public void accept(final int $) { this.$ = $; }

    @Override
    public int compareTo(final $int that) { return this.compareTo(that.$); }

    public int compareTo(final int that) { return Integer.compare(this.$, that); }

    @Override
    public $int add(final Integer $) {
        this.$ += $;
        return this;
    }

    @Override
    public $int mul(final Integer $) {
        this.$ *= $;
        return this;
    }

    @Override
    public $int div(final Integer $) {
        this.$ /= $;
        return this;
    }

    @Override
    public $int negate() {
        this.$ = -this.$;
        return this;
    }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $long
    extends AbstractRef<Long, $long>
    implements NumberRef<Long, $long>, Comparable<$long>, LongSupplier, LongConsumer {

    private static final long serialVersionUID = -5582550853171233363L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public long $; // NOPMD

    /**
     * constructor.
     */
    protected $long() { this(0); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $long(final long $) { this.$ = $; }

    @Override
    public void accept(final Long $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $long set(final long $) {
        this.$ = $;
        return this;
    }

    @Override
    public Long get() { return this.$; }

    @Override
    public void accept(final long $) { this.$ = $; }

    @Override
    public long getAsLong() { return this.$; }

    @Override
    public int compareTo(final $long that) { return this.compareTo(that.$); }

    public int compareTo(final long that) { return Long.compare(this.$, that); }

    @Override
    public $long add(final Long $) {
        this.$ += $;
        return this;
    }

    @Override
    public $long mul(final Long $) {
        this.$ *= $;
        return this;
    }

    @Override
    public $long div(final Long $) {
        this.$ /= $;
        return this;
    }

    @Override
    public $long negate() {
        this.$ = -this.$;
        return this;
    }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $short
    extends AbstractRef<Short, $short>
    implements NumberRef<Short, $short>, Comparable<$short> {

    private static final long serialVersionUID = -6819998391641172785L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public short $; // NOPMD

    /**
     * constructor.
     */
    protected $short() { this((short) 0); /* NOPMD*/ }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $short(final short $) { /* NOPMD*/this.$ = $; }

    @Override
    public void accept(final Short $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $short set(final short $) { // NOPMD
        this.$ = $;
        return this;
    }

    @Override
    public Short get() { return this.$; }

    @Override
    public int compareTo(final $short that) { return this.compareTo(that.$); }

    public int compareTo(final short that) { return Short.compare(this.$, that); }

    @Override
    public $short add(final Short $) {
        this.$ += $;
        return this;
    }

    @Override
    public $short mul(final Short $) {
        this.$ *= $;
        return this;
    }

    @Override
    public $short div(final Short $) {
        this.$ /= $;
        return this;
    }

    @Override
    public $short negate() {
        this.$ = (short) -this.$; // NOPMD
        return this;
    }
} public class $void<T>
    extends AbstractRef<T, $void<T>> {

    private static final long serialVersionUID = 2548417883489580934L;

    /** the value. */
    @SuppressWarnings("PublicField")
    public T $; // NOPMD

    /**
     * constructor.
     */
    protected $void() { this(null); }

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $void(final T $) { this.$ = $; }

    @Override
    public void accept(final T $) { this.$ = $; }

    /**
     * set value then return this instance.
     *
     * @param $ value
     * @return {@code this}
     */
    public $void<T> set(final T $) {
        this.$ = $;
        return this;
    }

    @Override
    public T get() { return this.$; }
} @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class $voidc<T extends Comparable<T>>
    extends $void<T>
    implements Comparable<Ref<? extends T, ? extends Ref<? extends T, ?>>> {

    private static final long serialVersionUID = 8031677453769730996L;

    /**
     * constructor.
     */
    protected $voidc() {}

    /**
     * constructor.
     *
     * @param $ the value.
     */
    protected $voidc(final T $) { super($); }

    @Override
    public int compareTo(final Ref<? extends T, ? extends Ref<? extends T, ?>> that) {
        return this.get().compareTo(that.get());
    }

    /**
     * test if value is equivalent or not.
     *
     * @param $ value to compare.
     * @return test result
     */
    public boolean equiv(final T $) { return Indolently.equiv(this.get(), $); }

    /**
     * compute largest value.
     *
     * @param that comparison target
     * @return largest value.
     */
    public T max(final Ref<? extends T, ?> that) { return Indolently.max(this.get(), that.get()); }

    /**
     * compute largest value.
     *
     * @param $ comparison target
     * @return largest value.
     */
    public T max(final T $) { return Indolently.max(this.get(), $); }

    /**
     * compute smallest value.
     *
     * @param that comparison target
     * @return smallest value.
     */
    public T min(final Ref<? extends T, ?> that) { return Indolently.min(this.get(), that.get()); }

    /**
     * compute smallest value.
     *
     * @param $ comparison target
     * @return smallest value.
     */
    public T min(final T $) { return Indolently.min(this.get(), $); }
} public interface Ref<T, S extends Ref<T, S>>
    extends Supplier<T>, Consumer<T>, Identical<S> {

    /**
     * get value then do something with this instance.
     *
     * @param f any operation
     * @return the value
     */
    default T getThen(final Consumer<? super S> f) { return this.optThen(f).get(); }

    /**
     * get value if exists, then do something with this instance.
     *
     * @param f any operation
     * @return optional representation of the value
     */
    default $<T> optThen(final Consumer<? super S> f) {
        final var curr = this.opt();
        f.accept(this.identity());
        return curr;
    }

    /**
     * do something with this instance, then get value if exists.
     *
     * @param f any operation
     * @return optional representation of the value
     */
    default S init(final Consumer<? super S> f) {
        if (this.get() == null) {
            f.accept(this.identity());
        }

        return this.identity();
    }

    /**
     * get value as optional representation.
     *
     * @return optional representation of the value
     */
    default $<T> opt() { return Indolently.opt(this.get()); }

    /**
     * get value if exists, otherwise use the value from supplier as this reference's value.
     *
     * @param f value supplier
     * @return the value
     */
    default T orAccept(final Expression<? extends T> f) {

        return this.opt().or(() -> {
            final var val = f.get();
            this.accept(val);
            return val;
        });
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $bool of(final boolean val) { return new $bool(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $int of(final int val) { return new $int(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $long of(final long val) { return new $long(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $double of(final double val) { return new $double(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $float of(final float val) { return new $float(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $short of(final short val) { /* NOPMD*/return new $short(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $byte of(final byte val) { return new $byte(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static $char of(final char val) { return new $char(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static <T> $void<T> of(final T val) { return new $void<>(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static <T extends Comparable<T>> $voidc<T> of(final T val) { return new $voidc<>(val); }
} public class AutomatonTest
    implements ReTest, ReFindable {

    private final RegExp re;

    final RunAutomaton automaton;

    private final String pattern;

    public AutomatonTest(final RegExp re, final String pattern) {
        this(re, new RunAutomaton(re.toAutomaton()), pattern);
    }

    public AutomatonTest(final RegExp re, final RunAutomaton automaton, final String pattern) {
        this.re = re;
        this.automaton = automaton;
        this.pattern = pattern;
    }

    @Override
    public String pattern() { return this.pattern; }

    public RegExp regex() { return this.re; }

    @Override
    public boolean test(final CharSequence cs) { return this.automaton.run(cs.toString()); }

    @Override
    public boolean find(final CharSequence cs) { return this.matcher(cs).find(); }

    public ReMatcherAutomaton matcher(final CharSequence cs) { return new ReMatcherAutomaton(this, cs); }

    @Override
    public String toString() { return this.pattern(); }
} public interface ReTest
    extends Predicate<CharSequence> {

    /**
     * get pattern string.
     *
     * @return regex string
     */
    String pattern();

    static ReTest of(final RegexBase<?, ?> ptrn) { return of(ptrn, ptrn.pattern()); }

    static ReTest of(final Predicate<CharSequence> pred, final String pattern) {
        return new ReTest() {

            @Override
            public String pattern() { return pattern; }

            @Override
            public boolean test(final CharSequence s) { return pred.test(s); }

            @Override
            public String toString() { return pattern; }
        };
    }
} public final class Regex
    implements RegexBase<Regex.Ptrn, ReMatcher> {

    public interface Ptrn {

        String pattern();
    }

    private final RegexBase<?, ?> pattern;

    public Regex(final RegexBase<?, ?> pattern) { this.pattern = pattern; }

    public <T extends RegexBase<?, ?>> T unwrap() {
        //noinspection unchecked
        return (T) this.pattern;
    }

    @Override
    public String toString() { return this.pattern.toString(); }

    @Override
    public int hashCode() { return this.pattern.hashCode(); }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof RegexBase that)) return false;

        return this.pattern().equals(that.pattern());
    }

    @Override
    public Ptrn ptrn() { return this::pattern; }

    @Override
    public ReMatcher<?, ?> matcher(final CharSequence cs) { return this.pattern.matcher(cs); }

    @Override
    public boolean test(final CharSequence cs) { return this.pattern.test(cs); }

    @Override
    public String pattern() { return this.pattern.pattern(); }

    @Override
    public $list<String> split(final CharSequence cs, final int limit) { return this.pattern.split(cs, limit); }
} public final class RegexJDK
    implements RegexBase<Pattern, ReMatcherJDK> {

    private final Pattern pattern;

    public RegexJDK(final Pattern pattern) {
        this.pattern = pattern;
    }

    @Override
    public Pattern ptrn() {
        return this.pattern;
    }

    @Override
    public String pattern() {
        return this.ptrn().pattern();
    }

    @Override
    public ReMatcherJDK matcher(final CharSequence cs) {
        return new ReMatcherJDK(this.ptrn().matcher(cs), cs);
    }

    @Override
    public $list<String> split(final CharSequence cs, final int limit) {
        return Indolently.list(this.ptrn().split(cs, limit));
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        } else if (!(o instanceof Regex)) {
            return false;
        }

        return this.ptrn().equals(((Regex) o).ptrn());
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.getClass(), this.pattern);
    }

    @Override
    public String toString() {
        return this.pattern.toString();
    }
} public final class RegexRe2
    implements RegexBase<Pattern, ReMatcherRE2> {

    private final Pattern pattern;

    public RegexRe2(final Pattern pattern) {
        this.pattern = pattern;
    }

    @Override
    public Pattern ptrn() {
        return this.pattern;
    }

    @Override
    public String pattern() {
        return this.ptrn().pattern();
    }

    @Override
    public boolean test(final CharSequence cs) {
        return this.matcher(cs).matches();
    }

    @Override
    public ReMatcherRE2 matcher(final CharSequence cs) {
        return new ReMatcherRE2(this.ptrn().matcher(cs), cs);
    }

    @Override
    public $list<String> split(final CharSequence cs, final int limit) {
        return Indolently.list(this.ptrn().split(cs.toString(), limit));
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        } else if (!(o instanceof Regex)) {
            return false;
        }

        return this.ptrn().equals(((Regex) o).ptrn());
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.getClass(), this.pattern);
    }

    @Override
    public String toString() {
        return this.pattern.toString();
    }
} public class Expressive {

    /** non private for subtyping. */
    protected Expressive() { }

    public static class RaisedException
        extends RuntimeException {

        private static final long serialVersionUID = 1037309592665638717L;

        public RaisedException(final Throwable e) { super(e); }
    }

    /**
     * Throw exception in expression manner.
     *
     * @param <T> the type of expression
     * @param e exception
     * @return actually return nothing
     */
    public static <T> T raise(final Throwable e) { return raise(() -> e); }

    /**
     * Throw exception in expression manner.
     *
     * @param <T> the type of expression
     * @param f supplies exception to throw
     * @return actually return nothing
     */
    public static <T> T raise(final Supplier<? extends Throwable> f) {

        throw opt(f.get()).map(e -> {
            if (e instanceof Error er) throw er;
            if (e instanceof RuntimeException re) return re;
            return new RaisedException(e);
        }).or(() -> new NullPointerException("supplier returns null: " + f));
    }

    /**
     * Block statement.
     *
     * @param stmt statement body
     */
    public static void let(final Statement stmt) { stmt.run(); }

    /**
     * Block statement.
     *
     * @param <E> the type of exception
     * @param stmt statement body
     */
    public static <E extends Exception> void letTry(final RunnableE<E> stmt) throws E { stmt.run(); }

    /**
     * In-line evaluation expression.
     *
     * @param <R> the type of expression
     * @param <E> the type of exception
     * @param expr expression body
     * @return the value of body expression
     */
    public static <R, E extends Exception> R evalTry(final SupplierE<R, E> expr) throws E { return expr.get(); }

    /**
     * In-line evaluation expression.
     *
     * @param <R> the type of expression
     * @param expr expression body
     * @return the value of body expression
     */
    public static <R> R eval(final Expression<R> expr) { return expr.get(); }

    /**
     * Block statement.
     *
     * @param value the value
     * @param stmt expression body
     */
    public static <T> void let(final T value, final Consumer<? super T> stmt) { stmt.accept(value); }

    /**
     * Block statement.
     *
     * @param <E> the type of exception
     * @param value the value
     * @param stmt expression body
     */
    public static <T, E extends Exception> void letTry(final T value, final ConsumerE<? super T, E> stmt) throws E {
        stmt.accept(value);
    }

    /**
     * Block statement which applied only if the value is an instance of the statement's argument type.
     *
     * @param value the value
     * @param stmt statement body
     */
    public static <T> void ifInstance(final Object value, final Consumer<T> stmt) {
        if (argTypeOf(stmt).isInstance(value)) stmt.accept(cast(value));
    }

    /**
     * if-else statement alternative.
     *
     * @param <T> context type
     * @param context context value
     * @param cond context test function
     * @param then context conversion function used if the condition is {@code true}
     * @param other context conversion function used if the condition is {@code false}
     */
    public static <T> void let(final T context, final Predicate<? super T> cond, final Consumer<? super T> then,
        final Consumer<? super T> other) {

        if (cond.test(context)) //
            then.accept(context);
        else //
            other.accept(context);
    }

    /**
     * In-line evaluation expression.
     *
     * @param <T> value type
     * @param <R> the type of expression
     * @param val the value
     * @param expr expression body
     * @return the value function returned
     */
    public static <T, R> R eval(final T val, final Function<? super T, ? extends R> expr) { return expr.apply(val); }

    /**
     * In-line evaluation expression.
     *
     * @param <T> value type
     * @param <R> the type of expression
     * @param <E> the type of exception
     * @param val the value
     * @param expr expression body
     * @return the value function returned
     */
    public static <T, R, E extends Exception> R evalTry(final T val, final FunctionE<? super T, ? extends R, E> expr)
        throws E { return expr.apply(val); }

    /**
     * In-line evaluation expression.
     *
     * @param <T1> first value type
     * @param <T2> second value type
     * @param <R> the type of expression
     * @param val1 first value
     * @param val2 second value
     * @param expr expression body
     * @return the value function returned
     */
    public static <T1, T2, R> R eval(final T1 val1, final T2 val2,
        final BiFunction<? super T1, ? super T2, ? extends R> expr) { return expr.apply(val1, val2); }

    /**
     * In-line evaluation expression.
     *
     * @param <T1> first value type
     * @param <T2> second value type
     * @param <R> the type of expression
     * @param <E> the type of exception
     * @param val1 first value
     * @param val2 second value
     * @param expr expression body
     * @return the value function returned
     */
    public static <T1, T2, R, E extends Exception> R evalTry(final T1 val1, final T2 val2,
        final Function2E<? super T1, ? super T2, ? extends R, E> expr) throws E { return expr.apply(val1, val2); }

    /**
     * In-line evaluation expression.
     *
     * @param <T1> first value type
     * @param <T2> second value type
     * @param <T3> third value type
     * @param <R> the type of expression
     * @param val1 first value
     * @param val2 second value
     * @param val3 third value
     * @param expr expression body
     * @return the value function returned
     */
    public static <T1, T2, T3, R> R eval(final T1 val1, final T2 val2, final T3 val3,
        final Function3<? super T1, ? super T2, ? super T3, ? extends R> expr) { return expr.apply(val1, val2, val3); }

    /**
     * In-line evaluation expression.
     *
     * @param <T1> first value type
     * @param <T2> second value type
     * @param <T3> third value type
     * @param <R> the type of expression
     * @param <E> the type of exception
     * @param val1 first value
     * @param val2 second value
     * @param val3 third value
     * @param expr expression body
     * @return the value function returned
     */
    public static <T1, T2, T3, R, E extends Exception> R evalTry(final T1 val1, final T2 val2, final T3 val3,
        final Function3E<? super T1, ? super T2, ? super T3, ? extends R, E> expr) throws E {
        return expr.apply(val1, val2, val3);
    }

    /**
     * evaluate following forms then return evaluation result of first expressions.
     *
     * @param <T> the type of expression
     * @param first evaluation result of this expression
     * @param forms evaluation target forms. argument is evaluation result of {@code first}.
     * @return first expression evaluation result
     */
    @SuppressWarnings({ "varargs", "RedundantSuppression" })
    @SafeVarargs
    public static <T> T prog1(final Supplier<? extends T> first, final Consumer<? super T>... forms) {

        final var val = first.get();

        list(forms).each(f -> f.accept(val));

        return val;
    }

    /**
     * evaluate following forms then return evaluation result of first expressions.
     *
     * @param <T> the type of expression
     * @param <E> the type of exception
     * @param first evaluation result of this expression
     * @param forms evaluation target forms. argument is evaluation result of {@code first}.
     * @return first expression evaluation result
     */
    @SuppressWarnings({ "varargs", "RedundantSuppression" })
    @SafeVarargs
    public static <T, E extends Exception> T prog1E(final SupplierE<? extends T, E> first,
        final ConsumerE<? super T, E>... forms) throws E {

        final var val = first.get();

        for (final var f: list(forms)) { f.accept(val); }

        return val;
    }

    /**
     * evaluate following forms then return first expression evaluation result.
     *
     * @param <T> the type of entire expression
     * @param first evaluation result of this expression
     * @param forms evaluation target forms
     * @return first expression evaluation result
     */
    public static <T> T prog1(final Supplier<? extends T> first, final Statement... forms) {

        final var val = first.get();
        list(forms).each(f -> f.run());
        return val;
    }

    @FunctionalInterface
    public interface WithBlock<X, Y> {

        Y apply(X x) throws IOException;
    }

    public static <T extends Closeable, R> R with(final T res, final WithBlock<T, R> f) throws IOException {
        try (res) { return f.apply(res); }
    }

    /**
     * try-with-resource statement.
     *
     * @param res the resource
     * @param stmt expression body
     */
    public static <T extends AutoCloseable> void letWith(final T res, final Consumer<? super T> stmt) {
        // cast is required to avoid compilation failure on javac
        letWith((Supplier<T>) () -> res, stmt);
    }

    /**
     * try-with-resource statement.
     *
     * @param res the resource
     * @param stmt expression body
     */
    public static <T extends AutoCloseable> void letWith(final Supplier<? extends T> res,
        final Consumer<? super T> stmt) {

        evalWith(res, x -> {
            stmt.accept(x);
            return null;
        });
    }

    /**
     * try-with-resource expression.
     *
     * @param res the resource
     * @param expr expression body
     * @return the value function returned
     */
    public static <T extends AutoCloseable, R> R evalWith(final T res, final Function<? super T, ? extends R> expr) {
        // cast is required to avoid compilation failure on javac
        return evalWith((Supplier<T>) () -> res, expr);
    }

    /**
     * try-with-resource expression.
     *
     * @param res the resource
     * @param expr expression body
     * @return the value function returned
     */
    @SuppressWarnings({ "try", "RedundantSuppression" })
    public static <T extends AutoCloseable, R> R evalWith(final Supplier<? extends T> res,
        final Function<? super T, ? extends R> expr) {

        return eval(() -> {
            try (final T x = res.get()) { return expr.apply(x); }
        });
    }

    /**
     * if-then-else expression.
     *
     * @author takahashikzn
     * @see Expressive#when(BooleanSupplier)
     */
    public interface When {

        /**
         * The value expression treated as the return value of the entire expression if the just before conditional
         * expression evaluated as {@code true}.
         *
         * @param <T> the type of entire expression
         * @param then value expression
         * @return 'then' object
         */
        <T> Then<T> then(Supplier<? extends T> then);

        /**
         * The value treated as the return value of the entire expression if the just before conditional expression
         * evaluated as {@code true}.
         *
         * @param <T> the type of entire expression
         * @param then a value
         * @return 'then' object
         */
        default <T> Then<T> then(final T then) { return this.then(() -> then); }

        /**
         * The body expression of 'if' expression.
         *
         * @param <T> the type of entire expression
         * @author takahashikzn
         */
        interface Then<T> {

            /**
             * Returns the value of entire expression. This is terminal operation.
             *
             * @param none value expression evaluated if and only if all conditional expression evaluated as
             * {@code false}.
             * @return the return value of the entire expression
             */
            T none(Supplier<? extends T> none);

            /**
             * 'else-if' expression.
             *
             * @param when a condition
             * @return 'case' object as an 'else-if' expression.
             */
            Case<T> when(BooleanSupplier when);

            default T none(final T none) { return this.none(() -> none); }

            default Case<T> when(final boolean when) { return this.when(() -> when); }

            default T raise(final Supplier<? extends RuntimeException> raise) {
                return this.none(() -> Expressive.raise(() -> raise.get()));
            }

            default T fatal() { return this.none(() -> Indolently.fatal()); }

            default T fatal(final Object msg) { return this.none(() -> Indolently.fatal(msg)); }
        }

        /**
         * 'if' expression.
         *
         * @param <T> the type of entire expression
         * @author takahashikzn
         */
        interface Case<T> {

            Then<T> then(Supplier<? extends T> then);

            default Then<T> then(final T then) { return this.then(() -> then); }
        }
    }

    /**
     * A shortcut of {@code when(() -> pred)}.
     *
     * @param pred constant conditional value
     * @return 'when' object
     */
    public static When when(final boolean pred) { return when(() -> pred); }

    /**
     * if-then-else expression.
     *
     * <div>
     * Example
     *
     * {@code
     * int x = ...;
     *
     * String evenOrOdd = Expressive
     * .when(() -> x % 2 == 0).then(() -> "even")
     * .when(() -> x % 2 != 0).then(() -> "odd")
     * .none(() -> "This is terminal operation but never called in this example.");
     *
     * // constants are acceptable
     * String simplerEvenOrOdd = Expressive
     * .when(x % 2 == 0).then("even")
     * .when(x % 2 != 0).then("odd")
     * .none("This is terminal operation but never called in this example.");
     * }
     * </div>
     *
     * @param pred first condition
     * @return 'when' object
     */
    public static When when(final BooleanSupplier pred) {

        final Match.IntroCase<?> intro = match(null).when(x -> pred.getAsBoolean());

        return new When() {

            @Override
            public <T> When.Then<T> then(final Supplier<? extends T> then) {

                return new When.Then<T>() {

                    @Override
                    public T none(final Supplier<? extends T> none) {
                        return intro //
                            .then(x -> (T) then.get()) //
                            .none(none);
                    }

                    @Override
                    public When.Case<T> when(final BooleanSupplier when) {
                        return toWhenCase(intro //
                            .then(x -> (T) then.get()) //
                            .when(x -> when.getAsBoolean()));
                    }
                };
            }
        };
    }

    private static <T> When.Case<T> toWhenCase(final Match.Case<?, T> theCase) {

        return then -> new When.Then<T>() {

            @Override
            public T none(final Supplier<? extends T> none) { return theCase.then(then).none(none); }

            @Override
            public When.Case<T> when(final BooleanSupplier when) { return toWhenCase(theCase.then(then).when(when)); }
        };
    }

    @SuppressWarnings({ "overloads", "RedundantSuppression" })
    private static <T> Class<T> argTypeOf(final Function<T, ?> f) {
        return cast(TypeResolver.resolveRawArguments(Function.class, f.getClass())[0]);
    }

    @SuppressWarnings({ "overloads", "RedundantSuppression" })
    private static <T> Class<T> argTypeOf(final Consumer<T> f) {
        return cast(TypeResolver.resolveRawArguments(Consumer.class, f.getClass())[0]);
    }

    public interface Match<C> {

        IntroCase<C> when(Predicate<? super C> pred);

        default IntroCase<C> when(final boolean pred) { return this.when(fixed(pred)); }

        default IntroCase<C> when(final Supplier<? extends C> pred) {
            return this.when(x -> Indolently.equal(x, pred.get()));
        }

        default IntroCase<C> when(final Class<?> type) { return this.when(x -> type.isInstance(x)); }

        default <SC extends C, T> Then<C, T> type(final Function<SC, ? extends T> then) {
            return eval(argTypeOf(then),
                argType -> this.when(argType::isInstance).then(ctx -> then.apply(argType.cast(ctx))));
        }

        interface IntroCase<C> {

            <T> Then<C, T> then(Function<? super C, ? extends T> then);

            default <T> Then<C, T> then(final T then) { return this.then(() -> then); }

            default <T> Then<C, T> then(final Supplier<? extends T> then) { return this.then(x -> then.get()); }
        }

        interface Then<C, T> {

            T none(Function<? super C, ? extends T> none);

            Case<C, T> when(Predicate<? super C> when);

            default Case<C, T> when(final Class<?> type) { return this.when(x -> type.isInstance(x)); }

            default <SC extends C> Then<C, T> type(final Function<SC, ? extends T> then) {

                return eval(argTypeOf(then),
                    argType -> this.when(argType::isInstance).then(ctx -> then.apply(argType.cast(ctx))));
            }

            default Case<C, T> when(final boolean when) { return this.when(fixed(when)); }

            default Case<C, T> when(final BooleanSupplier when) { return this.when(x -> when.getAsBoolean()); }

            default Case<C, T> when(final Supplier<? extends C> pred) {
                return this.when(x -> Indolently.equal(x, pred.get()));
            }

            default T none(final T none) { return this.none(() -> none); }

            default T none(final Supplier<? extends T> none) { return this.none(x -> none.get()); }

            default T raise(final Function<? super C, ? extends RuntimeException> raise) {
                return this.none(x -> Expressive.raise(() -> raise.apply(x)));
            }

            default T fatal() { return this.none(() -> Indolently.fatal()); }

            default T fatal(final Object msg) { return this.none(() -> Indolently.fatal(msg)); }
        }

        interface Case<C, T> {

            Then<C, T> then(Function<? super C, ? extends T> then);

            default Then<C, T> then(final T then) { return this.then(() -> then); }

            default Then<C, T> then(final Supplier<? extends T> then) { return this.then(x -> then.get()); }
        }
    }

    public static <C> Match<C> match(final C ctx) {

        return pred -> new Match.IntroCase<C>() {

            @Override
            public <T> Match.Then<C, T> then(final Function<? super C, ? extends T> then) {
                return Expressive.<C, T> toUnresolvedCase(ctx, pred).then(then);
            }
        };
    }

    private static <C, T> Match.Case<C, T> toResolvedCase(final T value) {

        return new Match.Case<C, T>() {

            @Override
            public Match.Then<C, T> then(final Function<? super C, ? extends T> then) {

                final Match.Case<C, T> self = this;

                return new Match.Then<C, T>() {

                    @Override
                    public T none(final Function<? super C, ? extends T> none) { return value; }

                    @Override
                    public Match.Case<C, T> when(final Predicate<? super C> when) { return self; }
                };
            }
        };
    }

    private static <C, T> Match.Case<C, T> toUnresolvedCase(final C ctx, final Predicate<? super C> condition) {

        final Predicate<? super C> pred = Functional.$(condition).memoize();

        return then -> new Match.Then<C, T>() {

            @Override
            public T none(final Function<? super C, ? extends T> none) {
                return pred.test(ctx) //
                    ? then.apply(ctx) //
                    : none.apply(ctx);
            }

            @Override
            public Match.Case<C, T> when(final Predicate<? super C> when) {
                return pred.test(ctx) //
                    ? toResolvedCase(then.apply(ctx)) //
                    : toUnresolvedCase(ctx, when);
            }
        };
    }
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.DoublePredicate;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import jp.root42.indolently.bridge.BytesInputStream;
import jp.root42.indolently.bridge.BytesOutputStream;
import jp.root42.indolently.bridge.ObjFactory;
import jp.root42.indolently.conc.Promise;
import jp.root42.indolently.conc.Promissory;
import jp.root42.indolently.function.RunnableE;
import jp.root42.indolently.function.Statement;
import jp.root42.indolently.ref.$;
import jp.root42.indolently.ref.$$;
import jp.root42.indolently.ref.$2;
import jp.root42.indolently.ref.$3;
import jp.root42.indolently.ref.$bool;
import jp.root42.indolently.ref.$byte;
import jp.root42.indolently.ref.$char;
import jp.root42.indolently.ref.$double;
import jp.root42.indolently.ref.$float;
import jp.root42.indolently.ref.$int;
import jp.root42.indolently.ref.$long;
import jp.root42.indolently.ref.$short;
import jp.root42.indolently.ref.$void;
import jp.root42.indolently.ref.$voidc;
import jp.root42.indolently.ref.Ref;
import jp.root42.indolently.regex.AutomatonTest;
import jp.root42.indolently.regex.ReTest;
import jp.root42.indolently.regex.Regex;
import jp.root42.indolently.regex.RegexJDK;
import jp.root42.indolently.regex.RegexRe2;
import static jp.root42.indolently.Expressive.*;

                : new $stream_impl<>(stream);
    }

    public static <K, V> $map<K, V> newMap(final Class<K> keyType, final Class<V> valType) { return map(); }

    @SuppressWarnings("unchecked")
    public static <K, V> $map<K, V> map(final K key, final V val) { return ($map<K, V>) map().push(key, val); }

    @SuppressWarnings("unchecked")
    public static <K, V> $map<K, V> map(final K key, final $<? extends V> val) {
        return ($map<K, V>) map().push(key, val);
    }

    /**
     * create two element tuple.
     *
     * @param _1 1st element
     * @param _2 2nd element
     * @return tuple
     */
    public static <F, S> $2<F, S> tuple(final F _1, final S _2) { return new $2<F, S>()._1(_1)._2(_2); }

    /**
     * create three element tuple.
     *
     * @param _1 1st element
     * @param _2 2nd element
     * @param _3 3rd element
     * @return tuple
     */
    public static <F, S, T> $3<F, S, T> tuple(final F _1, final S _2, final T _3) {
        return new $3<F, S, T>()._1(_1)._2(_2)._3(_3);
    }

    public static <F, S> Function<$2<F, S>, F> _1() { return x -> x._1; }

    public static <F, S> Function<$2<F, S>, S> _2() { return x -> x._2; }

    public static <F, S, T> Function<$3<F, S, T>, F> _1of3() { return x -> x._1; }

    public static <F, S, T> Function<$3<F, S, T>, S> _2of3() { return x -> x._2; }

    public static <F, S, T> Function<$3<F, S, T>, T> _3of3() { return x -> x._3; }

    public static <T, R> Function<List<T>, List<R>> mapl(final Function<T, R> f) {
        return (List<T> list) -> $(list).map(f);
    }

    public static <T, R> Function<Set<T>, Set<R>> maps(final Function<T, R> f) {
        return (Set<T> set) -> $(set).map(f);
    }

    public static <K, T, R> Function<Map<K, T>, Map<K, R>> mapm(final Function<T, R> f) {
        return (Map<K, T> map) -> $(map).map(f);
    }

    /**
     * Convert tuple to list.
     *
     * @param tuple two element tuple
     * @return list of tuple elements
     */
    public static <T> $list<T> list(final $2<? extends T, ? extends T> tuple) { return list(tuple._1, tuple._2); }

    /**
     * Convert tuple to list.
     *
     * @param tuple three element tuple
     * @return list of tuple elements
     */
    public static <T> $list<T> list(final $3<? extends T, ? extends T, ? extends T> tuple) {
        return list(tuple._1, tuple._2, tuple._3);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    public static $bool ref(final boolean val) { return Ref.of(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    public static $int ref(final int val) { return Ref.of(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    public static $long ref(final long val) { return Ref.of(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    public static $double ref(final double val) { return Ref.of(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    public static $float ref(final float val) { return Ref.of(val); }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */

    public static $short ref(final short val) { return Ref.of(val); }
