From 02cccbc0eabe65ce957eef52974660c0eaeb8b67 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 10 Sep 2009 03:41:30 +0000 Subject: [PATCH] I'm not going all the way down this path today, but now that we've hidden the implementation of operators and expressions, we can go the whole nine yards and eliminate the named classes entirely (except in cases where we need them for multiple dispatch by the visitors). Demonstrated this neatness with And and Or. --- src/java/com/samskivert/depot/KeySet.java | 3 +- src/java/com/samskivert/depot/Ops.java | 51 +++++++++++++-- .../com/samskivert/depot/clause/Where.java | 4 +- .../depot/expression/FluentExp.java | 7 +- .../samskivert/depot/impl/HSQLBuilder.java | 6 +- .../samskivert/depot/impl/operator/And.java | 65 ------------------- .../samskivert/depot/impl/operator/Or.java | 63 ------------------ 7 files changed, 54 insertions(+), 145 deletions(-) delete mode 100644 src/java/com/samskivert/depot/impl/operator/And.java delete mode 100644 src/java/com/samskivert/depot/impl/operator/Or.java diff --git a/src/java/com/samskivert/depot/KeySet.java b/src/java/com/samskivert/depot/KeySet.java index 1a83409..a9cf662 100644 --- a/src/java/com/samskivert/depot/KeySet.java +++ b/src/java/com/samskivert/depot/KeySet.java @@ -40,7 +40,6 @@ import com.samskivert.depot.impl.DepotUtil; import com.samskivert.depot.impl.ExpressionVisitor; import com.samskivert.depot.impl.expression.LiteralExp; import com.samskivert.depot.impl.operator.In; -import com.samskivert.depot.impl.operator.Or; /** * Contains a set of primary keys that match a set of persistent records. This is used internally @@ -225,7 +224,7 @@ public abstract class KeySet extends WhereClause for (Comparable[] kvals : _keys) { keyexps[ii++] = new Key.Expression(_pClass, kvals); } - return new Or(keyexps); + return Ops.or(keyexps); } // from Iterable> diff --git a/src/java/com/samskivert/depot/Ops.java b/src/java/com/samskivert/depot/Ops.java index ace3376..90cd187 100644 --- a/src/java/com/samskivert/depot/Ops.java +++ b/src/java/com/samskivert/depot/Ops.java @@ -26,12 +26,11 @@ import com.samskivert.depot.clause.SelectClause; import com.samskivert.depot.expression.FluentExp; import com.samskivert.depot.expression.SQLExpression; import com.samskivert.depot.impl.operator.Add; -import com.samskivert.depot.impl.operator.And; import com.samskivert.depot.impl.operator.Exists; import com.samskivert.depot.impl.operator.Like; import com.samskivert.depot.impl.operator.Mul; +import com.samskivert.depot.impl.operator.MultiOperator; import com.samskivert.depot.impl.operator.Not; -import com.samskivert.depot.impl.operator.Or; /** * Provides static methods for operator construction that don't fit nicely into the fluent style. @@ -52,7 +51,7 @@ public class Ops */ public static FluentExp and (Collection conditions) { - return new And(conditions); + return and(conditions.toArray(new SQLExpression[conditions.size()])); } /** @@ -60,7 +59,28 @@ public class Ops */ public static FluentExp and (SQLExpression... conditions) { - return new And(conditions); + return new MultiOperator(conditions) { + @Override public String operator() { + return " and "; + } + + @Override public Object evaluate (Object[] values) { + // if all operands are true, return true, else if all operands are boolean, return + // false, else return NO_VALUE + boolean allTrue = true; + for (Object value : values) { + if (value instanceof NoValue) { + return value; + } + if (Boolean.FALSE.equals(value)) { + allTrue = false; + } else if (!Boolean.TRUE.equals(value)) { + return new NoValue("Non-boolean operand to AND: " + value); + } + } + return allTrue; + } + }; } /** @@ -68,7 +88,7 @@ public class Ops */ public static FluentExp or (Collection conditions) { - return new Or(conditions); + return or(conditions.toArray(new SQLExpression[conditions.size()])); } /** @@ -76,7 +96,26 @@ public class Ops */ public static FluentExp or (SQLExpression... conditions) { - return new Or(conditions); + return new MultiOperator(conditions) { + @Override public String operator() { + return " or "; + } + + @Override public Object evaluate (Object[] values) { + boolean anyTrue = false; + for (Object value : values) { + if (value instanceof NoValue) { + return value; + } + if (Boolean.TRUE.equals(value)) { + anyTrue = true; + } else if (!Boolean.FALSE.equals(value)) { + return new NoValue("Non-boolean operand to OR: " + value); + } + } + return anyTrue; + } + }; } /** diff --git a/src/java/com/samskivert/depot/clause/Where.java b/src/java/com/samskivert/depot/clause/Where.java index 6ad8978..9d07d89 100644 --- a/src/java/com/samskivert/depot/clause/Where.java +++ b/src/java/com/samskivert/depot/clause/Where.java @@ -22,12 +22,12 @@ package com.samskivert.depot.clause; import java.util.Collection; +import com.samskivert.depot.Ops; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.expression.ColumnExp; import com.samskivert.depot.expression.SQLExpression; import com.samskivert.depot.impl.ExpressionVisitor; import com.samskivert.depot.impl.expression.ValueExp; -import com.samskivert.depot.impl.operator.And; import com.samskivert.depot.impl.operator.Equals; import com.samskivert.depot.impl.operator.IsNull; @@ -97,7 +97,7 @@ public class Where extends WhereClause comparisons[ii] = (values[ii] == null) ? new IsNull(columns[ii]) : new Equals(columns[ii], new ValueExp(values[ii])); } - return new And(comparisons); + return Ops.and(comparisons); } protected SQLExpression _condition; diff --git a/src/java/com/samskivert/depot/expression/FluentExp.java b/src/java/com/samskivert/depot/expression/FluentExp.java index 252bc84..232b870 100644 --- a/src/java/com/samskivert/depot/expression/FluentExp.java +++ b/src/java/com/samskivert/depot/expression/FluentExp.java @@ -20,8 +20,8 @@ package com.samskivert.depot.expression; +import com.samskivert.depot.Ops; import com.samskivert.depot.impl.operator.Add; -import com.samskivert.depot.impl.operator.And; import com.samskivert.depot.impl.operator.BitAnd; import com.samskivert.depot.impl.operator.BitOr; import com.samskivert.depot.impl.operator.Div; @@ -32,7 +32,6 @@ import com.samskivert.depot.impl.operator.LessThan; import com.samskivert.depot.impl.operator.LessThanEquals; import com.samskivert.depot.impl.operator.Mul; import com.samskivert.depot.impl.operator.NotEquals; -import com.samskivert.depot.impl.operator.Or; import com.samskivert.depot.impl.operator.Sub; /** @@ -117,13 +116,13 @@ public abstract class FluentExp /** Returns an {@link And} with this expression and the supplied target. */ public FluentExp and (SQLExpression expr) { - return new And(this, expr); + return Ops.and(this, expr); } /** Returns an {@link Or} with this expression and the supplied target. */ public FluentExp or (SQLExpression expr) { - return new Or(this, expr); + return Ops.or(this, expr); } /** Returns an {@link BitAnd} with this expression and the supplied target. */ diff --git a/src/java/com/samskivert/depot/impl/HSQLBuilder.java b/src/java/com/samskivert/depot/impl/HSQLBuilder.java index 01b4d0f..cb4ec0f 100644 --- a/src/java/com/samskivert/depot/impl/HSQLBuilder.java +++ b/src/java/com/samskivert/depot/impl/HSQLBuilder.java @@ -39,6 +39,7 @@ import com.samskivert.jdbc.ColumnDefinition; import com.samskivert.util.ArrayUtil; import com.samskivert.util.Tuple; +import com.samskivert.depot.Ops; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.annotation.FullTextIndex; import com.samskivert.depot.annotation.GeneratedValue; @@ -47,15 +48,14 @@ import com.samskivert.depot.expression.*; import com.samskivert.depot.operator.FullText; import com.samskivert.depot.impl.clause.CreateIndexClause; +import com.samskivert.depot.impl.expression.DateFun.DatePart.Part; import com.samskivert.depot.impl.expression.DateFun.DatePart; import com.samskivert.depot.impl.expression.DateFun.DateTruncate; -import com.samskivert.depot.impl.expression.DateFun.DatePart.Part; import com.samskivert.depot.impl.expression.StringFun.Lower; import com.samskivert.depot.impl.operator.BitAnd; import com.samskivert.depot.impl.operator.BitOr; import com.samskivert.depot.impl.operator.Like; import com.samskivert.depot.impl.operator.MultiOperator; -import com.samskivert.depot.impl.operator.Or; public class HSQLBuilder extends SQLBuilder @@ -90,7 +90,7 @@ public class HSQLBuilder } } // then just OR them all together and we have our query - _ftsCondition = new Or(bits); + _ftsCondition = Ops.or(bits); _ftsCondition.accept(this); return null; } diff --git a/src/java/com/samskivert/depot/impl/operator/And.java b/src/java/com/samskivert/depot/impl/operator/And.java deleted file mode 100644 index 27170e0..0000000 --- a/src/java/com/samskivert/depot/impl/operator/And.java +++ /dev/null @@ -1,65 +0,0 @@ -// -// $Id$ -// -// Depot library - a Java relational persistence library -// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Lesser General Public License as published -// by the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -package com.samskivert.depot.impl.operator; - -import java.util.Collection; - -import com.samskivert.depot.expression.SQLExpression; - -/** - * Represents a condition that is true iff all its subconditions are true. - */ -public class And extends MultiOperator -{ - public And (Collection conditions) - { - super(conditions.toArray(new SQLExpression[conditions.size()])); - } - - public And (SQLExpression... conditions) - { - super(conditions); - } - - @Override public String operator() - { - return " and "; - } - - @Override - public Object evaluate (Object[] values) - { - // if all operands are true, return true, else if all operands are boolean, return - // false, else return NO_VALUE - boolean allTrue = true; - for (Object value : values) { - if (value instanceof NoValue) { - return value; - } - if (Boolean.FALSE.equals(value)) { - allTrue = false; - } else if (!Boolean.TRUE.equals(value)) { - return new NoValue("Non-boolean operand to AND: " + value); - } - } - return allTrue; - } -} diff --git a/src/java/com/samskivert/depot/impl/operator/Or.java b/src/java/com/samskivert/depot/impl/operator/Or.java deleted file mode 100644 index f3509e9..0000000 --- a/src/java/com/samskivert/depot/impl/operator/Or.java +++ /dev/null @@ -1,63 +0,0 @@ -// -// $Id$ -// -// Depot library - a Java relational persistence library -// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Lesser General Public License as published -// by the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -package com.samskivert.depot.impl.operator; - -import java.util.Collection; - -import com.samskivert.depot.expression.SQLExpression; - -/** - * Represents a condition that is false iff all its subconditions are false. - */ -public class Or extends MultiOperator -{ - public Or (Collection conditions) - { - super(conditions.toArray(new SQLExpression[conditions.size()])); - } - - public Or (SQLExpression... conditions) - { - super(conditions); - } - - @Override public String operator() - { - return " or "; - } - - @Override - public Object evaluate (Object[] values) - { - boolean anyTrue = false; - for (Object value : values) { - if (value instanceof NoValue) { - return value; - } - if (Boolean.TRUE.equals(value)) { - anyTrue = true; - } else if (!Boolean.FALSE.equals(value)) { - return new NoValue("Non-boolean operand to OR: " + value); - } - } - return anyTrue; - } -}