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

org.rythmengine.utils.F Maven / Gradle / Ivy

Go to download

A strong typed high performance Java Template engine with .Net Razor like syntax

There is a newer version: 1.4.2
Show newest version
/**
 * Copyright (C) 2013-2016 The Rythm Engine project
 * for LICENSE and other details see:
 * https://github.com/rythmengine/rythmengine
 */
package org.rythmengine.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Utility classes
 */
// Most of the code come from Play!Framework F.java, under Apache License 2.0
public class F {

    private F() {
    }

    public static interface Action0 {

        void invoke();
    }

    public static interface Action {

        void invoke(T result);
    }

    public static abstract class Option implements Iterable {

        public abstract boolean isDefined();

        public abstract T get();

        public static  None none() {
            return (None) (Object) None;
        }

        public static  Some some(T value) {
            return new Some(value);
        }
    }

    public static  Some some(A a) {
        return new Some(a);
    }

    public static class None extends Option {

        @Override
        public boolean isDefined() {
            return false;
        }

        @Override
        public T get() {
            throw new IllegalStateException("No value");
        }

        public Iterator iterator() {
            return Collections.emptyList().iterator();
        }

        @Override
        public String toString() {
            return "None";
        }
    }

    public static final None None = new None();

    public static class Some extends Option {

        final T value;

        public Some(T value) {
            this.value = value;
        }

        @Override
        public boolean isDefined() {
            return true;
        }

        @Override
        public T get() {
            return value;
        }

        public Iterator iterator() {
            return Collections.singletonList(value).iterator();
        }

        @Override
        public String toString() {
            return "Some(" + value + ")";
        }
    }

    public static class Either {

        final public Option _1;
        final public Option _2;

        private Either(Option _1, Option _2) {
            this._1 = _1;
            this._2 = _2;
        }

        public static  Either _1(A value) {
            return new Either(some(value), None);
        }

        public static  Either _2(B value) {
            return new Either(None, some(value));
        }

        @Override
        public String toString() {
            return "E2(_1: " + _1 + ", _2: " + _2 + ")";
        }
    }

    public static class E2 extends Either {

        private E2(Option _1, Option _2) {
            super(_1, _2);
        }
    }

    public static class E3 {

        final public Option _1;
        final public Option _2;
        final public Option _3;

        private E3(Option _1, Option _2, Option _3) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
        }

        public static  E3 _1(A value) {
            return new E3(some(value), None, None);
        }

        public static  E3 _2(B value) {
            return new E3(None, some(value), None);
        }

        public static  E3 _3(C value) {
            return new E3(None, None, some(value));
        }

        @Override
        public String toString() {
            return "E3(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ")";
        }
    }

    public static class E4 {

        final public Option _1;
        final public Option _2;
        final public Option _3;
        final public Option _4;

        private E4(Option _1, Option _2, Option _3, Option _4) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
            this._4 = _4;
        }

        public static  E4 _1(A value) {
            return new E4(Option.some(value), None, None, None);
        }

        public static  E4 _2(B value) {
            return new E4(None, some(value), None, None);
        }

        public static  E4 _3(C value) {
            return new E4(None, None, some(value), None);
        }

        public static  E4 _4(D value) {
            return new E4(None, None, None, some(value));
        }

        @Override
        public String toString() {
            return "E4(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ", _4:" + _4 + ")";
        }
    }

    public static class E5 {

        final public Option _1;
        final public Option _2;
        final public Option _3;
        final public Option _4;
        final public Option _5;

        private E5(Option _1, Option _2, Option _3, Option _4, Option _5) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
            this._4 = _4;
            this._5 = _5;
        }

        public static  E5 _1(A value) {
            return new E5(Option.some(value), None, None, None, None);
        }

        public static  E5 _2(B value) {
            return new E5(None, Option.some(value), None, None, None);
        }

        public static  E5 _3(C value) {
            return new E5(None, None, Option.some(value), None, None);
        }

        public static  E5 _4(D value) {
            return new E5(None, None, None, Option.some(value), None);
        }

        public static  E5 _5(E value) {
            return new E5(None, None, None, None, Option.some(value));
        }

        @Override
        public String toString() {
            return "E5(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ", _4:" + _4 + ", _5:" + _5 + ")";
        }
    }

    private static boolean eq(Object a, Object b) {
        if (a == b) return true;
        if (null == a) return null == b;
        return a.equals(b);
    }

    public static class Tuple {

        final public A _1;
        final public B _2;

        public Tuple(A _1, B _2) {
            this._1 = _1;
            this._2 = _2;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o instanceof Tuple) {
                Tuple that = (Tuple) o;
                return eq(that._1, _1) && eq(that._2, _2);
            }
            return false;
        }

        @Override
        public int hashCode() {
            int i = 17;
            if (null != _1) i = i * 31 + _1.hashCode();
            if (null != _2) i = i * 31 + _2.hashCode();
            return i;
        }

        @Override
        public String toString() {
            return "T2(_1: " + _1 + ", _2: " + _2 + ")";
        }
    }

    public static  Tuple tuple(A a, B b) {
        return new Tuple(a, b);
    }

    public static class T2 extends Tuple {

        public T2(A _1, B _2) {
            super(_1, _2);
        }
        
        public Map asMap() {
            Map m = new HashMap();
            m.put(_1, _2);
            return m;
        }
    }

    public static  T2 T2(A a, B b) {
        return new T2(a, b);
    }

    public static class T3 {

        final public A _1;
        final public B _2;
        final public C _3;

        public T3(A _1, B _2, C _3) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o instanceof T3) {
                T3 that = (T3) o;
                return eq(that._1, _1) && eq(that._2, _2) && eq(that._3, _3);
            }
            return false;
        }

        @Override
        public int hashCode() {
            int i = 17;
            if (null != _1) i = i * 31 + _1.hashCode();
            if (null != _2) i = i * 31 + _2.hashCode();
            if (null != _3) i = i * 31 + _3.hashCode();
            return i;
        }

        @Override
        public String toString() {
            return "T3(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ")";
        }
    }

    public static  T3 T3(A a, B b, C c) {
        return new T3(a, b, c);
    }

    public static class T4 {

        final public A _1;
        final public B _2;
        final public C _3;
        final public D _4;

        public T4(A _1, B _2, C _3, D _4) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
            this._4 = _4;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o instanceof T4) {
                T4 that = (T4) o;
                return eq(that._1, _1) && eq(that._2, _2) && eq(that._3, _3) && eq(that._4, _4);
            }
            return false;
        }

        @Override
        public int hashCode() {
            int i = 17;
            if (null != _1) i = i * 31 + _1.hashCode();
            if (null != _2) i = i * 31 + _2.hashCode();
            if (null != _3) i = i * 31 + _3.hashCode();
            if (null != _4) i = i * 31 + _4.hashCode();
            return i;
        }

        @Override
        public String toString() {
            return "T4(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ", _4:" + _4 + ")";
        }
    }

    public static  T4 T4(A a, B b, C c, D d) {
        return new T4(a, b, c, d);
    }

    public static class T5 {

        final public A _1;
        final public B _2;
        final public C _3;
        final public D _4;
        final public E _5;

        public T5(A _1, B _2, C _3, D _4, E _5) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
            this._4 = _4;
            this._5 = _5;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o instanceof T5) {
                T5 that = (T5) o;
                return eq(that._1, _1) && eq(that._2, _2) && eq(that._3, _3) && eq(that._4, _4) && eq(that._5, _5);
            }
            return false;
        }

        @Override
        public int hashCode() {
            int i = 17;
            if (null != _1) i = i * 31 + _1.hashCode();
            if (null != _2) i = i * 31 + _2.hashCode();
            if (null != _3) i = i * 31 + _3.hashCode();
            if (null != _4) i = i * 31 + _4.hashCode();
            if (null != _5) i = i * 31 + _5.hashCode();
            return i;
        }

        @Override
        public String toString() {
            return "T5(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ", _4:" + _4 + ", _5:" + _5 + ")";
        }
    }

    public static  T5 T5(A a, B b, C c, D d, E e) {
        return new T5(a, b, c, d, e);
    }

    public static abstract class Matcher {

        public abstract Option match(T o);

        public Option match(Option o) {
            if (o.isDefined()) {
                return match(o.get());
            }
            return Option.none();
        }

        public  Matcher and(final Matcher nextMatcher) {
            final Matcher firstMatcher = this;
            return new Matcher() {

                @Override
                public Option match(T o) {
                    for (R r : firstMatcher.match(o)) {
                        return nextMatcher.match(r);
                    }
                    return Option.none();
                }
            };
        }

        public static final Matcher String = new Matcher() {

            @Override
            public Option match(Object o) {
                if (o instanceof String) {
                    return Option.some((String) o);
                }
                return Option.none();
            }
        };

        public static  Matcher classOf(final Class clazz) {
            return new Matcher() {

                @Override
                public Option match(Object o) {
                    if (o instanceof Option && ((Option) o).isDefined()) {
                        o = ((Option) o).get();
                    }
                    if (clazz.isInstance(o)) {
                        return Option.some((K) o);
                    }
                    return Option.none();
                }
            };
        }

        public static Matcher startsWith(final String prefix) {
            return new Matcher() {

                @Override
                public Option match(String o) {
                    if (o.startsWith(prefix)) {
                        return Option.some(o);
                    }
                    return Option.none();
                }
            };
        }

        public static Matcher re(final String pattern) {
            return new Matcher() {

                @Override
                public Option match(String o) {
                    if (o.matches(pattern)) {
                        return Option.some(o);
                    }
                    return Option.none();
                }
            };
        }

        public static  Matcher eq(final X other) {
            return new Matcher() {

                @Override
                public Option match(X o) {
                    if (o.equals(other)) {
                        return Option.some(o);
                    }
                    return Option.none();
                }
            };
        }
    }

    public static Range R(final int minInclusive, final int maxExclusive) {
        return new Range(minInclusive, maxExclusive) {
            @Override
            protected Integer next(Integer element) {
                return ++element;
            }

            @Override
            public int size() {
                return max() - min();
            }

            @Override
            public boolean include(Integer element) {
                return (min() <= element) && (element < max());
            }
        };
    }

    public static Range R(final char minInclusive, final char maxExclusive) {
        return new Range(minInclusive, maxExclusive) {
            @Override
            protected Character next(Character element) {
                return (char) (element + 1);
            }

            @Override
            public int size() {
                char min = min();
                char max = max();
                return (int) max - (int) min;
            }

            @Override
            public boolean include(Character element) {
                return (min() <= element) && (element < max());
            }
        };
    }

}