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

com.github.sviperll.Isomorphism Maven / Gradle / Ivy

There is a newer version: 0.36
Show newest version
/*
 * Copyright (c) 2014, Victor Nazarov 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Victor Nazarov nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL  BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.github.sviperll;

/**
 *
 * @author Victor Nazarov 
 */
public class Isomorphism implements IsomorphismDefinition {
    @SuppressWarnings({"rawtypes", "unchecked"})
    private static Isomorphism ID = new Isomorphism(new IdIsomorphismDefinition());

    @SuppressWarnings({"unchecked"})
    public static  Isomorphism identity() {
        return (Isomorphism)ID;
    }

    public static  Isomorphism of(final Applicable forward, final Applicable backward) {
        return of(new IsomorphismDefinition() {
            @Override
            public U forward(T object) {
                return forward.apply(object);
            }

            @Override
            public T backward(U object) {
                return backward.apply(object);
            }
        });
    }

    public static  Isomorphism of(IsomorphismDefinition isomorphism) {
        if (isomorphism instanceof Isomorphism)
            return (Isomorphism)isomorphism;
        else
            return new Isomorphism(isomorphism);
    }

    private final IsomorphismDefinition isomorphism;
    private Isomorphism(IsomorphismDefinition isomorphism) {
        this.isomorphism = isomorphism;
    }

    @Override
    public U forward(T object) {
        return isomorphism.forward(object);
    }

    @Override
    public T backward(U object) {
        return isomorphism.backward(object);
    }

    public  Isomorphism composeWith(final IsomorphismDefinition thatIsomorphism) {
        return new Isomorphism(new IsomorphismDefinition() {
            @Override
            public U forward(V object) {
                return isomorphism.forward(thatIsomorphism.forward(object));
            }

            @Override
            public V backward(U object) {
                return thatIsomorphism.backward(isomorphism.backward(object));
            }
        });
    }

    public  Isomorphism andThen(final IsomorphismDefinition thatIsomorphism) {
        return new Isomorphism(new IsomorphismDefinition() {
            @Override
            public V forward(T object) {
                return thatIsomorphism.forward(isomorphism.forward(object));
            }

            @Override
            public T backward(V object) {
                return isomorphism.backward(thatIsomorphism.backward(object));
            }
        });
    }

    public Isomorphism reverse() {
        return new Isomorphism(new IsomorphismDefinition() {
            @Override
            public T forward(U object) {
                return isomorphism.backward(object);
            }

            @Override
            public U backward(T object) {
                return isomorphism.forward(object);
            }
        });
    }

    public Isomorphism passNullThrough() {
        return new Isomorphism(new IsomorphismDefinition() {
            @Override
            public U forward(T object) {
                if (object == null)
                    return null;
                else
                    return isomorphism.forward(object);
            }

            @Override
            public T backward(U object) {
                if (object == null)
                    return null;
                else
                    return isomorphism.backward(object);
            }
        });
    }

    public Isomorphism throwOnNull() {
        return new Isomorphism(new IsomorphismDefinition() {
            @Override
            public U forward(T object) {
                if (object == null)
                    throw new NullPointerException();
                else
                    return isomorphism.forward(object);
            }

            @Override
            public T backward(U object) {
                if (object == null)
                    throw new NullPointerException();
                else
                    return isomorphism.backward(object);
            }
        });
    }

    public Function forwardFunction() {
        return Function.of(new Applicable() {
            @Override
            public U apply(T t) {
                return isomorphism.forward(t);
            }
        });
    }

    public Function backwardFunction() {
        return Function.of(new Applicable() {
            @Override
            public T apply(U t) {
                return isomorphism.backward(t);
            }
        });
    }

    private static class IdIsomorphismDefinition implements IsomorphismDefinition {
        @Override
        public T forward(T object) {
            return object;
        }

        @Override
        public T backward(T object) {
            return object;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy