org.neo4j.driver.internal.util.Iterables Maven / Gradle / Ivy
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.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.AbstractQueue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.function.Function;
public class Iterables
{
@SuppressWarnings( "rawtypes" )
private static final Queue EMPTY_QUEUE = new EmptyQueue();
private static final float DEFAULT_HASH_MAP_LOAD_FACTOR = 0.75F;
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 = newHashMapWithSize( alternatingKeyValue.length / 2 );
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();
}
};
}
};
}
@SuppressWarnings( "unchecked" )
public static Queue emptyQueue()
{
return (Queue) EMPTY_QUEUE;
}
public static HashMap newHashMapWithSize( int expectedSize )
{
return new HashMap<>( hashMapCapacity( expectedSize ) );
}
public static LinkedHashMap newLinkedHashMapWithSize( int expectedSize )
{
return new LinkedHashMap<>( hashMapCapacity( expectedSize ) );
}
private static int hashMapCapacity( int expectedSize )
{
if ( expectedSize < 3 )
{
if ( expectedSize < 0 )
{
throw new IllegalArgumentException( "Illegal map size: " + expectedSize );
}
return expectedSize + 1;
}
return (int) ((float) expectedSize / DEFAULT_HASH_MAP_LOAD_FACTOR + 1.0F);
}
private static class EmptyQueue extends AbstractQueue
{
@Override
public Iterator iterator()
{
return Collections.emptyIterator();
}
@Override
public int size()
{
return 0;
}
@Override
public boolean offer( T t )
{
throw new UnsupportedOperationException();
}
@Override
public T poll()
{
return null;
}
@Override
public T peek()
{
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy