org.eclipse.sisu.wire.EntryMapAdapter Maven / Gradle / Ivy
/*******************************************************************************
* 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;
}
}
}