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

org.eclipse.sisu.wire.EntryMapAdapter Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-present Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.sisu.wire;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * {@link Map} backed by an {@link Iterable} sequence of map entries.
 */
public final class EntryMapAdapter
    extends AbstractMap
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    private final Set> entrySet;

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    public EntryMapAdapter( final Iterable> iterable )
    {
        entrySet = new EntrySet( iterable );
    }

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    @Override
    public Set> entrySet()
    {
        return entrySet;
    }

    @Override
    public boolean isEmpty()
    {
        return entrySet.isEmpty();
    }

    // ----------------------------------------------------------------------
    // Implementation types
    // ----------------------------------------------------------------------

    /**
     * Entry {@link Set} backed by an {@link Iterable} sequence of map entries.
     */
    private static final class EntrySet
        extends AbstractSet>
    {
        // ----------------------------------------------------------------------
        // Implementation fields
        // ----------------------------------------------------------------------

        private final Iterable> iterable;

        // ----------------------------------------------------------------------
        // Constructors
        // ----------------------------------------------------------------------

        @SuppressWarnings( "unchecked" )
        EntrySet( final Iterable> iterable )
        {
            this.iterable = (Iterable>) iterable;
        }

        // ----------------------------------------------------------------------
        // Public methods
        // ----------------------------------------------------------------------

        @Override
        public Iterator> iterator()
        {
            return iterable.iterator();
        }

        @Override
        public boolean isEmpty()
        {
            return false == iterator().hasNext();
        }

        @Override
        public int size()
        {
            int size = 0;
            for ( final Iterator i = iterable.iterator(); i.hasNext(); i.next() )
            {
                size++;
            }
            return size;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy