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

com.greenlaw110.rythm.utils.F Maven / Gradle / Ivy

Go to download

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

The newest version!
/* 
 * Copyright (C) 2013 The Rythm Engine project
 * Gelin Luo 
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.greenlaw110.rythm.utils;

import java.util.Collections;
import java.util.Iterator;

/**
 * Utility classes
 */
// Most of the code come from Play!Framework F.java, under Apache License 2.0
public class 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 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 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 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 Equals(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());
            }
        };
    }

}