-
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.
test: SimpleCondition
- Loading branch information
Showing
15 changed files
with
651 additions
and
25 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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/github/sepgh/testudo/operation/query/CompositeCondition.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,47 @@ | ||
package com.github.sepgh.testudo.operation.query; | ||
|
||
import com.github.sepgh.testudo.operation.CollectionIndexProvider; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class CompositeCondition implements Condition { | ||
private final CompositeOperator operator; | ||
private final List<Condition> conditions = new ArrayList<>(); | ||
private final List<CompositeCondition> compositeConditions = new ArrayList<>(); | ||
|
||
public CompositeCondition(CompositeOperator operator, Condition... initialConditions) { | ||
this.operator = operator; | ||
for (Condition condition : initialConditions) { | ||
addCondition(condition); | ||
} | ||
} | ||
|
||
public CompositeCondition(CompositeOperator operator, CompositeCondition left, Condition right) { | ||
this.operator = operator; | ||
this.compositeConditions.add(left); | ||
this.conditions.add(right); | ||
} | ||
|
||
public void addCondition(Condition condition) { | ||
conditions.add(condition); | ||
} | ||
|
||
@Override | ||
public <V extends Number & Comparable<V>> Iterator<V> evaluate(CollectionIndexProvider collectionIndexProvider, Order order) { | ||
List<Iterator<V>> iterators = conditions.stream() | ||
.map(cond -> (Iterator<V>) cond.evaluate(collectionIndexProvider, order)) | ||
.collect(Collectors.toList()); | ||
return new CompositeConditionIterator<>(operator, iterators, order); } | ||
|
||
@Override | ||
public String getField() { | ||
throw new UnsupportedOperationException("Composite conditions do not have a single field"); | ||
} | ||
|
||
public enum CompositeOperator { | ||
AND, OR; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/com/github/sepgh/testudo/operation/query/CompositeConditionIterator.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,109 @@ | ||
package com.github.sepgh.testudo.operation.query; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.PriorityQueue; | ||
|
||
public class CompositeConditionIterator<T extends Comparable<T>> implements Iterator<T> { | ||
private final CompositeCondition.CompositeOperator operator; | ||
private final PriorityQueue<PeekableIterator<T>> iterators; | ||
private T nextItem; | ||
|
||
CompositeConditionIterator(CompositeCondition.CompositeOperator operator, List<Iterator<T>> iterators, Order order) { | ||
this.operator = operator; | ||
this.iterators = new PriorityQueue<>((a, b) -> order.equals(Order.ASC) ? | ||
a.peek().compareTo(b.peek()) : b.peek().compareTo(a.peek())); | ||
|
||
// Initialize the priority queue with each iterator's first element | ||
for (Iterator<T> it : iterators) { | ||
if (it.hasNext()) { | ||
this.iterators.add(new PeekableIterator<>(it)); | ||
} | ||
} | ||
advance(); | ||
} | ||
|
||
private void advance() { | ||
if (operator == CompositeCondition.CompositeOperator.AND) { | ||
advanceForAnd(); | ||
} else { | ||
advanceForOr(); | ||
} | ||
} | ||
|
||
private void advanceForAnd() { | ||
while (!iterators.isEmpty()) { | ||
T candidate = iterators.peek().peek(); | ||
boolean allMatch = true; | ||
|
||
for (PeekableIterator<T> it : iterators) { | ||
while (it.hasNext() && !it.peek().equals(candidate)) { | ||
it.next(); | ||
} | ||
if (!it.hasNext() || !it.peek().equals(candidate)) { | ||
allMatch = false; | ||
break; | ||
} | ||
} | ||
|
||
if (allMatch) { | ||
nextItem = candidate; | ||
iterators.forEach(PeekableIterator::next); | ||
return; | ||
} | ||
} | ||
nextItem = null; | ||
} | ||
|
||
private void advanceForOr() { | ||
if (!iterators.isEmpty()) { | ||
PeekableIterator<T> smallest = iterators.poll(); | ||
nextItem = smallest.next(); | ||
if (smallest.hasNext()) { | ||
iterators.add(smallest); | ||
} | ||
} else { | ||
nextItem = null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return nextItem != null; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
T result = nextItem; | ||
advance(); | ||
return result; | ||
} | ||
|
||
|
||
// PeekableIterator class for reference | ||
private static class PeekableIterator<T> implements Iterator<T> { | ||
private final Iterator<T> iterator; | ||
private T next; | ||
|
||
public PeekableIterator(Iterator<T> iterator) { | ||
this.iterator = iterator; | ||
if (iterator.hasNext()) next = iterator.next(); | ||
} | ||
|
||
public T peek() { | ||
return next; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return next != null; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
T current = next; | ||
next = iterator.hasNext() ? iterator.next() : null; | ||
return current; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/sepgh/testudo/operation/query/Condition.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,10 @@ | ||
package com.github.sepgh.testudo.operation.query; | ||
|
||
import com.github.sepgh.testudo.operation.CollectionIndexProvider; | ||
|
||
import java.util.Iterator; | ||
|
||
public interface Condition { | ||
<V extends Number & Comparable<V>> Iterator<V> evaluate(CollectionIndexProvider collectionIndexProvider, Order order); | ||
String getField(); | ||
} |
Oops, something went wrong.