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

net.hamnaberg.json.codec.Iso Maven / Gradle / Ivy

There is a newer version: 8.0.0
Show newest version
package net.hamnaberg.json.codec;

import java.util.function.Function;

public interface Iso {
    A reverseGet(B b);
    B get(A a);

    default Function modify(Function f) {
        return a -> this.reverseGet(f.apply(this.get(a)));
    }

    default Iso reverse() {
        return from(this::reverseGet, this::get);
    }

    default  Iso compose(Iso iso) {
        Function reverseGet = this::reverseGet;
        Function get = this::get;

        return from(get.andThen(iso::get), reverseGet.compose(iso::reverseGet));
    }

    static  Iso identity() {
        return new IdIso<>();
    }

    static  Iso from(Function getF, Function reverseGetF) {
        return new Iso() {
            @Override
            public A reverseGet(B b) {
                return reverseGetF.apply(b);
            }

            @Override
            public B get(A a) {
                return getF.apply(a);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy