forked from apache/tinkerpop
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
607 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...re/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/NoneStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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.filter; | ||
|
||
import org.apache.tinkerpop.gremlin.process.traversal.GremlinTypeErrorException; | ||
import org.apache.tinkerpop.gremlin.process.traversal.P; | ||
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.util.StringFactory; | ||
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
public final class NoneStep<S, S2> extends FilterStep<S> { | ||
|
||
private P<S2> predicate; | ||
|
||
public NoneStep(final Traversal.Admin traversal, final P<S2> predicate) { | ||
super(traversal); | ||
|
||
if (null == predicate) { | ||
throw new IllegalArgumentException("Input predicate to none step can't be null."); | ||
} | ||
|
||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
protected boolean filter(final Traverser.Admin<S> traverser) { | ||
final S item = traverser.get(); | ||
|
||
if (item instanceof Iterable || item instanceof Iterator || ((item != null) && item.getClass().isArray())) { | ||
GremlinTypeErrorException typeError = null; | ||
final Iterator<S2> iterator = IteratorUtils.asIterator(item); | ||
while (iterator.hasNext()) { | ||
try { | ||
if (this.predicate.test(iterator.next())) { | ||
return false; | ||
} | ||
} catch (GremlinTypeErrorException gtee) { | ||
// hold onto it until the end in case any other element evaluates to TRUE | ||
typeError = gtee; | ||
} | ||
} | ||
if (typeError != null) throw typeError; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringFactory.stepString(this, this.predicate); | ||
} | ||
|
||
@Override | ||
public NoneStep<S, S2> clone() { | ||
final NoneStep<S, S2> clone = (NoneStep<S, S2>) super.clone(); | ||
clone.predicate = this.predicate.clone(); | ||
return clone; | ||
} | ||
|
||
@Override | ||
public Set<TraverserRequirement> getRequirements() { | ||
return EnumSet.of(TraverserRequirement.OBJECT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...rc/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/NoneStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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.filter; | ||
|
||
import org.apache.tinkerpop.gremlin.process.traversal.P; | ||
import org.apache.tinkerpop.gremlin.process.traversal.TextP; | ||
import org.apache.tinkerpop.gremlin.process.traversal.Traversal; | ||
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; | ||
import org.apache.tinkerpop.gremlin.process.traversal.step.StepTest; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.any; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class NoneStepTest extends StepTest { | ||
|
||
@Override | ||
protected List<Traversal> getTraversals() { return Collections.singletonList(__.none(P.gt(0))); } | ||
|
||
@Test | ||
public void testReturnTypes() { | ||
assertTrue(__.__(new int[]{}).none(P.gt(7)).hasNext()); | ||
assertArrayEquals(new int[] {5, 6}, __.__(new int[] {5, 8, 10}, new int[] {5, 6}).not(any(P.gte(7))).next()); | ||
assertArrayEquals(new long[] {5L, 6L}, __.__(new long[] {5L, 8L, 10L}, new long[] {5L, 6L}).none(P.gte(7)).next()); | ||
assertArrayEquals(new Long[] {5L, 6L}, __.__(1).constant(new Long[] {5L, 6L}).none(P.gte(7)).next()); | ||
assertArrayEquals(new double[] {5.1, 6.5}, __.__(new double[] {5.5, 8.0, 10.1}, new double[] {5.1, 6.5}).none(P.gte(7.0)).next(), 0.01); | ||
} | ||
|
||
@Test | ||
public void testNullParameter() { | ||
final Throwable thrown = assertThrows(IllegalArgumentException.class, () -> __.__(new int[]{1}).none(null).hasNext()); | ||
assertEquals("Input predicate to none step can't be null.", thrown.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testSetTraverser() { | ||
final Set<Integer> numbers = new HashSet<>(); | ||
numbers.add(5); | ||
numbers.add(6); | ||
|
||
assertTrue(__.__(numbers).none(P.gt(7)).hasNext()); | ||
} | ||
|
||
@Test | ||
public void testListIteratorTraverser() { | ||
final List<Integer> numbers = new ArrayList<>(); | ||
numbers.add(10); | ||
numbers.add(11); | ||
|
||
assertTrue(__.__(numbers.iterator()).none(P.lt(10)).hasNext()); | ||
} | ||
|
||
@Test | ||
public void testCornerCases() { | ||
final List validOne = new ArrayList() {{ add(20); }}; | ||
final List validTwo = new ArrayList() {{ add(21); add(25);}}; | ||
final List validThree = new ArrayList() {{ add(51); add(57); add(71); }}; | ||
final List containsNull = new ArrayList() {{ add(50); add(null); add(60); }}; | ||
final List containsNull2 = new ArrayList() {{ add(1); add(null); add(2); }}; | ||
final List empty = new ArrayList(); | ||
final List incorrectType = new ArrayList() {{ add(100); add("25"); }}; | ||
final List valueTooSmall = new ArrayList() {{ add(101); add(1); add(10);}}; | ||
|
||
final List bcd = new ArrayList() {{ add(null); add("bcd"); }}; | ||
final List bcdnull = new ArrayList() {{ add("bcd"); }}; | ||
final List abc = new ArrayList() {{ add("abc"); }}; | ||
final List abcnull = new ArrayList() {{ add("abc"); add(null); }}; | ||
|
||
assertEquals(4L, __.__(validOne, null, containsNull, empty, incorrectType, valueTooSmall, validTwo, validThree) | ||
.none(P.lte(3)).count().next().longValue()); | ||
assertEquals(1L, __.__(bcd).none(TextP.startingWith("a")).count().next().longValue()); // e 0 a 0 | ||
assertEquals(1L, __.__(bcdnull).none(TextP.startingWith("a")).count().next().longValue()); // actual 1 | ||
assertEquals(0L, __.__(abc).none(TextP.startingWith("a")).count().next().longValue()); // actual 0 | ||
assertEquals(0L, __.__(abcnull).none(TextP.startingWith("a")).count().next().longValue()); // actual 0 | ||
|
||
assertEquals(1L, __.__(bcd).all(TextP.startingWith("a")).count().next().longValue()); // actual 0 | ||
assertEquals(1L, __.__(bcdnull).all(TextP.startingWith("a")).count().next().longValue()); // actual 1 | ||
assertEquals(0L, __.__(abcnull).all(TextP.startingWith("a")).count().next().longValue()); // actual 0 | ||
} | ||
} |
Oops, something went wrong.