Skip to content

Commit

Permalink
Cleanup and clarify usage of empty iterable and empty set
Browse files Browse the repository at this point in the history
Use default available emptySet, use default empty iterator,
clarify usage of empty iterator and empty resource iterator.
  • Loading branch information
MishaDemianenko committed May 9, 2017
1 parent c4fe909 commit 998b3b0
Show file tree
Hide file tree
Showing 34 changed files with 93 additions and 154 deletions.
Expand Up @@ -54,7 +54,7 @@ private Iterables()
@SuppressWarnings( "unchecked" )
public static <T> Iterable<T> empty()
{
return EMPTY;
return Collections.emptyList();
}

public static <T> Iterable<T> limit( final int limitItems, final Iterable<T> iterable )
Expand Down Expand Up @@ -186,7 +186,7 @@ public static <X> Iterable<X> skip( final int skip, final Iterable<X> iterable )
}
else
{
return Iterables.<X>empty().iterator();
return Collections.emptyIterator();
}
}

Expand Down Expand Up @@ -914,33 +914,4 @@ public static <T extends Enum<T>> List<String> enumNames( Class<T> cls )
EnumSet.allOf( cls ).forEach( item -> names.add( item.name() ) );
return names;
}

private static Iterable EMPTY = new Iterable()
{
Iterator iterator = new Iterator()
{
@Override
public boolean hasNext()
{
return false;
}

@Override
public Object next()
{
throw new NoSuchElementException();
}

@Override
public void remove()
{
}
};

@Override
public Iterator iterator()
{
return iterator;
}
};
}
Expand Up @@ -23,7 +23,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -45,12 +44,17 @@
import org.neo4j.graphdb.ResourceIterable;
import org.neo4j.graphdb.ResourceIterator;

import static java.util.Collections.emptyIterator;

/**
* Contains common functionality regarding {@link Iterator}s and
* {@link Iterable}s.
*/
public abstract class Iterators
{
private static final ResourceIterator EMPTY_RESOURCE_ITERATOR =
resourceIterator( emptyIterator(), Resource.EMPTY );

/**
* Returns the given iterator's first element or {@code null} if no
* element found.
Expand Down Expand Up @@ -421,11 +425,6 @@ public static <T> Set<T> asSet( T... items )
return new HashSet<>( Arrays.asList( items ) );
}

public static <T> Set<T> emptySetOf( @SuppressWarnings( "unused"/*just used as a type marker*/ ) Class<T> type )
{
return Collections.emptySet();
}

/**
* Alias for asSet()
*
Expand Down Expand Up @@ -588,9 +587,9 @@ public static <T> Iterator<T> iterator( int maxItems, T ... items )
}

@SuppressWarnings( "unchecked" )
public static <T> ResourceIterator<T> emptyIterator()
public static <T> ResourceIterator<T> emptyResourceIterator()
{
return EMPTY_ITERATOR;
return EMPTY_RESOURCE_ITERATOR;
}

public static <T> boolean contains( Iterator<T> iterator, T item )
Expand Down Expand Up @@ -648,34 +647,6 @@ public static <T> T[] array( T... items )
return items;
}

@SuppressWarnings( "rawtypes" )
private static final ResourceIterator EMPTY_ITERATOR = new ResourceIterator()
{
@Override
public boolean hasNext()
{
return false;
}

@Override
public Object next()
{
throw new NoSuchElementException();
}

@Override
public void remove()
{
throw new UnsupportedOperationException();
}

@Override
public void close()
{
// do nothing
}
};

public static <X> Iterator<X> filter( Predicate<? super X> specification, Iterator<X> i )
{
return new FilterIterable.FilterIterator<>( i, specification );
Expand Down
Expand Up @@ -31,7 +31,8 @@
import org.neo4j.kernel.impl.api.index.updater.SwallowingIndexUpdater;
import org.neo4j.storageengine.api.schema.IndexReader;

import static org.neo4j.helpers.collection.Iterators.emptyIterator;
import static java.util.Collections.emptyIterator;
import static org.neo4j.helpers.collection.Iterators.emptyResourceIterator;

/**
* Used for online operation of an index.
Expand Down Expand Up @@ -163,7 +164,7 @@ public Iterator<Long> iterator()
@Override
public ResourceIterator<File> snapshotFiles()
{
return emptyIterator();
return emptyResourceIterator();
}

@Override
Expand Down
Expand Up @@ -229,7 +229,7 @@ public abstract StoreMigrationParticipant storeMigrationParticipant( FileSystemA
*/
public ResourceIterator<File> snapshotMetaFiles()
{
return Iterators.emptyIterator();
return Iterators.emptyResourceIterator();
}

public static class Descriptor
Expand Down
Expand Up @@ -33,7 +33,7 @@
import org.neo4j.logging.LogProvider;

import static org.neo4j.helpers.FutureAdapter.VOID;
import static org.neo4j.helpers.collection.Iterators.emptyIterator;
import static org.neo4j.helpers.collection.Iterators.emptyResourceIterator;

public class FailedIndexProxy extends AbstractSwallowingIndexProxy
{
Expand Down Expand Up @@ -95,6 +95,6 @@ public void validate() throws IndexPopulationFailedKernelException
@Override
public ResourceIterator<File> snapshotFiles()
{
return emptyIterator();
return emptyResourceIterator();
}
}
Expand Up @@ -38,7 +38,7 @@
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.PopulationProgress;

import static org.neo4j.helpers.collection.Iterators.emptyIterator;
import static org.neo4j.helpers.collection.Iterators.emptyResourceIterator;

public class PopulatingIndexProxy implements IndexProxy
{
Expand Down Expand Up @@ -157,7 +157,7 @@ public void validate()
@Override
public ResourceIterator<File> snapshotFiles()
{
return emptyIterator();
return emptyResourceIterator();
}

@Override
Expand Down
Expand Up @@ -22,7 +22,6 @@
import java.util.Iterator;
import java.util.function.Predicate;

import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.helpers.collection.CombiningIterator;
import org.neo4j.helpers.collection.FilteringIterator;
import org.neo4j.helpers.collection.Iterators;
Expand All @@ -32,12 +31,11 @@
import org.neo4j.storageengine.api.StorageProperty;
import org.neo4j.storageengine.api.txstate.PropertyContainerState;

import static org.neo4j.helpers.collection.Iterators.emptyIterator;
import static java.util.Collections.emptyIterator;

public class PropertyContainerStateImpl implements PropertyContainerState
{
private final long id;
private static final ResourceIterator<StorageProperty> NO_PROPERTIES = emptyIterator();

private VersionedHashMap<Integer, StorageProperty> addedProperties;
private VersionedHashMap<Integer, StorageProperty> changedProperties;
Expand Down Expand Up @@ -145,20 +143,20 @@ public void removeProperty( DefinedProperty property )
@Override
public Iterator<StorageProperty> addedProperties()
{
return addedProperties != null ? addedProperties.values().iterator() : NO_PROPERTIES;
return addedProperties != null ? addedProperties.values().iterator() : emptyIterator();
}

@Override
public Iterator<StorageProperty> changedProperties()
{
return changedProperties != null ? changedProperties.values().iterator() : NO_PROPERTIES;
return changedProperties != null ? changedProperties.values().iterator() : emptyIterator();
}

@Override
public Iterator<Integer> removedProperties()
{
return removedProperties != null ? removedProperties.keySet().iterator()
: Iterators.<Integer>emptyIterator();
: emptyIterator();
}

@Override
Expand All @@ -181,7 +179,7 @@ public Iterator<StorageProperty> addedAndChangedProperties()
out = changedProperties.values().iterator();
}
}
return out != null ? out : NO_PROPERTIES;
return out != null ? out : emptyIterator();
}

@Override
Expand Down
Expand Up @@ -42,6 +42,8 @@
import org.neo4j.kernel.impl.store.record.IndexRule;
import org.neo4j.storageengine.api.schema.SchemaRule;

import static java.util.Collections.emptyIterator;

/**
* A cache of {@link SchemaRule schema rules} as well as enforcement of schema consistency.
* Will always reflect the committed state of the schema store.
Expand Down Expand Up @@ -237,12 +239,12 @@ public IndexDescriptor indexDescriptor( LabelSchemaDescriptor descriptor )
public Iterator<IndexDescriptor> indexDescriptorsForLabel( int labelId )
{
Set<IndexDescriptor> forLabel = indexDescriptorsByLabel.get( labelId );
return forLabel == null ? Iterators.emptyIterator() : forLabel.iterator();
return forLabel == null ? emptyIterator() : forLabel.iterator();
}

public Iterator<IndexDescriptor> indexesByProperty( int propertyId )
{
List<IndexDescriptor> indexes = indexByProperty.get( propertyId );
return (indexes == null) ? Iterators.emptyIterator() : indexes.iterator();
return (indexes == null) ? emptyIterator() : indexes.iterator();
}
}
Expand Up @@ -108,7 +108,7 @@

import static java.lang.String.format;
import static org.neo4j.collection.primitive.PrimitiveLongCollections.map;
import static org.neo4j.helpers.collection.Iterators.emptyIterator;
import static org.neo4j.helpers.collection.Iterators.emptyResourceIterator;
import static org.neo4j.kernel.api.security.SecurityContext.AUTH_DISABLED;
import static org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing.NODE_AUTO_INDEX;
import static org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing.RELATIONSHIP_AUTO_INDEX;
Expand Down Expand Up @@ -593,7 +593,7 @@ private ResourceIterator<Node> nodesByLabelAndProperty( Label myLabel, String ke
if ( propertyId == NO_SUCH_PROPERTY_KEY || labelId == NO_SUCH_LABEL )
{
statement.close();
return emptyIterator();
return emptyResourceIterator();
}

IndexDescriptor descriptor = findAnyIndexByLabelAndProperty( readOps, propertyId, labelId );
Expand Down Expand Up @@ -652,7 +652,7 @@ private ResourceIterator<Node> allNodesWithLabel( final Label myLabel )
if ( labelId == KeyReadOperations.NO_SUCH_LABEL )
{
statement.close();
return emptyIterator();
return emptyResourceIterator();
}

final PrimitiveLongIterator nodeIds = statement.readOperations().nodesGetForLabel( labelId );
Expand Down
Expand Up @@ -26,13 +26,12 @@
import java.util.function.IntPredicate;

import org.neo4j.collection.primitive.PrimitiveLongResourceIterator;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.helpers.collection.Visitor;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.impl.api.index.NodeUpdates;
import org.neo4j.kernel.api.labelscan.NodeLabelUpdate;
import org.neo4j.kernel.impl.api.index.MultipleIndexPopulator;
import org.neo4j.kernel.impl.api.index.NodeUpdates;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.PropertyStore;
Expand All @@ -42,6 +41,7 @@
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.util.Validators;

import static java.util.Collections.emptyIterator;
import static org.neo4j.collection.primitive.PrimitiveLongCollections.EMPTY_LONG_ARRAY;
import static org.neo4j.kernel.api.labelscan.NodeLabelUpdate.labelChanges;
import static org.neo4j.kernel.impl.store.NodeLabelsField.parseLabelsField;
Expand Down Expand Up @@ -161,14 +161,14 @@ public void configure( Collection<MultipleIndexPopulator.IndexPopulation> popula
private class PropertyBlockIterator extends PrefetchingIterator<PropertyBlock>
{
private final Iterator<PropertyRecord> records;
private Iterator<PropertyBlock> blocks = Iterators.emptyIterator();
private Iterator<PropertyBlock> blocks = emptyIterator();

PropertyBlockIterator( NodeRecord node )
{
long firstPropertyId = node.getNextProp();
if ( firstPropertyId == Record.NO_NEXT_PROPERTY.intValue() )
{
records = Iterators.emptyIterator();
records = emptyIterator();
}
else
{
Expand Down
Expand Up @@ -21,10 +21,11 @@

import java.util.Iterator;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.storageengine.api.StorageProperty;

import static java.util.Collections.emptyIterator;

/**
* Represents the property changes to a {@link NodeState node} or {@link RelationshipState relationship}:
* <ul>
Expand Down Expand Up @@ -69,25 +70,25 @@ class EmptyPropertyContainerState implements PropertyContainerState
@Override
public Iterator<StorageProperty> addedProperties()
{
return Iterators.emptyIterator();
return emptyIterator();
}

@Override
public Iterator<StorageProperty> changedProperties()
{
return Iterators.emptyIterator();
return emptyIterator();
}

@Override
public Iterator<Integer> removedProperties()
{
return Iterators.emptyIterator();
return emptyIterator();
}

@Override
public Iterator<StorageProperty> addedAndChangedProperties()
{
return Iterators.emptyIterator();
return emptyIterator();
}

@Override
Expand Down

0 comments on commit 998b3b0

Please sign in to comment.