TINKERPOP-1589 Re-introduced CloseableIterator https://issues.apache.org/jira/browse/TINKERPOP-1589 Add support for the closing of Iterators returned from `Vertex.vertices()` and `Vertex.edges()`. Make `FlatMapStep` close iterator after read to completion. Make `EdgeVertexStep`, `PropertiesStep`, `VertexStep` `AutoCloseable` in case iterator is not read to completion to ensure closed when Traversal is closed. OLTP mode support only. More extensive changes required for OLAP. Add unchecked `close()` helper method to `CloseableIterator`
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/25232ad5 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/25232ad5 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/25232ad5 Branch: refs/heads/TINKERPOP-1599 Commit: 25232ad5fe4b6036962c246b11b50191a4244981 Parents: c2d183a Author: PaulJackson123 <[email protected]> Authored: Fri Jan 27 18:28:26 2017 -0500 Committer: PaulJackson123 <[email protected]> Committed: Fri Jan 27 18:28:26 2017 -0500 ---------------------------------------------------------------------- .../traversal/step/map/EdgeVertexStep.java | 149 +++++++------- .../process/traversal/step/map/FlatMapStep.java | 25 +-- .../traversal/step/map/PropertiesStep.java | 165 +++++++-------- .../process/traversal/step/map/VertexStep.java | 205 ++++++++++--------- .../structure/util/CloseableIterator.java | 109 +++++----- 5 files changed, 333 insertions(+), 320 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/25232ad5/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/EdgeVertexStep.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/EdgeVertexStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/EdgeVertexStep.java index df20587..1851d97 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/EdgeVertexStep.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/EdgeVertexStep.java @@ -1,72 +1,77 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.tinkerpop.gremlin.process.traversal.step.map; - -import org.apache.tinkerpop.gremlin.process.traversal.Traversal; -import org.apache.tinkerpop.gremlin.process.traversal.Traverser; -import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.util.StringFactory; - -import java.util.Collections; -import java.util.Iterator; -import java.util.Set; - -/** - * @author Marko A. Rodriguez (http://markorodriguez.com) - */ -public final class EdgeVertexStep extends FlatMapStep<Edge, Vertex> { - - private Direction direction; - - public EdgeVertexStep(final Traversal.Admin traversal, final Direction direction) { - super(traversal); - this.direction = direction; - } - - @Override - protected Iterator<Vertex> flatMap(final Traverser.Admin<Edge> traverser) { - return traverser.get().vertices(this.direction); - } - - @Override - public String toString() { - return StringFactory.stepString(this, this.direction); - } - - @Override - public int hashCode() { - return super.hashCode() ^ this.direction.hashCode(); - } - - public Direction getDirection() { - return this.direction; - } - - public void reverseDirection() { - this.direction = this.direction.opposite(); - } - - @Override - public Set<TraverserRequirement> getRequirements() { - return Collections.singleton(TraverserRequirement.OBJECT); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tinkerpop.gremlin.process.traversal.step.map; + +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.Traverser; +import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.util.StringFactory; + +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +public final class EdgeVertexStep extends FlatMapStep<Edge, Vertex> implements AutoCloseable { + + private Direction direction; + + public EdgeVertexStep(final Traversal.Admin traversal, final Direction direction) { + super(traversal); + this.direction = direction; + } + + @Override + protected Iterator<Vertex> flatMap(final Traverser.Admin<Edge> traverser) { + return traverser.get().vertices(this.direction); + } + + @Override + public String toString() { + return StringFactory.stepString(this, this.direction); + } + + @Override + public int hashCode() { + return super.hashCode() ^ this.direction.hashCode(); + } + + public Direction getDirection() { + return this.direction; + } + + public void reverseDirection() { + this.direction = this.direction.opposite(); + } + + @Override + public Set<TraverserRequirement> getRequirements() { + return Collections.singleton(TraverserRequirement.OBJECT); + } + + @Override + public void close() { + closeIterator(); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/25232ad5/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java index ce79bdf..42f6ecd 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java @@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.process.traversal.step.map; import org.apache.tinkerpop.gremlin.process.traversal.Traversal; import org.apache.tinkerpop.gremlin.process.traversal.Traverser; import org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep; +import org.apache.tinkerpop.gremlin.structure.util.CloseableIterator; import org.apache.tinkerpop.gremlin.util.iterator.EmptyIterator; import java.util.Iterator; @@ -28,10 +29,10 @@ import java.util.Iterator; /** * @author Marko A. Rodriguez (http://markorodriguez.com) */ -public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> implements AutoCloseable { +public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> { private Traverser.Admin<S> head = null; - private Iterator<E> iterator = EmptyIterator.instance(); + protected Iterator<E> iterator = EmptyIterator.instance(); public FlatMapStep(final Traversal.Admin traversal) { super(traversal); @@ -56,24 +57,10 @@ public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> implements Au public void reset() { super.reset(); closeIterator(); + this.iterator = EmptyIterator.instance(); } - @Override - public void close() { - closeIterator(); - } - - private void closeIterator() { - try { - if (this.iterator instanceof AutoCloseable) { - ((AutoCloseable) this.iterator).close(); - } - } - catch (Exception e) { - throw new RuntimeException(e); - } - finally { - this.iterator = EmptyIterator.instance(); - } + protected void closeIterator() { + CloseableIterator.closeIterator(this.iterator); } } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/25232ad5/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertiesStep.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertiesStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertiesStep.java index d91ec36..b418a7c 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertiesStep.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertiesStep.java @@ -1,80 +1,85 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.tinkerpop.gremlin.process.traversal.step.map; - -import org.apache.tinkerpop.gremlin.process.traversal.Traversal; -import org.apache.tinkerpop.gremlin.process.traversal.Traverser; -import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; -import org.apache.tinkerpop.gremlin.structure.Element; -import org.apache.tinkerpop.gremlin.structure.PropertyType; -import org.apache.tinkerpop.gremlin.structure.util.StringFactory; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.Set; - -/** - * @author Marko A. Rodriguez (http://markorodriguez.com) - */ -public class PropertiesStep<E> extends FlatMapStep<Element, E> { - - protected final String[] propertyKeys; - protected final PropertyType returnType; - - public PropertiesStep(final Traversal.Admin traversal, final PropertyType propertyType, final String... propertyKeys) { - super(traversal); - this.returnType = propertyType; - this.propertyKeys = propertyKeys; - } - - @Override - protected Iterator<E> flatMap(final Traverser.Admin<Element> traverser) { - return this.returnType.equals(PropertyType.VALUE) ? - traverser.get().values(this.propertyKeys) : - (Iterator) traverser.get().properties(this.propertyKeys); - } - - public PropertyType getReturnType() { - return this.returnType; - } - - public String[] getPropertyKeys() { - return this.propertyKeys; - } - - @Override - public String toString() { - return StringFactory.stepString(this, Arrays.asList(this.propertyKeys), this.returnType.name().toLowerCase()); - } - - @Override - public int hashCode() { - int result = super.hashCode() ^ this.returnType.hashCode(); - for (final String propertyKey : this.propertyKeys) { - result ^= propertyKey.hashCode(); - } - return result; - } - - @Override - public Set<TraverserRequirement> getRequirements() { - return Collections.singleton(TraverserRequirement.OBJECT); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tinkerpop.gremlin.process.traversal.step.map; + +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.Traverser; +import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; +import org.apache.tinkerpop.gremlin.structure.Element; +import org.apache.tinkerpop.gremlin.structure.PropertyType; +import org.apache.tinkerpop.gremlin.structure.util.StringFactory; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +public class PropertiesStep<E> extends FlatMapStep<Element, E> implements AutoCloseable { + + protected final String[] propertyKeys; + protected final PropertyType returnType; + + public PropertiesStep(final Traversal.Admin traversal, final PropertyType propertyType, final String... propertyKeys) { + super(traversal); + this.returnType = propertyType; + this.propertyKeys = propertyKeys; + } + + @Override + protected Iterator<E> flatMap(final Traverser.Admin<Element> traverser) { + return this.returnType.equals(PropertyType.VALUE) ? + traverser.get().values(this.propertyKeys) : + (Iterator) traverser.get().properties(this.propertyKeys); + } + + public PropertyType getReturnType() { + return this.returnType; + } + + public String[] getPropertyKeys() { + return this.propertyKeys; + } + + @Override + public String toString() { + return StringFactory.stepString(this, Arrays.asList(this.propertyKeys), this.returnType.name().toLowerCase()); + } + + @Override + public int hashCode() { + int result = super.hashCode() ^ this.returnType.hashCode(); + for (final String propertyKey : this.propertyKeys) { + result ^= propertyKey.hashCode(); + } + return result; + } + + @Override + public Set<TraverserRequirement> getRequirements() { + return Collections.singleton(TraverserRequirement.OBJECT); + } + + @Override + public void close() { + closeIterator(); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/25232ad5/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexStep.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexStep.java index 9e24953..882c519 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexStep.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexStep.java @@ -1,100 +1,105 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.tinkerpop.gremlin.process.traversal.step.map; - -import org.apache.tinkerpop.gremlin.process.traversal.Traversal; -import org.apache.tinkerpop.gremlin.process.traversal.Traverser; -import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Element; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.util.StringFactory; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.Set; - -/** - * @author Marko A. Rodriguez (http://markorodriguez.com) - */ -public class VertexStep<E extends Element> extends FlatMapStep<Vertex, E> { - - private final String[] edgeLabels; - private Direction direction; - private final Class<E> returnClass; - - public VertexStep(final Traversal.Admin traversal, final Class<E> returnClass, final Direction direction, final String... edgeLabels) { - super(traversal); - this.direction = direction; - this.edgeLabels = edgeLabels; - this.returnClass = returnClass; - } - - @Override - protected Iterator<E> flatMap(final Traverser.Admin<Vertex> traverser) { - return Vertex.class.isAssignableFrom(this.returnClass) ? - (Iterator<E>) traverser.get().vertices(this.direction, this.edgeLabels) : - (Iterator<E>) traverser.get().edges(this.direction, this.edgeLabels); - } - - public Direction getDirection() { - return this.direction; - } - - public String[] getEdgeLabels() { - return this.edgeLabels; - } - - public Class<E> getReturnClass() { - return this.returnClass; - } - - public void reverseDirection() { - this.direction = this.direction.opposite(); - } - - public boolean returnsVertex() { - return this.returnClass.equals(Vertex.class); - } - - public boolean returnsEdge() { - return this.returnClass.equals(Edge.class); - } - - @Override - public String toString() { - return StringFactory.stepString(this, this.direction, Arrays.asList(this.edgeLabels), this.returnClass.getSimpleName().toLowerCase()); - } - - @Override - public int hashCode() { - int result = super.hashCode() ^ this.direction.hashCode() ^ this.returnClass.hashCode(); - for (final String edgeLabel : this.edgeLabels) { - result ^= edgeLabel.hashCode(); - } - return result; - } - - @Override - public Set<TraverserRequirement> getRequirements() { - return Collections.singleton(TraverserRequirement.OBJECT); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tinkerpop.gremlin.process.traversal.step.map; + +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.Traverser; +import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Element; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.util.StringFactory; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +public class VertexStep<E extends Element> extends FlatMapStep<Vertex, E> implements AutoCloseable { + + private final String[] edgeLabels; + private Direction direction; + private final Class<E> returnClass; + + public VertexStep(final Traversal.Admin traversal, final Class<E> returnClass, final Direction direction, final String... edgeLabels) { + super(traversal); + this.direction = direction; + this.edgeLabels = edgeLabels; + this.returnClass = returnClass; + } + + @Override + protected Iterator<E> flatMap(final Traverser.Admin<Vertex> traverser) { + return Vertex.class.isAssignableFrom(this.returnClass) ? + (Iterator<E>) traverser.get().vertices(this.direction, this.edgeLabels) : + (Iterator<E>) traverser.get().edges(this.direction, this.edgeLabels); + } + + public Direction getDirection() { + return this.direction; + } + + public String[] getEdgeLabels() { + return this.edgeLabels; + } + + public Class<E> getReturnClass() { + return this.returnClass; + } + + public void reverseDirection() { + this.direction = this.direction.opposite(); + } + + public boolean returnsVertex() { + return this.returnClass.equals(Vertex.class); + } + + public boolean returnsEdge() { + return this.returnClass.equals(Edge.class); + } + + @Override + public String toString() { + return StringFactory.stepString(this, this.direction, Arrays.asList(this.edgeLabels), this.returnClass.getSimpleName().toLowerCase()); + } + + @Override + public int hashCode() { + int result = super.hashCode() ^ this.direction.hashCode() ^ this.returnClass.hashCode(); + for (final String edgeLabel : this.edgeLabels) { + result ^= edgeLabel.hashCode(); + } + return result; + } + + @Override + public Set<TraverserRequirement> getRequirements() { + return Collections.singleton(TraverserRequirement.OBJECT); + } + + @Override + public void close() { + closeIterator(); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/25232ad5/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/CloseableIterator.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/CloseableIterator.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/CloseableIterator.java index 5f334a5..6999a12 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/CloseableIterator.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/CloseableIterator.java @@ -1,49 +1,60 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.tinkerpop.gremlin.structure.util; - -import org.apache.tinkerpop.gremlin.structure.Graph; - -import java.io.Closeable; -import java.util.Iterator; - -/** - * An extension of {@code Iterator} that implements {@code Closeable} which allows a {@link Graph} implementation - * that hold open resources to provide the user the option to release those resources. - * - * @author Stephen Mallette (http://stephen.genoprime.com) - */ -public interface CloseableIterator<T> extends Iterator<T>, Closeable { - - /** - * Wraps an existing {@code Iterator} in a {@code CloseableIterator}. If the {@code Iterator} is already of that - * type then it will simply be returned as-is. - */ - public static <T> CloseableIterator<T> asCloseable(final Iterator<T> iterator) { - if (iterator instanceof CloseableIterator) - return (CloseableIterator<T>) iterator; - - return new DefaultCloseableIterator<T>(iterator); - } - - @Override - public default void close() { - // do nothing by default - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tinkerpop.gremlin.structure.util; + +import org.apache.tinkerpop.gremlin.structure.Graph; + +import java.io.Closeable; +import java.util.Iterator; + +/** + * An extension of {@code Iterator} that implements {@code Closeable} which allows a {@link Graph} implementation + * that hold open resources to provide the user the option to release those resources. + * + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public interface CloseableIterator<T> extends Iterator<T>, Closeable { + + /** + * Wraps an existing {@code Iterator} in a {@code CloseableIterator}. If the {@code Iterator} is already of that + * type then it will simply be returned as-is. + */ + public static <T> CloseableIterator<T> asCloseable(final Iterator<T> iterator) { + if (iterator instanceof CloseableIterator) + return (CloseableIterator<T>) iterator; + + return new DefaultCloseableIterator<T>(iterator); + } + + @Override + public default void close() { + // do nothing by default + } + + public static <T> void closeIterator(Iterator<T> iterator) { + if (iterator instanceof AutoCloseable) { + try { + ((AutoCloseable) iterator).close(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + } +}
