// Path: src/main/java/org/smallibs/suitcase/cases/Case.java
// public interface Case<T, R> {
// 
//     Optional<R> unapply(T t);
// 
//     interface WithoutCapture<T, R> extends Case<T, Result.WithoutCapture<R>> {
//         static <T, R> WithoutCapture<T, R> adapt(Case<T, Result.WithoutCapture<R>> aCase) {
//             return aCase::unapply;
//         }
//     }
// 
//     interface WithCapture<T, R> extends Case<T, Result.WithCapture<R>> {
//         static <T, R> WithCapture<T, R> adapt(Case<T, Result.WithCapture<R>> aCase) {
//             return aCase::unapply;
//         }
//     }
// 
// }
// 
// Path: src/main/java/org/smallibs/suitcase/cases/Result.java
// public abstract class Result<R> {
// 
//     public static <R> WithoutCapture<R> success(R term) {
//         return new WithoutCapture<>(term);
//     }
// 
//     public static <C> WithCapture<C> successWithCapture(C result) {
//         return new WithCapture<>(result);
//     }
// 
//     private final R capturedObject;
// 
//     public Result(R capturedObject) {
//         this.capturedObject = capturedObject;
//     }
// 
//     public R resultValue() {
//         return capturedObject;
//     }
// 
//     public static class WithCapture<C> extends Result<C> {
//         public WithCapture(C capturedObject) {
//             super(capturedObject);
//         }
//     }
// 
//     public static class WithoutCapture<T> extends Result<T> {
//         public WithoutCapture(T capturedObject) {
//             super(capturedObject);
//         }
//     }
// 
// }
// 
// Path: src/main/java/org/smallibs/suitcase/utils/Pair.java
// public class Pair<T1, T2> {
// 
//     public final T1 _1;
//     public final T2 _2;
// 
//     public Pair(T1 _1, T2 _2) {
//         this._1 = _1;
//         this._2 = _2;
//     }
// }
// 
// Path: src/main/java/org/smallibs/suitcase/cases/core/Cases.java
// static <T> Case.WithoutCapture<T, T> Constant(T value) {
//     return new Case0<T, T>(p -> Objects.deepEquals(p, value) ? Optional.of(p) : Optional.empty()).$();
// }


import org.smallibs.suitcase.cases.Case;
import org.smallibs.suitcase.cases.Result;
import org.smallibs.suitcase.utils.Pair;
import java.util.Map;
import static org.smallibs.suitcase.cases.core.Cases.Constant;
/*
 * Copyright (C)2015 D. Plaindoux.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.smallibs.suitcase.cases.lang;




public interface Maps {

    static <T1, T2> Case.WithoutCapture<Map<T1, T2>, Pair<T1, T2>> Entry(T1 o1, T2 o2) {
        return Entry(Constant(o1), Constant(o2));
    }

    static <T1, T2, C2> Case.WithoutCapture<Map<T1, T2>, Pair<T1, C2>> Entry(T1 o1, Case.WithoutCapture<T2, C2> o2) {
        return Entry(Constant(o1), o2);
    }

    static <T1, T2, C1> Case.WithoutCapture<Map<T1, T2>, Pair<C1, T2>> Entry(Case.WithoutCapture<T1, C1> o1, T2 o2) {
        return Entry(o1, Constant(o2));
    }

    static <T1, T2, C2> Case.WithCapture<Map<T1, T2>, C2> Entry(T1 o1, Case.WithCapture<T2, C2> o2) {
        return Entry(Constant(o1), o2);
    }

    static <T1, T2, C1> Case.WithCapture<Map<T1, T2>, C1> Entry(Case.WithCapture<T1, C1> o1, T2 o2) {
        return Entry(o1, Constant(o2));
    }

    static <T1, T2, C1, C2> Case.WithoutCapture<Map<T1, T2>, Pair<C1, C2>> Entry(Case.WithoutCapture<T1, C1> o1, Case.WithoutCapture<T2, C2> o2) {
        return map -> map.keySet().stream().filter(k -> o1.unapply(k).isPresent()).findFirst().
                flatMap(k -> o2.unapply(map.get(k)).map(c2 -> Result.success(new Pair<>(o1.unapply(k).get().resultValue(), c2.resultValue()))));
    }

    static <T1, T2, C1, C2> Case.WithCapture<Map<T1, T2>, C1> Entry(Case.WithCapture<T1, C1> o1, Case.WithoutCapture<T2, C2> o2) {
        return map -> map.keySet().stream().filter(k -> o1.unapply(k).isPresent()).findFirst().
                flatMap(k -> o2.unapply(map.get(k)).flatMap(c2 -> o1.unapply(k)));
    }

    static <T1, T2, C1, C2> Case.WithCapture<Map<T1, T2>, C2> Entry(Case.WithoutCapture<T1, C1> o1, Case.WithCapture<T2, C2> o2) {
        return map -> map.keySet().stream().filter(k -> o1.unapply(k).isPresent()).findFirst().
// Path: src/main/java/org/smallibs/suitcase/cases/Case.java
// public interface Case<T, R> {
// 
//     Optional<R> unapply(T t);
// 
//     interface WithoutCapture<T, R> extends Case<T, Result.WithoutCapture<R>> {
//         static <T, R> WithoutCapture<T, R> adapt(Case<T, Result.WithoutCapture<R>> aCase) {
//             return aCase::unapply;
//         }
//     }
// 
//     interface WithCapture<T, R> extends Case<T, Result.WithCapture<R>> {
//         static <T, R> WithCapture<T, R> adapt(Case<T, Result.WithCapture<R>> aCase) {
//             return aCase::unapply;
//         }
//     }
// 
// }
// 
// Path: src/main/java/org/smallibs/suitcase/cases/Result.java
// public abstract class Result<R> {
// 
//     public static <R> WithoutCapture<R> success(R term) {
//         return new WithoutCapture<>(term);
//     }
// 
//     public static <C> WithCapture<C> successWithCapture(C result) {
//         return new WithCapture<>(result);
//     }
// 
//     private final R capturedObject;
// 
//     public Result(R capturedObject) {
//         this.capturedObject = capturedObject;
//     }
// 
//     public R resultValue() {
//         return capturedObject;
//     }
// 
//     public static class WithCapture<C> extends Result<C> {
//         public WithCapture(C capturedObject) {
//             super(capturedObject);
//         }
//     }
// 
//     public static class WithoutCapture<T> extends Result<T> {
//         public WithoutCapture(T capturedObject) {
//             super(capturedObject);
//         }
//     }
// 
// }
// 
// Path: src/main/java/org/smallibs/suitcase/utils/Pair.java
// public class Pair<T1, T2> {
// 
//     public final T1 _1;
//     public final T2 _2;
// 
//     public Pair(T1 _1, T2 _2) {
//         this._1 = _1;
//         this._2 = _2;
//     }
// }
// 
// Path: src/main/java/org/smallibs/suitcase/cases/core/Cases.java
// static <T> Case.WithoutCapture<T, T> Constant(T value) {
//     return new Case0<T, T>(p -> Objects.deepEquals(p, value) ? Optional.of(p) : Optional.empty()).$();
// }


// Path: src/main/java/org/smallibs/suitcase/cases/lang/Maps.java
import org.smallibs.suitcase.cases.Case;
import org.smallibs.suitcase.cases.Result;
import org.smallibs.suitcase.utils.Pair;
import java.util.Map;
import static org.smallibs.suitcase.cases.core.Cases.Constant;

/*
 * Copyright (C)2015 D. Plaindoux.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.smallibs.suitcase.cases.lang;




public interface Maps {

    static <T1, T2> Case.WithoutCapture<Map<T1, T2>, Pair<T1, T2>> Entry(T1 o1, T2 o2) {
        return Entry(Constant(o1), Constant(o2));
    }

    static <T1, T2, C2> Case.WithoutCapture<Map<T1, T2>, Pair<T1, C2>> Entry(T1 o1, Case.WithoutCapture<T2, C2> o2) {
        return Entry(Constant(o1), o2);
    }

    static <T1, T2, C1> Case.WithoutCapture<Map<T1, T2>, Pair<C1, T2>> Entry(Case.WithoutCapture<T1, C1> o1, T2 o2) {
        return Entry(o1, Constant(o2));
    }

    static <T1, T2, C2> Case.WithCapture<Map<T1, T2>, C2> Entry(T1 o1, Case.WithCapture<T2, C2> o2) {
        return Entry(Constant(o1), o2);
    }

    static <T1, T2, C1> Case.WithCapture<Map<T1, T2>, C1> Entry(Case.WithCapture<T1, C1> o1, T2 o2) {
        return Entry(o1, Constant(o2));
    }

    static <T1, T2, C1, C2> Case.WithoutCapture<Map<T1, T2>, Pair<C1, C2>> Entry(Case.WithoutCapture<T1, C1> o1, Case.WithoutCapture<T2, C2> o2) {
        return map -> map.keySet().stream().filter(k -> o1.unapply(k).isPresent()).findFirst().
                flatMap(k -> o2.unapply(map.get(k)).map(c2 -> Result.success(new Pair<>(o1.unapply(k).get().resultValue(), c2.resultValue()))));
    }

    static <T1, T2, C1, C2> Case.WithCapture<Map<T1, T2>, C1> Entry(Case.WithCapture<T1, C1> o1, Case.WithoutCapture<T2, C2> o2) {
        return map -> map.keySet().stream().filter(k -> o1.unapply(k).isPresent()).findFirst().
                flatMap(k -> o2.unapply(map.get(k)).flatMap(c2 -> o1.unapply(k)));
    }

    static <T1, T2, C1, C2> Case.WithCapture<Map<T1, T2>, C2> Entry(Case.WithoutCapture<T1, C1> o1, Case.WithCapture<T2, C2> o2) {
        return map -> map.keySet().stream().filter(k -> o1.unapply(k).isPresent()).findFirst().
                flatMap(k -> o2.unapply(map.get(k)));