Eliminate another dubious use of Tuple2.

If only Java didn't make it so damned hard to create a simple named class with
two or more values in it. Naming is generally a good thing, especially when the
alternative is Tuple2<SQLExpression<?>,SQLExpression<?>>.
This commit is contained in:
Michael Bayne
2015-02-10 12:57:26 -08:00
parent 40e6ccc833
commit 837daa2116
3 changed files with 26 additions and 21 deletions
@@ -38,7 +38,6 @@ import com.samskivert.depot.expression.*;
import com.samskivert.depot.operator.Case; import com.samskivert.depot.operator.Case;
import com.samskivert.depot.operator.FullText; import com.samskivert.depot.operator.FullText;
import com.samskivert.depot.util.ByteEnum; import com.samskivert.depot.util.ByteEnum;
import com.samskivert.depot.util.Tuple2;
import static com.samskivert.depot.Log.log; import static com.samskivert.depot.Log.log;
import com.samskivert.depot.impl.clause.*; import com.samskivert.depot.impl.clause.*;
@@ -176,11 +175,11 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
public Void visit (Case<?> caseExp) public Void visit (Case<?> caseExp)
{ {
_builder.append("(case "); _builder.append("(case ");
for (Tuple2<SQLExpression<?>, SQLExpression<?>> tuple : caseExp.getWhenExps()) { for (Case.Exp exp : caseExp.getWhenExps()) {
_builder.append(" when "); _builder.append(" when ");
tuple.a.accept(this); exp.when.accept(this);
_builder.append(" then "); _builder.append(" then ");
tuple.b.accept(this); exp.then.accept(this);
} }
SQLExpression<?> elseExp = caseExp.getElseExp(); SQLExpression<?> elseExp = caseExp.getElseExp();
if (elseExp != null) { if (elseExp != null) {
@@ -9,7 +9,6 @@ import java.util.Arrays;
import com.samskivert.depot.Key; import com.samskivert.depot.Key;
import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.util.Tuple2;
import com.samskivert.depot.clause.Distinct; import com.samskivert.depot.clause.Distinct;
import com.samskivert.depot.clause.FieldDefinition; import com.samskivert.depot.clause.FieldDefinition;
@@ -145,13 +144,13 @@ public class ExpressionEvaluator
public Object visit (Case<?> caseExp) public Object visit (Case<?> caseExp)
{ {
for (Tuple2<SQLExpression<?>, SQLExpression<?>> exp : caseExp.getWhenExps()) { for (Case.Exp exp : caseExp.getWhenExps()) {
Object result = exp.a.accept(this); Object result = exp.when.accept(this);
if (result instanceof NoValue || !(result instanceof Boolean)) { if (result instanceof NoValue || !(result instanceof Boolean)) {
return new NoValue("Failed to evaluate case: " + exp.a + " -> " + result); return new NoValue("Failed to evaluate case: " + exp.when + " -> " + result);
} }
if (((Boolean) result).booleanValue()) { if (((Boolean) result).booleanValue()) {
return exp.b.accept(this); return exp.then.accept(this);
} }
} }
SQLExpression<?> elseExp = caseExp.getElseExp(); SQLExpression<?> elseExp = caseExp.getElseExp();
@@ -12,24 +12,31 @@ import com.google.common.collect.Lists;
import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.SQLExpression; import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.FragmentVisitor; import com.samskivert.depot.impl.FragmentVisitor;
import com.samskivert.depot.util.Tuple2;
/** /**
* The SQL 'case' operator. * The SQL 'case' operator.
*/ */
public class Case<T> public class Case<T> implements SQLExpression<T>
implements SQLExpression<T>
{ {
public static class Exp {
public SQLExpression<?> when;
public SQLExpression<?> then;
public Exp (SQLExpression<?> when, SQLExpression<?> then) {
this.when = when;
this.then = then;
}
}
public Case (SQLExpression<?>... exps) public Case (SQLExpression<?>... exps)
{ {
int i = 0; int i = 0;
for (; i + 1 < exps.length; i += 2) { for (; i + 1 < exps.length; i += 2) {
_whenExps.add(Tuple2.<SQLExpression<?>, SQLExpression<?>>create(exps[i], exps[i + 1])); _whenExps.add(new Exp(exps[i], exps[i + 1]));
} }
_elseExp = (i < exps.length) ? exps[i] : null; _elseExp = (i < exps.length) ? exps[i] : null;
} }
public List<Tuple2<SQLExpression<?>, SQLExpression<?>>> getWhenExps () public List<Exp> getWhenExps ()
{ {
return _whenExps; return _whenExps;
} }
@@ -48,9 +55,9 @@ public class Case<T>
// from SQLExpression // from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet) public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{ {
for (Tuple2<SQLExpression<?>, SQLExpression<?>> tuple : _whenExps) { for (Exp exp : _whenExps) {
tuple.a.addClasses(classSet); exp.when.addClasses(classSet);
tuple.b.addClasses(classSet); exp.then.addClasses(classSet);
} }
if (_elseExp != null) { if (_elseExp != null) {
_elseExp.addClasses(classSet); _elseExp.addClasses(classSet);
@@ -62,9 +69,9 @@ public class Case<T>
{ {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("Case("); builder.append("Case(");
for (Tuple2<SQLExpression<?>, SQLExpression<?>> tuple : _whenExps) { for (Exp exp : _whenExps) {
builder.append(tuple.a.toString()).append("->"); builder.append(exp.when.toString()).append("->");
builder.append(tuple.b.toString()).append(","); builder.append(exp.then.toString()).append(",");
} }
if (_elseExp != null) { if (_elseExp != null) {
builder.append(_elseExp.toString()).append(")"); builder.append(_elseExp.toString()).append(")");
@@ -72,6 +79,6 @@ public class Case<T>
return builder.toString(); return builder.toString();
} }
protected List<Tuple2<SQLExpression<?>, SQLExpression<?>>> _whenExps = Lists.newArrayList(); protected List<Exp> _whenExps = Lists.newArrayList();
protected SQLExpression<?> _elseExp; protected SQLExpression<?> _elseExp;
} }