From c5e6a41a0e071e342b6f82d219e390354d34041a Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Fri, 22 Sep 2006 06:40:51 +0000 Subject: Added asJoinedFrom method. Refined return values of subclasses. --- .../com/amazon/carbonado/filter/AndFilter.java | 6 ++++ .../com/amazon/carbonado/filter/ClosedFilter.java | 16 ++++++--- .../java/com/amazon/carbonado/filter/Filter.java | 38 +++++++++++++++++++--- .../com/amazon/carbonado/filter/OpenFilter.java | 14 +++++--- .../java/com/amazon/carbonado/filter/OrFilter.java | 6 ++++ .../amazon/carbonado/filter/PropertyFilter.java | 22 +++++++++++-- 6 files changed, 86 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main/java/com/amazon/carbonado/filter/AndFilter.java b/src/main/java/com/amazon/carbonado/filter/AndFilter.java index a5aa61e..2b5ff0f 100644 --- a/src/main/java/com/amazon/carbonado/filter/AndFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/AndFilter.java @@ -22,6 +22,8 @@ import java.io.IOException; import com.amazon.carbonado.Storable; +import com.amazon.carbonado.info.ChainedProperty; + /** * Filter tree node that performs a logical 'and' test. * @@ -60,6 +62,10 @@ public class AndFilter extends BinaryOpFilter { return mLeft.unbind().and(mRight.unbind()); } + public Filter asJoinedFrom(ChainedProperty joinProperty) { + return mLeft.asJoinedFrom(joinProperty).and(mRight.asJoinedFrom(joinProperty)); + } + Filter buildDisjunctiveNormalForm() { Filter left = mLeft.reduce().dnf(); Filter right = mRight.reduce().dnf(); diff --git a/src/main/java/com/amazon/carbonado/filter/ClosedFilter.java b/src/main/java/com/amazon/carbonado/filter/ClosedFilter.java index 7895a3d..6538f8d 100644 --- a/src/main/java/com/amazon/carbonado/filter/ClosedFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/ClosedFilter.java @@ -22,6 +22,8 @@ import java.io.IOException; import com.amazon.carbonado.Storable; +import com.amazon.carbonado.info.ChainedProperty; + /** * Filter which blocks any results from passing through. * @@ -32,7 +34,7 @@ public class ClosedFilter extends Filter { super(type); } - public Filter and(Filter filter) { + public ClosedFilter and(Filter filter) { return this; } @@ -40,7 +42,7 @@ public class ClosedFilter extends Filter { return filter; } - public Filter not() { + public OpenFilter not() { return getOpenFilter(getStorableType()); } @@ -58,7 +60,11 @@ public class ClosedFilter extends Filter { return visitor.visit(this, param); } - public Filter bind() { + public ClosedFilter bind() { + return this; + } + + public ClosedFilter unbind() { return this; } @@ -66,8 +72,8 @@ public class ClosedFilter extends Filter { return true; } - public Filter unbind() { - return this; + public ClosedFilter asJoinedFrom(ChainedProperty joinProperty) { + return getClosedFilter(joinProperty.getPrimeProperty().getEnclosingType()); } void markBound() { diff --git a/src/main/java/com/amazon/carbonado/filter/Filter.java b/src/main/java/com/amazon/carbonado/filter/Filter.java index 338b768..55e0b93 100644 --- a/src/main/java/com/amazon/carbonado/filter/Filter.java +++ b/src/main/java/com/amazon/carbonado/filter/Filter.java @@ -28,6 +28,7 @@ import com.amazon.carbonado.MalformedFilterException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.info.ChainedProperty; +import com.amazon.carbonado.info.StorableIntrospector; import com.amazon.carbonado.util.Appender; @@ -95,7 +96,7 @@ public abstract class Filter implements Appender { * @return canonical Filter instance * @see OpenFilter */ - public static Filter getOpenFilter(Class type) { + public static OpenFilter getOpenFilter(Class type) { Map> filterCache = getFilterCache(type); synchronized (filterCache) { Filter filter = filterCache.get(OPEN_KEY); @@ -103,7 +104,7 @@ public abstract class Filter implements Appender { filter = new OpenFilter(type); filterCache.put(OPEN_KEY, filter); } - return filter; + return (OpenFilter) filter; } } @@ -115,7 +116,7 @@ public abstract class Filter implements Appender { * @return canonical Filter instance * @see ClosedFilter */ - public static Filter getClosedFilter(Class type) { + public static ClosedFilter getClosedFilter(Class type) { Map> filterCache = getFilterCache(type); synchronized (filterCache) { Filter filter = filterCache.get(CLOSED_KEY); @@ -123,7 +124,7 @@ public abstract class Filter implements Appender { filter = new ClosedFilter(type); filterCache.put(CLOSED_KEY, filter); } - return filter; + return (ClosedFilter) filter; } } @@ -450,6 +451,35 @@ public abstract class Filter implements Appender { return isReduced() ? this : accept(new Reducer(), null); } + /** + * Prepends a join property to all properties of this filter. For example, + * consider two Storable types, Person and Address. Person has a property + * "homeAddress" which joins to Address. An Address filter, "city = ?", as + * joined from Person's "homeAddress", becomes "homeAddress.city = ?". + * + * @param type type of T which contains join property + * @param joinProperty property of T which joins to this Filter's Storable type + * @return filter for type T + * @throws IllegalArgumentException if property does not exist or is not a + * join to type S + */ + public final Filter asJoinedFrom(Class type, String joinProperty) { + return asJoinedFrom + (ChainedProperty.parse(StorableIntrospector.examine(type), joinProperty)); + } + + /** + * Prepends a join property to all properties of this filter. For example, + * consider two Storable types, Person and Address. Person has a property + * "homeAddress" which joins to Address. An Address filter, "city = ?", as + * joined from Person's "homeAddress", becomes "homeAddress.city = ?". + * + * @param joinProperty property of T which joins to this Filter's Storable type + * @return filter for type T + * @throws IllegalArgumentException if property is not a join to type S + */ + public abstract Filter asJoinedFrom(ChainedProperty joinProperty); + abstract Filter buildDisjunctiveNormalForm(); abstract Filter buildConjunctiveNormalForm(); diff --git a/src/main/java/com/amazon/carbonado/filter/OpenFilter.java b/src/main/java/com/amazon/carbonado/filter/OpenFilter.java index 6f6e211..235c27d 100644 --- a/src/main/java/com/amazon/carbonado/filter/OpenFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/OpenFilter.java @@ -22,6 +22,8 @@ import java.io.IOException; import com.amazon.carbonado.Storable; +import com.amazon.carbonado.info.ChainedProperty; + /** * Filter which lets all results pass through. * @@ -36,11 +38,11 @@ public class OpenFilter extends Filter { return filter; } - public Filter or(Filter filter) { + public OpenFilter or(Filter filter) { return this; } - public Filter not() { + public ClosedFilter not() { return getClosedFilter(getStorableType()); } @@ -58,11 +60,11 @@ public class OpenFilter extends Filter { return visitor.visit(this, param); } - public Filter bind() { + public OpenFilter bind() { return this; } - public Filter unbind() { + public OpenFilter unbind() { return this; } @@ -70,6 +72,10 @@ public class OpenFilter extends Filter { return true; } + public OpenFilter asJoinedFrom(ChainedProperty joinProperty) { + return getOpenFilter(joinProperty.getPrimeProperty().getEnclosingType()); + } + void markBound() { } diff --git a/src/main/java/com/amazon/carbonado/filter/OrFilter.java b/src/main/java/com/amazon/carbonado/filter/OrFilter.java index 1e7bf72..e1d4f6e 100644 --- a/src/main/java/com/amazon/carbonado/filter/OrFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/OrFilter.java @@ -22,6 +22,8 @@ import java.io.IOException; import com.amazon.carbonado.Storable; +import com.amazon.carbonado.info.ChainedProperty; + /** * Filter tree node that performs a logical 'or' test. * @@ -60,6 +62,10 @@ public class OrFilter extends BinaryOpFilter { return mLeft.unbind().or(mRight.unbind()); } + public Filter asJoinedFrom(ChainedProperty joinProperty) { + return mLeft.asJoinedFrom(joinProperty).or(mRight.asJoinedFrom(joinProperty)); + } + Filter 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 824d1d1..b66ad22 100644 --- a/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java +++ b/src/main/java/com/amazon/carbonado/filter/PropertyFilter.java @@ -105,7 +105,7 @@ public class PropertyFilter extends Filter { } @Override - public Filter not() { + public PropertyFilter not() { if (mBindID == BOUND_CONSTANT) { return getCanonical(mProperty, mOp.reverse(), mConstant); } else { @@ -156,11 +156,11 @@ public class PropertyFilter extends Filter { return mBindID; } - public Filter bind() { + public PropertyFilter bind() { return mBindID == 0 ? getCanonical(this, 1) : this; } - public Filter unbind() { + public PropertyFilter unbind() { return mBindID == 0 ? this : getCanonical(this, 0); } @@ -168,6 +168,22 @@ public class PropertyFilter extends Filter { return mBindID != 0; } + public PropertyFilter asJoinedFrom(ChainedProperty joinProperty) { + if (joinProperty.getType() != getStorableType()) { + throw new IllegalArgumentException + ("Property is not of type \"" + getStorableType().getName() + "\": " + + joinProperty); + } + + ChainedProperty newProperty = joinProperty.append(getChainedProperty()); + + if (isConstant()) { + return getCanonical(newProperty, mOp, mConstant); + } else { + return getCanonical(newProperty, mOp, mBindID); + } + } + /** * Returns another PropertyFilter instance which is bound to the given constant value. * -- cgit v1.2.3