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

moe.maple.api.script.util.tuple.Tuple Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2019, y785, http://github.com/y785
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package moe.maple.api.script.util.tuple;

import moe.maple.api.script.util.ListOf;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public interface Tuple {
    L left();
    R right();

    static  Tuple of(L left, R right) {
        return new ImmutableTuple<>(left, right);
    }

    /**
     * Utility method to perform an action for each Tuple
     * @param tuples An iterable Tuple collection
     * @param action An action to perform for each Tuple
     * @param  Left type
     * @param  Right type
     */
    static  void forEach(Iterable> tuples, BiConsumer action) {
        for(var tuple : tuples) {
            action.accept(tuple.left(), tuple.right());
        }
    }

    static  void flatForEach(Iterable> tuples, Consumer action) {
        for(var tuple : tuples) {
            action.accept(tuple.left());
            action.accept(tuple.right());
        }
    }

    @SafeVarargs
    static  List flatten(Tuple... tuples) {
        List list = new ArrayList<>(tuples.length*2);
        for (int i = 0, tuplesLength = tuples.length; i < tuplesLength; i++) {
            Tuple tuple = tuples[i];
            list.add(tuple.left());
            list.add(tuple.right());
        }
        return list;
    }

    static  List flatten(Collection> tuples) {
        List list = new ArrayList<>(tuples.size()*2);
        for (Tuple tuple : tuples) {
            list.add(tuple.left());
            list.add(tuple.right());
        }
        return list;
    }

    //========================================== Caution: memes below =================================================

    /**
     * Returns a list containing N tuples.
     *
     * @param  the left type
     * @param  the right type
     * @param args varargs to fill the list
     * @return a {@code List} containing the specified elements
     * @throws NullPointerException if either value is {@code null}
     */
    private static  List> unsafeListOf(Object... args) {
        return ListOf.tuples(ImmutableTuple::new, args);
    }

    @SafeVarargs
    static  List> listOf(V... args) {
        return unsafeListOf((Object[]) args);
    }

    static  List> listOf(L l1, R r1, L l2, R r2) {
        return unsafeListOf(l1, r1, l2, r2);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4, L l5, R r5) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4, l5, r5);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4, L l5, R r5, L l6, R r6) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4, l5, r5, l6, r6);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4, L l5, R r5, L l6, R r6, L l7, R r7) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4, l5, r5, l6, r6, l7, r7);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4, L l5, R r5, L l6, R r6, L l7, R r7, L l8, R r8) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4, l5, r5, l6, r6, l7, r7, l8, r8);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4, L l5, R r5, L l6, R r6, L l7, R r7, L l8, R r8, L l9, R r9) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4, l5, r5, l6, r6, l7, r7, l8, r8, l9, r9);
    }

    static  List> listOf(L l1, R r1, L l2, R r2, L l3, R r3, L l4, R r4, L l5, R r5, L l6, R r6, L l7, R r7, L l8, R r8, L l9, R r9, L l10, R r10) {
        return unsafeListOf(l1, r1, l2, r2, l3, r3, l4, r4, l5, r5, l6, r6, l7, r7, l8, r8, l9, r9, l10, r10);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy