com.mattc.argus2.util.Pairs.java Source code

Java tutorial

Introduction

Here is the source code for com.mattc.argus2.util.Pairs.java

Source

/*
 * Argus Installer v2 -- A Better School Zip Alternative Copyright (C) 2014 Matthew
 * Crocco
 * 
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this
 * program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.mattc.argus2.util;

import java.util.Iterator;
import java.util.Map;

import com.google.common.collect.Lists;
import com.mattc.argus2.misc.Pair;

public final class Pairs {

    public static final <K, V> Iterator<Pair<K, V>> fromMap(final Map<K, V> map) {
        final Iterator<Map.Entry<K, V>> keys = Lists.newArrayList(map.entrySet()).iterator();
        return fromMapIterator(keys);
    }

    public static final <K, V> Iterator<Pair<K, V>> fromMapIterator(final Iterator<Map.Entry<K, V>> iterator) {
        return new Iterator<Pair<K, V>>() {
            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public Pair<K, V> next() {
                return Pairs.of(iterator.next());
            }

            @Override
            public void remove() {
                iterator.remove();
            }
        };
    }

    public static final <K, V> Pair<K, V> of(Map.Entry<K, V> entry) {
        return new Pair<K, V>(entry.getKey(), entry.getValue());
    }

    public static final <K, V> Pair<K, V> of(K first, V second) {
        return new Pair<K, V>(first, second);
    }

}