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

com.github.andrewoma.dexx.collection.internal.base.AbstractMap Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
/*
 * Copyright (c) 2014 Andrew O'Malley
 *
 * 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 com.github.andrewoma.dexx.collection.internal.base;


import com.github.andrewoma.dexx.collection.Function;
import com.github.andrewoma.dexx.collection.Map;
import com.github.andrewoma.dexx.collection.Pair;
import com.github.andrewoma.dexx.collection.internal.adapter.MapAdapter;
import org.jetbrains.annotations.NotNull;

public abstract class AbstractMap extends AbstractIterable> implements Map {
    @NotNull
    @Override
    public com.github.andrewoma.dexx.collection.Iterable keys() {
        return new MappedIterable>(this, new Function, K>() {
            @Override
            public K invoke(Pair pair) {
                return pair.component1();
            }
        });
    }

    @NotNull
    @Override
    public com.github.andrewoma.dexx.collection.Iterable values() {
        return new MappedIterable>(this, new Function, V>() {
            @Override
            public V invoke(Pair pair) {
                return pair.component2();
            }
        });
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(Object o) {
        if (o == this)
            return true;

        if (!(o instanceof Map))
            return false;

        Map m = (Map) o;
        if (m.size() != size())
            return false;

        try {
            for (Pair pair : this) {
                K key = pair.component1();
                V value = pair.component2();
                if (value == null) {
                    if (m.get(key) != null || !m.containsKey(key))
                        return false;
                } else {
                    if (!value.equals(m.get(key)))
                        return false;
                }
            }
        } catch (ClassCastException e) {
            return false;
        } catch (NullPointerException e) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int h = 0;
        for (Pair pair : this) {
            h += 31 * pair.hashCode();
        }
        return h;
    }

    @NotNull
    @Override
    public java.util.Map asMap() {
        return new MapAdapter(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy