Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notComplement() #79

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/org/jinq/orm/stream/JinqStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,5 +758,5 @@ public static <U> JinqStream<U> from(Collection<U> collection)
public static <U> JinqStream<U> of(U value)
{
return new NonQueryJinqStream<>(Stream.of(value));
}
}
}
4 changes: 2 additions & 2 deletions api/src/org/jinq/orm/stream/NonQueryJinqStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public NonQueryJinqStream(Stream<T> wrapped)
public NonQueryJinqStream(Stream<T> wrapped, InQueryStreamSource inQueryStreamSource)
{
super(wrapped);
this.inQueryStreamSource = inQueryStreamSource;
this.inQueryStreamSource = inQueryStreamSource;
}

NonQueryJinqStream()
Expand Down Expand Up @@ -689,5 +689,5 @@ protected void generateNext()
public JinqStream<T> setHint(String name, Object value)
{
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import java.util.Optional;
import java.util.function.Consumer;




import org.hibernate.Query;
import org.hibernate.Session;
import org.jinq.jpa.JPAJinqStream;
Expand Down Expand Up @@ -39,6 +36,7 @@
import org.jinq.jpa.transform.LimitSkipTransform;
import org.jinq.jpa.transform.MetamodelUtil;
import org.jinq.jpa.transform.MultiAggregateTransform;
import org.jinq.jpa.transform.NotTransform;
import org.jinq.jpa.transform.OuterJoinOnTransform;
import org.jinq.jpa.transform.OuterJoinTransform;
import org.jinq.jpa.transform.QueryTransformException;
Expand Down Expand Up @@ -601,6 +599,18 @@ public QueryComposer<T> andIntersect(JPAJinqStream<T> otherSet)
return applyTransformWithTwoQueryMerge(new SetOperationEmulationTransform(getConfig(), SetOperationEmulationTransform.SetOperationType.AND_INTERSECT), otherSet);
}

public QueryComposer<T> notComplement()
{
return applyTransformWithLambda(new NotTransform(getConfig()));
}


public QueryComposer<T> difference(JPAJinqStream<T> otherSet)
{
return applyTransformWithTwoQueryMerge(new SetOperationEmulationTransform(getConfig(), SetOperationEmulationTransform.SetOperationType.DIFFERENCE), otherSet);
}


@Override
public Long count()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jinq.hibernate;

import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
Expand All @@ -11,6 +12,7 @@
import org.jinq.jpa.JPAJinqStream;
import org.jinq.orm.stream.JinqStream;
import org.jinq.orm.stream.LazyWrappedStream;
import org.jinq.orm.stream.NonQueryJinqStream;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple3;
import org.jinq.tuples.Tuple4;
Expand Down Expand Up @@ -435,4 +437,24 @@ public JPAJinqStream<T> andIntersect(JPAJinqStream<T> otherSet)
Set<T> saved = collect(Collectors.toSet());
return wrap(JinqStream.from(otherSet.filter(el -> saved.contains(el)).collect(Collectors.toSet())));
}

@Override
public JPAJinqStream<T> notComplement()
{
//Set<T> all = ?
//Set<T> saved = wrapped.collect(Collectors.toSet());
//return wrap( JinqStream.from(all).where( el -> !saved.contains(el)) );

throw new UnsupportedOperationException("operation not supported for this stream");
}

@Override
public JPAJinqStream<T> andNotDifference(JPAJinqStream<T> otherSet)
{
Set<T> saved = collect(Collectors.toSet());
Set<T> other = otherSet.collect(Collectors.toSet());
saved.removeAll(other);
return wrap( JinqStream.from( saved ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,21 @@ public JPAJinqStream<T> andIntersect(JPAJinqStream<T> otherSet)
if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
return new JPAJinqStreamWrapper<>(this).andIntersect(otherSet);
}

@Override
public JPAJinqStream<T> notComplement()
{
QueryComposer<T> newComposer = jpaComposer.notComplement();
if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
return new JPAJinqStreamWrapper<>(this).notComplement();
}

@Override
public JPAJinqStream<T> andNotDifference(JPAJinqStream<T> otherSet)
{
QueryComposer<T> newComposer = jpaComposer.difference(otherSet);
if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
return new JPAJinqStreamWrapper<>(this).andNotDifference(otherSet);
}

// Wrapped versions of old API

Expand Down Expand Up @@ -315,4 +329,5 @@ public JPAJinqStream<T> setHint(String name, Object value)
{
return wrap(super.setHint(name, value));
}

}
79 changes: 75 additions & 4 deletions jinq-hibernate-legacy/test/org/jinq/hibernate/JinqJPATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.Optional;
import java.util.stream.Collectors;

import javax.persistence.Query;

import org.jinq.hibernate.test.entities.Customer;
import org.jinq.hibernate.test.entities.Item;
import org.jinq.hibernate.test.entities.Lineorder;
Expand All @@ -22,12 +20,12 @@
import org.jinq.jpa.JPAJinqStream;
import org.jinq.jpa.JPQL;
import org.jinq.jpa.jpqlquery.JPQLQuery;
import org.jinq.orm.stream.InQueryStreamSource;
import org.jinq.orm.stream.JinqStream;
import org.jinq.orm.stream.JinqStream.Where;
import org.jinq.orm.stream.QueryJinqStream;
import org.jinq.orm.stream.NonQueryJinqStream;
import org.jinq.tuples.Pair;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class JinqJPATest extends JinqJPATestBase
Expand Down Expand Up @@ -575,6 +573,78 @@ public void testOrUnionParameters()
assertEquals("SELECT A FROM org.jinq.hibernate.test.entities.Customer A WHERE A.name = :param0 OR A.name = :param1", query);
assertEquals(2, customers.size());
}

@Test(expected = UnsupportedOperationException.class)
public void testNotComplement()
{
JPAJinqStream<Customer> base = streams.streamAll(em, Customer.class);
JPAJinqStream<Customer> Bob = base.where( c -> c.getName().equals("Bob") );
JPAJinqStream<Customer> notBob = Bob.notComplement();
List<Customer> notBobList = notBob.toList();

assertEquals("SELECT A FROM Customer A WHERE NOT A.name = 'Bob'", query);

JinqStream<Customer> BobStream = new NonQueryJinqStream<>(base);
JPAJinqStream<Customer> notBobStream = (new JPAJinqStreamWrapper<>(BobStream)).notComplement();
Assert.fail();
}

@Test
public void testNotComplementQueries()
{
JPAJinqStream<Customer> base = streams.streamAll(em, Customer.class);
JPAJinqStream<Customer> Dave = base.where(c -> c.getName().equals("Dave"));
JPAJinqStream<Customer> allCustomers = base;

JPAJinqStream<Customer> notDave = Dave.notComplement();
List<Customer> notDaveList = notDave.toList();
assertEquals("SELECT A FROM Customer A WHERE NOT A.name = 'Dave'", query);

JPAJinqStream<Customer> notAll = allCustomers.notComplement();
List<Customer> notAllList = notAll.toList();
assertEquals("SELECT A FROM Customer A WHERE 0 = 1", query);

JPAJinqStream<Customer> notNotAll = notAll.notComplement();
List<Customer> notNotAllList = notNotAll.toList();
assertEquals("SELECT A FROM Customer A WHERE NOT 0 = 1", query);

JPAJinqStream<Customer> q = notNotAll.andIntersect(notDave);
List<Customer> qList = q.toList();
assertEquals("SELECT A FROM Customer A WHERE NOT 0 = 1 AND NOT A.name = 'Dave'", query);

JPAJinqStream<Customer> notQ = q.notComplement();
List<Customer> notQList = notQ.toList();
assertEquals("SELECT A FROM Customer A WHERE NOT (NOT 0 = 1 AND NOT A.name = 'Dave')", query);
}


@Test
public void testDifference()
{
JPAJinqStream<Customer> base = streams.streamAll(em, Customer.class);
JPAJinqStream<Customer> Dave = base.where(c -> c.getName().equals("Dave"));
String bobName = "Bob";
JPAJinqStream<Customer> Bob = base.where(c -> c.getName().equals(bobName));
JPAJinqStream<Customer> allCustomers = base;

JPAJinqStream<Customer> allDiffDave = allCustomers.andNotDifference(Dave);
List<Customer> allDiffDaveList = allDiffDave.toList();
assertEquals("SELECT A FROM Customer A WHERE NOT A.name = 'Dave'", query);

JPAJinqStream<Customer> BobDiffDave = Bob.andNotDifference(Dave);
List<Customer> BobDiffDaveResult = BobDiffDave.toList();
assertEquals("SELECT A FROM Customer A WHERE A.name = :param0 AND NOT A.name = 'Dave'", query);

JPAJinqStream<Customer> allDiffAll = allCustomers.andNotDifference(allCustomers);
List<Customer> allDiffAllList = allDiffAll.toList();
assertEquals("SELECT A FROM Customer A WHERE 0 = 1", query);

JPAJinqStream<Customer> DaveDiffAll = Dave.andNotDifference(allCustomers);
List<Customer> DaveDiffAllList = DaveDiffAll.toList();
assertEquals("SELECT A FROM Customer A WHERE 0 = 1", query);

}


@Test
public void testAndIntersect()
Expand All @@ -591,4 +661,5 @@ public void testAndIntersect()
assertEquals("Bob", customers.get(0));
assertEquals("Dave", customers.get(1));
}

}
44 changes: 42 additions & 2 deletions jinq-jpa/main/org/jinq/jpa/JPAJinqStream.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jinq.jpa;

import java.util.Collection;
import java.util.stream.Stream;
import org.jinq.orm.stream.JinqStream;
import org.jinq.orm.stream.NonQueryJinqStream;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple3;
import org.jinq.tuples.Tuple4;
Expand Down Expand Up @@ -90,8 +93,29 @@ public interface JPAJinqStream<T> extends JinqStream<T>
* @return a new stream with the contents of the two streams INTERSECTed together
*/
public JPAJinqStream<T> andIntersect(JPAJinqStream<T> otherSet);



/**
* Emulates a complement of stream by negation of the query
* using a NOT operation. JPA does not support NOT operations, so
* Jinq must emulate that behavior using NOTs. It also provides a mechanism
* for Jinq to let people create NOT expressions programmatically and to specify
* complex expressions exactly without relying on the Jinq translation algorithm.
*<p>
* Since Universe of collection-based <code>NonQueryJinqStream</code> is not well defined,
* <code>notComplement()</code> is not supported for such streams.
*
* @return a new stream being a COMPLEMENT of <code>this</code> stream,
* if <code>this</code> is actually QueryJPAJinqStream and its complement can be succesfully translated
* @throws UnsupportedOperationException otherwise
*
* @see difference(JPAJinqStream<T> otherSet);
*/
public JPAJinqStream<T> notComplement();


// andNotDifference of sets
public JPAJinqStream<T> andNotDifference(JPAJinqStream<T> otherSet);

// Variants of the existing JinqStream API that return a JPAJinqStream instead
// of a JinqStream.

Expand Down Expand Up @@ -193,4 +217,20 @@ public <V extends Comparable<V>> JPAJinqStream<T> sortedDescendingBy(
public JPAJinqStream<T> distinct();

public JPAJinqStream<T> setHint(String name, Object value);

/**
* Easy way to get a JinqStream from a collection.
*/
public static <U> JPAJinqStream<U> from(Collection<U> collection)
{
return new JPAJinqStreamWrapper<>(new NonQueryJinqStream<U>(collection.stream()));
}

/**
* Creates a JinqStream containing a single object.
*/
public static <U> JPAJinqStream<U> of(U value, Stream<U> universe)
{
return new JPAJinqStreamWrapper<>(new NonQueryJinqStream<>(Stream.of(value)));
}
}
26 changes: 26 additions & 0 deletions jinq-jpa/main/org/jinq/jpa/JPAJinqStreamWrapper.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package org.jinq.jpa;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jinq.orm.stream.JinqStream;
import org.jinq.orm.stream.LazyWrappedStream;
import org.jinq.orm.stream.NonQueryJinqStream;
import org.jinq.orm.stream.QueryJinqStream;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple3;
import org.jinq.tuples.Tuple4;
Expand Down Expand Up @@ -435,4 +442,23 @@ public JPAJinqStream<T> andIntersect(JPAJinqStream<T> otherSet)
Set<T> saved = collect(Collectors.toSet());
return wrap(JinqStream.from(otherSet.filter(el -> saved.contains(el)).collect(Collectors.toSet())));
}

@Override
public JPAJinqStream<T> notComplement()
{
//Set<T> all = ?
//Set<T> saved = wrapped.collect(Collectors.toSet());
//return wrap( JinqStream.from(all).where( el -> !saved.contains(el)) );

throw new UnsupportedOperationException("operation not supported for this stream");
}

@Override
public JPAJinqStream<T> andNotDifference(JPAJinqStream<T> otherSet)
{
Set<T> saved = collect(Collectors.toSet());
Set<T> other = otherSet.collect(Collectors.toSet());
saved.removeAll(other);
return wrap( JinqStream.from( saved ) );
}
}
13 changes: 12 additions & 1 deletion jinq-jpa/main/org/jinq/jpa/JPAQueryComposer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jinq.jpa.transform.LimitSkipTransform;
import org.jinq.jpa.transform.MetamodelUtil;
import org.jinq.jpa.transform.MultiAggregateTransform;
import org.jinq.jpa.transform.NotTransform;
import org.jinq.jpa.transform.SetOperationEmulationTransform;
import org.jinq.jpa.transform.OuterJoinOnTransform;
import org.jinq.jpa.transform.OuterJoinTransform;
Expand Down Expand Up @@ -589,7 +590,17 @@ public QueryComposer<T> andIntersect(JPAJinqStream<T> otherSet)
{
return applyTransformWithTwoQueryMerge(new SetOperationEmulationTransform(getConfig(), SetOperationEmulationTransform.SetOperationType.AND_INTERSECT), otherSet);
}


public QueryComposer<T> notComplement()
{
return applyTransformWithLambda(new NotTransform(getConfig()));
}

public QueryComposer<T> difference(JPAJinqStream<T> otherSet)
{
return applyTransformWithTwoQueryMerge(new SetOperationEmulationTransform(getConfig(), SetOperationEmulationTransform.SetOperationType.DIFFERENCE), otherSet);
}

@Override
public Long count()
{
Expand Down
18 changes: 17 additions & 1 deletion jinq-jpa/main/org/jinq/jpa/QueryJPAJinqStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,23 @@ public JPAJinqStream<T> andIntersect(JPAJinqStream<T> otherSet)
return new JPAJinqStreamWrapper<>(this).andIntersect(otherSet);
}


@Override
public JPAJinqStream<T> notComplement()
{
QueryComposer<T> newComposer = jpaComposer.notComplement();
if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
return new JPAJinqStreamWrapper<>(this).notComplement();
}

@Override
public JPAJinqStream<T> andNotDifference(JPAJinqStream<T> otherSet)
{
QueryComposer<T> newComposer = jpaComposer.difference(otherSet);
if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
return new JPAJinqStreamWrapper<>(this).andNotDifference(otherSet);
}


// Wrapped versions of old API

@Override
Expand Down
Loading