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

com.epam.commons.pairs.Pairs Maven / Gradle / Ivy

There is a newer version: 1.1.34
Show newest version
package com.epam.commons.pairs;
/*
 * Copyright 2004-2016 EPAM Systems
 *
 * This file is part of JDI project.
 *
 * JDI 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 3 of the License, or
 * (at your option) any later version.
 *
 * JDI 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 JDI. If not, see .
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import static com.epam.commons.LinqUtils.select;
import static com.epam.commons.PrintUtils.print;

/**
 * Created by 12345 on 30.09.2014.
 */
public class Pairs extends ArrayList> {
    public Pairs() {
    }

    public Pairs(List> pairs) {
        if (pairs == null) return;
        pairs.forEach(this::add);
    }

    public Pairs(TValue1 value1, TValue2 value2, Collection> pairs) {
        if (pairs != null)
            pairs.forEach(this::add);
        add(value1, value2);
    }

    public static  Pairs toPairs(Iterable list,
                Function selectorValue1, Function selectorValue2) {
        Pairs pairs = new Pairs<>();
        for (T element : list)
            pairs.add(selectorValue1.apply(element), selectorValue2.apply(element));
        return pairs;
    }

    public Pairs add(TValue1 value1, TValue2 value2) {
        this.add(new Pair(value1, value2));
        return this;
    }

    public Pairs add(Pairs pairs) {
        pairs.foreach(this::add);
        return this;
    }

    public void addNew(TValue1 value1, TValue2 value2) {
        clear();
        add(new Pair(value1, value2));
    }

    public void foreach(Consumer> action) {
        this.forEach(action::accept);
    }

    public Pairs subList(int from) {
        return new Pairs<>(subList(from, size()));
    }

    @Override
    public String toString() {
        return print(select(this, pair -> pair.key + ":" + pair.value));
    }

    @Override
    public Pairs clone() {
        return new Pairs<>(this);
    }

    public Pairs copy() {
        return clone();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy