summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2006-09-23 00:05:31 +0000
committerBrian S. O'Neill <bronee@gmail.com>2006-09-23 00:05:31 +0000
commita314f76c0eb33222809fd686d4a41a271e62fcff (patch)
treef2a7d0a00bfaafa788ba2867dcbc3229ad34c6d8 /src
parentc5e6a41a0e071e342b6f82d219e390354d34041a (diff)
Added notJoinedFrom method.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/amazon/carbonado/filter/AndFilter.java15
-rw-r--r--src/main/java/com/amazon/carbonado/filter/Filter.java135
-rw-r--r--src/main/java/com/amazon/carbonado/filter/OrFilter.java32
-rw-r--r--src/main/java/com/amazon/carbonado/filter/PropertyFilter.java31
-rw-r--r--src/test/java/com/amazon/carbonado/filter/TestFilterAsJoinedFrom.java156
-rw-r--r--src/test/java/com/amazon/carbonado/filter/TestFilterNotJoinedFrom.java271
6 files changed, 640 insertions, 0 deletions
diff --git a/src/main/java/com/amazon/carbonado/filter/AndFilter.java b/src/main/java/com/amazon/carbonado/filter/AndFilter.java
index 2b5ff0f..a1fd664 100644
--- a/src/main/java/com/amazon/carbonado/filter/AndFilter.java
+++ b/src/main/java/com/amazon/carbonado/filter/AndFilter.java
@@ -66,6 +66,21 @@ public class AndFilter<S extends Storable> extends BinaryOpFilter<S> {
return mLeft.asJoinedFrom(joinProperty).and(mRight.asJoinedFrom(joinProperty));
}
+ @Override
+ NotJoined notJoinedFrom(ChainedProperty<S> joinProperty,
+ Class<? extends Storable> joinPropertyType)
+ {
+ NotJoined left = mLeft.notJoinedFrom(joinProperty, joinPropertyType);
+ NotJoined right = mRight.notJoinedFrom(joinProperty, joinPropertyType);
+
+ // Remove wildcards to shut the compiler up.
+ Filter leftNotJoined = left.getNotJoinedFilter();
+ Filter rightNotJoined = right.getNotJoinedFilter();
+
+ return new NotJoined(leftNotJoined.and(rightNotJoined),
+ left.getRemainderFilter().and(right.getRemainderFilter()));
+ }
+
Filter<S> buildDisjunctiveNormalForm() {
Filter<S> left = mLeft.reduce().dnf();
Filter<S> right = mRight.reduce().dnf();
diff --git a/src/main/java/com/amazon/carbonado/filter/Filter.java b/src/main/java/com/amazon/carbonado/filter/Filter.java
index 55e0b93..05cf47d 100644
--- a/src/main/java/com/amazon/carbonado/filter/Filter.java
+++ b/src/main/java/com/amazon/carbonado/filter/Filter.java
@@ -480,6 +480,91 @@ public abstract class Filter<S extends Storable> implements Appender {
*/
public abstract <T extends Storable> Filter<T> asJoinedFrom(ChainedProperty<T> joinProperty);
+ /**
+ * Removes a join property prefix from all applicable properties of this
+ * filter. For example, consider two Storable types, Person and
+ * Address. Person has a property "homeAddress" which joins to Address. A
+ * Person filter might be "homeAddress.city = ? & lastName = ?". When not
+ * joined from "homeAddress", it becomes "city = ?" on Address with a
+ * remainder of "lastName = ?" on Person.
+ *
+ * <p>The resulting remainder filter (if any) is always logically and'd to
+ * the not joined filter. In order to achieve this, the original filter is
+ * first converted to conjunctive normal form. And as a side affect, both
+ * the remainder and not joined filters are {@link #bind bound}.
+ *
+ * @param joinProperty property to not join from
+ * @return not join result
+ * @throws IllegalArgumentException if property does not exist or if
+ * property does not refer to a Storable
+ */
+ public final NotJoined notJoinedFrom(String joinProperty) {
+ return notJoinedFrom
+ (ChainedProperty.parse(StorableIntrospector.examine(mType), joinProperty));
+ }
+
+ /**
+ * Removes a join property prefix from all applicable properties of this
+ * filter. For example, consider two Storable types, Person and
+ * Address. Person has a property "homeAddress" which joins to Address. A
+ * Person filter might be "homeAddress.city = ? & lastName = ?". When not
+ * joined from "homeAddress", it becomes "city = ?" on Address with a
+ * remainder of "lastName = ?" on Person.
+ *
+ * <p>The resulting remainder filter (if any) is always logically and'd to
+ * the not joined filter. In order to achieve this, the original filter is
+ * first converted to conjunctive normal form. And as a side affect, both
+ * the remainder and not joined filters are {@link #bind bound}.
+ *
+ * @param joinProperty property to not join from
+ * @return not join result
+ * @throws IllegalArgumentException if property does not refer to a Storable
+ */
+ public final NotJoined notJoinedFrom(ChainedProperty<S> joinProperty) {
+ Class<?> type = joinProperty.getType();
+ if (!Storable.class.isAssignableFrom(type)) {
+ throw new IllegalArgumentException
+ ("Join property type is not a Storable: " + joinProperty);
+ }
+
+ Filter<S> cnf = conjunctiveNormalForm();
+ NotJoined nj = cnf.notJoinedFrom(joinProperty, (Class<Storable>) type);
+
+ if (nj.getNotJoinedFilter() instanceof OpenFilter) {
+ // Remainder filter should be same as original, but it might have
+ // expanded with conjunctive normal form. If so, restore to
+ // original, but still bind it to ensure consistent side-effects.
+ if (nj.getRemainderFilter() != this) {
+ nj = new NotJoined(nj.getNotJoinedFilter(), bind());
+ }
+ }
+
+ if (isDisjunctiveNormalForm()) {
+ // Try to return filters which look similar to the original. The
+ // conversion from disjunctive normal form to conjunctive normal
+ // form may make major changes. If original was dnf, restore the
+ // result filters to dnf.
+
+ if (!(nj.getNotJoinedFilter().isDisjunctiveNormalForm()) ||
+ !(nj.getRemainderFilter().isDisjunctiveNormalForm()))
+ {
+ nj = new NotJoined(nj.getNotJoinedFilter().disjunctiveNormalForm(),
+ nj.getRemainderFilter().disjunctiveNormalForm());
+ }
+ }
+
+ return nj;
+ }
+
+ /**
+ * Should only be called on a filter in conjunctive normal form.
+ */
+ NotJoined notJoinedFrom(ChainedProperty<S> joinProperty,
+ Class<? extends Storable> joinPropertyType)
+ {
+ return new NotJoined(getOpenFilter(joinPropertyType), this);
+ }
+
abstract Filter<S> buildDisjunctiveNormalForm();
abstract Filter<S> buildConjunctiveNormalForm();
@@ -541,4 +626,54 @@ public abstract class Filter<S extends Storable> implements Appender {
}
abstract void dumpTree(Appendable app, int indentLevel) throws IOException;
+
+ /**
+ * Result from calling {@link Filter#notJoinedFrom}.
+ */
+ public class NotJoined {
+ private final Filter<?> mNotJoined;
+ private final Filter<S> mRemainder;
+
+ NotJoined(Filter<?> notJoined, Filter<S> remainder) {
+ mNotJoined = notJoined;
+ mRemainder = remainder;
+ }
+
+ /**
+ * Returns the filter which is no longer as from a join.
+ *
+ * @return not joined filter or open filter if none
+ */
+ public Filter<?> getNotJoinedFilter() {
+ return mNotJoined;
+ }
+
+ /**
+ * Returns the filter which could not be separated.
+ *
+ * @return remainder filter or open filter if none
+ */
+ public Filter<S> getRemainderFilter() {
+ return mRemainder;
+ }
+
+ public int hashCode() {
+ return mNotJoined.hashCode() * 31 + mRemainder.hashCode();
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof Filter.NotJoined) {
+ NotJoined other = (NotJoined) obj;
+ return mNotJoined.equals(other.mNotJoined) && mRemainder.equals(other.mRemainder);
+ }
+ return false;
+ }
+
+ public String toString() {
+ return "not joined: " + mNotJoined + ", remainder: " + mRemainder;
+ }
+ }
}
diff --git a/src/main/java/com/amazon/carbonado/filter/OrFilter.java b/src/main/java/com/amazon/carbonado/filter/OrFilter.java
index e1d4f6e..c7ff64a 100644
--- a/src/main/java/com/amazon/carbonado/filter/OrFilter.java
+++ b/src/main/java/com/amazon/carbonado/filter/OrFilter.java
@@ -66,6 +66,38 @@ public class OrFilter<S extends Storable> extends BinaryOpFilter<S> {
return mLeft.asJoinedFrom(joinProperty).or(mRight.asJoinedFrom(joinProperty));
}
+ @Override
+ NotJoined notJoinedFrom(ChainedProperty<S> joinProperty,
+ Class<? extends Storable> joinPropertyType)
+ {
+ NotJoined left = mLeft.notJoinedFrom(joinProperty, joinPropertyType);
+ NotJoined right = mRight.notJoinedFrom(joinProperty, joinPropertyType);
+
+ // Assert that our child nodes are only OrFilter or PropertyFilter.
+ if (!isConjunctiveNormalForm()) {
+ throw new IllegalStateException();
+ }
+
+ // If child nodes have any remainder, then everything must go to the
+ // remainder. As per the contract of notJoinedFrom, the not-joined and
+ // remainder filters are logically and'd together to reform the
+ // original filter. If the remainder was broken up, then the not-joined
+ // and remainder filters would need to logically or'd together to
+ // reform the original filter, breaking the notJoinedFrom contract.
+
+ if (!(left.getRemainderFilter() instanceof OpenFilter) ||
+ !(right.getRemainderFilter() instanceof OpenFilter))
+ {
+ return super.notJoinedFrom(joinProperty, joinPropertyType);
+ }
+
+ // Remove wildcards to shut the compiler up.
+ Filter leftNotJoined = left.getNotJoinedFilter();
+ Filter rightNotJoined = right.getNotJoinedFilter();
+
+ return new NotJoined(leftNotJoined.or(rightNotJoined), getOpenFilter(getStorableType()));
+ }
+
Filter<S> buildDisjunctiveNormalForm() {
return mLeft.dnf().or(mRight.dnf()).reduce();
}
diff --git a/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java b/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java
index b66ad22..64d1d94 100644
--- a/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java
+++ b/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java
@@ -184,6 +184,37 @@ public class PropertyFilter<S extends Storable> extends Filter<S> {
}
}
+ @Override
+ NotJoined notJoinedFrom(ChainedProperty<S> joinProperty,
+ Class<? extends Storable> joinPropertyType)
+ {
+ ChainedProperty<?> notJoinedProp = getChainedProperty();
+ ChainedProperty<?> jp = joinProperty;
+
+ while (notJoinedProp.getPrimeProperty().equals(jp.getPrimeProperty())) {
+ notJoinedProp = notJoinedProp.tail();
+ if (jp.getChainCount() == 0) {
+ jp = null;
+ break;
+ }
+ jp = jp.tail();
+ }
+
+ if (jp != null || notJoinedProp.equals(getChainedProperty())) {
+ return super.notJoinedFrom(joinProperty, joinPropertyType);
+ }
+
+ PropertyFilter<?> notJoinedFilter;
+
+ if (isConstant()) {
+ notJoinedFilter = getCanonical(notJoinedProp, mOp, mConstant);
+ } else {
+ notJoinedFilter = getCanonical(notJoinedProp, mOp, mBindID);
+ }
+
+ return new NotJoined(notJoinedFilter, getOpenFilter(getStorableType()));
+ }
+
/**
* Returns another PropertyFilter instance which is bound to the given constant value.
*
diff --git a/src/test/java/com/amazon/carbonado/filter/TestFilterAsJoinedFrom.java b/src/test/java/com/amazon/carbonado/filter/TestFilterAsJoinedFrom.java
new file mode 100644
index 0000000..3b6bfde
--- /dev/null
+++ b/src/test/java/com/amazon/carbonado/filter/TestFilterAsJoinedFrom.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
+ * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
+ * of Amazon Technologies, Inc. or its affiliates. All rights reserved.
+ *
+ * Licensed 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 com.amazon.carbonado.filter;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import com.amazon.carbonado.stored.Address;
+import com.amazon.carbonado.stored.Order;
+import com.amazon.carbonado.stored.Shipment;
+
+/**
+ *
+ *
+ * @author Brian S O'Neill
+ */
+public class TestFilterAsJoinedFrom extends TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(TestFilterAsJoinedFrom.class);
+ }
+
+ public TestFilterAsJoinedFrom(String name) {
+ super(name);
+ }
+
+ public void testOpen() {
+ Filter<Address> filter = Filter.getOpenFilter(Address.class);
+
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "address");
+ assertTrue(f2 instanceof OpenFilter);
+ assertEquals(Order.class, f2.getStorableType());
+
+ Filter<Shipment> f3 = filter.asJoinedFrom(Shipment.class, "order.address");
+ assertTrue(f3 instanceof OpenFilter);
+ assertEquals(Shipment.class, f3.getStorableType());
+
+ Filter<Shipment> f4 = f2.asJoinedFrom(Shipment.class, "order");
+ assertEquals(f3, f4);
+ assertTrue(f3 == f4);
+ }
+
+ public void testClosed() {
+ Filter<Address> filter = Filter.getClosedFilter(Address.class);
+
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "address");
+ assertTrue(f2 instanceof ClosedFilter);
+ assertEquals(Order.class, f2.getStorableType());
+
+ Filter<Shipment> f3 = filter.asJoinedFrom(Shipment.class, "order.address");
+ assertTrue(f3 instanceof ClosedFilter);
+ assertEquals(Shipment.class, f3.getStorableType());
+
+ Filter<Shipment> f4 = f2.asJoinedFrom(Shipment.class, "order");
+ assertEquals(f3, f4);
+ assertTrue(f3 == f4);
+ }
+
+ public void testSimple() {
+ Filter<Address> filter = Filter.filterFor(Address.class, "addressCity = ?");
+
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "address");
+ assertEquals(f2, Filter.filterFor(Order.class, "address.addressCity = ?"));
+
+ Filter<Shipment> f3 = filter.asJoinedFrom(Shipment.class, "order.address");
+ assertEquals(f3, Filter.filterFor(Shipment.class, "order.address.addressCity = ?"));
+
+ Filter<Shipment> f4 = f2.asJoinedFrom(Shipment.class, "order");
+ assertEquals(f3, f4);
+ assertTrue(f3 == f4);
+ }
+
+ public void testAnd() {
+ Filter<Address> filter = Filter.filterFor
+ (Address.class, "addressCity = ? & addressZip != ?");
+
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "address");
+ assertEquals(f2, Filter.filterFor
+ (Order.class, "address.addressCity = ? & address.addressZip != ?"));
+
+ Filter<Shipment> f3 = filter.asJoinedFrom(Shipment.class, "order.address");
+ assertEquals(f3, Filter.filterFor
+ (Shipment.class,
+ "order.address.addressCity = ? & order.address.addressZip != ?"));
+
+ Filter<Shipment> f4 = f2.asJoinedFrom(Shipment.class, "order");
+ assertEquals(f3, f4);
+ assertTrue(f3 == f4);
+ }
+
+ public void testOr() {
+ Filter<Address> filter = Filter.filterFor
+ (Address.class, "addressCity = ? | addressZip != ?");
+
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "address");
+ assertEquals(f2, Filter.filterFor
+ (Order.class, "address.addressCity = ? | address.addressZip != ?"));
+
+ Filter<Shipment> f3 = filter.asJoinedFrom(Shipment.class, "order.address");
+ assertEquals(f3, Filter.filterFor
+ (Shipment.class,
+ "order.address.addressCity = ? | order.address.addressZip != ?"));
+
+ Filter<Shipment> f4 = f2.asJoinedFrom(Shipment.class, "order");
+ assertEquals(f3, f4);
+ assertTrue(f3 == f4);
+ }
+
+ public void testMixed() {
+ Filter<Address> filter = Filter.filterFor
+ (Address.class, "addressCity = ? & addressZip != ? | addressState > ?");
+
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "address");
+ assertEquals
+ (f2, Filter.filterFor
+ (Order.class,
+ "address.addressCity = ? & address.addressZip != ? | address.addressState > ?"));
+
+ Filter<Shipment> f3 = filter.asJoinedFrom(Shipment.class, "order.address");
+ assertEquals(f3, Filter.filterFor
+ (Shipment.class,
+ "order.address.addressCity = ? & order.address.addressZip != ? " +
+ " | order.address.addressState > ?"));
+ Filter<Shipment> f4 = f2.asJoinedFrom(Shipment.class, "order");
+ assertEquals(f3, f4);
+ assertTrue(f3 == f4);
+ }
+
+ public void testError() {
+ try {
+ Filter<Address> filter = Filter.filterFor(Address.class, "addressCity = ?");
+ Filter<Order> f2 = filter.asJoinedFrom(Order.class, "orderTotal");
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+ }
+}
diff --git a/src/test/java/com/amazon/carbonado/filter/TestFilterNotJoinedFrom.java b/src/test/java/com/amazon/carbonado/filter/TestFilterNotJoinedFrom.java
new file mode 100644
index 0000000..1e97d89
--- /dev/null
+++ b/src/test/java/com/amazon/carbonado/filter/TestFilterNotJoinedFrom.java
@@ -0,0 +1,271 @@
+/*
+ * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
+ * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
+ * of Amazon Technologies, Inc. or its affiliates. All rights reserved.
+ *
+ * Licensed 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 com.amazon.carbonado.filter;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import com.amazon.carbonado.stored.Address;
+import com.amazon.carbonado.stored.Order;
+import com.amazon.carbonado.stored.Shipment;
+
+/**
+ *
+ *
+ * @author Brian S O'Neill
+ */
+public class TestFilterNotJoinedFrom extends TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(TestFilterNotJoinedFrom.class);
+ }
+
+ public TestFilterNotJoinedFrom(String name) {
+ super(name);
+ }
+
+ public void testOpen() {
+ Filter<Shipment> filter = Filter.getOpenFilter(Shipment.class);
+
+ Filter<Shipment>.NotJoined nj = filter.notJoinedFrom("order");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+ }
+
+ public void testClosed() {
+ Filter<Shipment> filter = Filter.getClosedFilter(Shipment.class);
+
+ Filter<Shipment>.NotJoined nj = filter.notJoinedFrom("order");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+ }
+
+ public void testSimple() {
+ Filter<Shipment> filter = Filter.filterFor(Shipment.class, "shipmentNotes != ?");
+ filter = filter.bind();
+
+ Filter<Shipment>.NotJoined nj = filter.notJoinedFrom("order");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = Filter.filterFor(Shipment.class, "order.orderTotal < ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor(Order.class, "orderTotal < ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = Filter.filterFor(Shipment.class, "order.address.addressCity = ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor(Order.class, "address.addressCity = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.filterFor(Address.class, "addressCity = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ try {
+ nj = filter.notJoinedFrom("order.address.addressCity");
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAnd() {
+ Filter<Shipment> filter = Filter.filterFor
+ (Shipment.class, "shipmentNotes != ? & shipmentDate > ?");
+ filter = filter.bind();
+
+ Filter<Shipment>.NotJoined nj = filter.notJoinedFrom("order");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = Filter.filterFor
+ (Shipment.class,
+ "order.orderTotal < ? & shipmentNotes != ? & order.orderComments = ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor(Order.class, "orderTotal < ? & orderComments = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.filterFor(Shipment.class, "shipmentNotes != ?").bind(),
+ nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = Filter.filterFor
+ (Shipment.class,
+ "order.orderTotal < ? & order.address.addressCity != ? " +
+ "& order.address.addressZip = ? & shipmentNotes != ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor(Order.class, "orderTotal < ? & address.addressCity != ? " +
+ "& address.addressZip = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.filterFor(Shipment.class, "shipmentNotes != ?").bind(),
+ nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.filterFor(Address.class, "addressCity != ? & addressZip = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.filterFor
+ (Shipment.class, "order.orderTotal < ? & shipmentNotes != ?").bind(),
+ nj.getRemainderFilter());
+ }
+
+ public void testOr() {
+ Filter<Shipment> filter = Filter.filterFor
+ (Shipment.class, "shipmentNotes != ? | shipmentDate > ?");
+ filter = filter.bind();
+
+ Filter<Shipment>.NotJoined nj = filter.notJoinedFrom("order");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = Filter.filterFor
+ (Shipment.class,
+ "order.orderTotal < ? | order.orderComments = ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor(Order.class, "orderTotal < ? | orderComments = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ filter = Filter.filterFor
+ (Shipment.class,
+ "order.orderTotal < ? | shipmentNotes != ? | order.orderComments = ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.getOpenFilter(Order.class), nj.getNotJoinedFilter());
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertTrue(nj.getNotJoinedFilter() instanceof OpenFilter);
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = Filter.filterFor
+ (Shipment.class,
+ "order.address.addressCity != ? | order.address.addressZip = ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor(Order.class,
+ "address.addressCity != ? | address.addressZip = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.filterFor(Address.class, "addressCity != ? | addressZip = ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ filter = Filter.filterFor
+ (Shipment.class,
+ "order.orderTotal < ? | order.address.addressCity != ? " +
+ "| order.address.addressZip = ? | shipmentNotes != ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.getOpenFilter(Order.class), nj.getNotJoinedFilter());
+ assertEquals(filter, nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.getOpenFilter(Address.class), nj.getNotJoinedFilter());
+ assertEquals(filter, nj.getRemainderFilter());
+ }
+
+ public void testMixed() {
+ Filter<Shipment> filter = Filter.filterFor
+ (Shipment.class,
+ "(order.address.addressCity = ? & order.address.addressZip = ?) " +
+ "| order.orderTotal != ?");
+ filter = filter.bind();
+
+ Filter<Shipment>.NotJoined nj = filter.notJoinedFrom("order");
+ assertEquals(Filter.filterFor
+ (Order.class,
+ "address.addressCity = ? & address.addressZip = ? | orderTotal != ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.getOpenFilter(Shipment.class), nj.getRemainderFilter());
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.getOpenFilter(Address.class), nj.getNotJoinedFilter());
+ assertEquals(filter, nj.getRemainderFilter());
+
+ filter = filter.and("order.address.customData > ?");
+ filter = filter.bind();
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.filterFor(Address.class, "customData > ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.filterFor
+ (Shipment.class,
+ "(order.address.addressCity = ? | order.orderTotal != ?) " +
+ "& (order.address.addressZip = ? | order.orderTotal != ?)"),
+ nj.getRemainderFilter().unbind());
+
+ filter = filter.disjunctiveNormalForm();
+
+ nj = filter.notJoinedFrom("order.address");
+ assertEquals(Filter.filterFor(Address.class, "customData > ?").bind(),
+ nj.getNotJoinedFilter());
+ assertEquals(Filter.filterFor
+ (Shipment.class,
+ "order.address.addressCity = ? & order.address.addressZip = ? " +
+ "| order.orderTotal != ?").bind(),
+ nj.getRemainderFilter());
+ }
+}