org.neo4j.driver.internal.util.Iterables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-java-driver Show documentation
Show all versions of neo4j-java-driver Show documentation
Access to the Neo4j graph database through Java
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.neo4j.driver.v1.util.Function;
public class Iterables
{
public static int count( Iterable> it )
{
if ( it instanceof Collection ) { return ((Collection) it).size(); }
int size = 0;
for ( Object o : it )
{
size++;
}
return size;
}
public static List asList( Iterable it )
{
if ( it instanceof List ) { return (List) it; }
List list = new ArrayList<>();
for ( T t : it )
{
list.add( t );
}
return list;
}
public static T single( Iterable it )
{
Iterator iterator = it.iterator();
if ( !iterator.hasNext() )
{
throw new IllegalArgumentException( "Given iterable is empty" );
}
T result = iterator.next();
if ( iterator.hasNext() )
{
throw new IllegalArgumentException( "Given iterable contains more than one element: " + it );
}
return result;
}
public static Map map( String ... alternatingKeyValue )
{
Map out = new HashMap<>();
for ( int i = 0; i < alternatingKeyValue.length; i+=2 )
{
out.put( alternatingKeyValue[i], alternatingKeyValue[i+1] );
}
return out;
}
public static Iterable map(final Iterable it, final Function f)
{
return new Iterable()
{
@Override
public Iterator iterator()
{
final Iterator aIterator = it.iterator();
return new Iterator()
{
@Override
public boolean hasNext()
{
return aIterator.hasNext();
}
@Override
public B next()
{
return f.apply( aIterator.next() );
}
@Override
public void remove()
{
aIterator.remove();
}
};
}
};
}
public static Map mapValues( Map map, Function f )
{
HashMap transformed = new HashMap<>( map.size() );
for ( Entry entry : map.entrySet() )
{
transformed.put( entry.getKey(), f.apply( entry.getValue() ) );
}
return transformed;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy