diff --git a/pom.xml b/pom.xml index ac472cffd..a71ca46e2 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ - + 4.0.0 @@ -251,6 +251,7 @@ BACKWARD_COMPATIBLE_USER true true + 4.0.0 @@ -693,4 +694,4 @@ - + diff --git a/querydsl-core/src/main/java/com/querydsl/core/types/dsl/CaseForEqBuilder.java b/querydsl-core/src/main/java/com/querydsl/core/types/dsl/CaseForEqBuilder.java index fe038c9cc..06d56dcd6 100644 --- a/querydsl-core/src/main/java/com/querydsl/core/types/dsl/CaseForEqBuilder.java +++ b/querydsl-core/src/main/java/com/querydsl/core/types/dsl/CaseForEqBuilder.java @@ -13,15 +13,14 @@ */ package com.querydsl.core.types.dsl; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Date; import java.util.List; import javax.annotation.Nullable; -import com.querydsl.core.types.ConstantImpl; -import com.querydsl.core.types.Expression; -import com.querydsl.core.types.NullExpression; -import com.querydsl.core.types.Ops; +import com.querydsl.core.types.*; /** * {@code CaseForEqBuilder} enables the construction of typesafe case-when-then-else constructs @@ -78,14 +77,34 @@ public final class CaseForEqBuilder { this.other = other; } - public Cases> then(Expression then) { - type = then.getType(); + public Cases> then(Expression expr) { + if (expr instanceof Predicate) { + return (Cases) then((Predicate) expr); + } else if (expr instanceof StringExpression) { + return (Cases) then((StringExpression) expr); + } else if (expr instanceof NumberExpression) { + return then((NumberExpression) expr); + } else if (expr instanceof DateExpression) { + return then((DateExpression) expr); + } else if (expr instanceof DateTimeExpression) { + return then((DateTimeExpression) expr); + } else if (expr instanceof TimeExpression) { + return then((TimeExpression) expr); + } else if (expr instanceof ComparableExpression) { + return then((ComparableExpression) expr); + } else { + return thenSimple(expr); + } + } + + private Cases> thenSimple(Expression expr) { + type = expr.getType(); return new Cases>() { @Override protected Expression createResult(Class type, Expression last) { return Expressions.operation(type, Ops.CASE_EQ, base, last); } - }.when(other).then(then); + }.when(other).then(expr); } public Cases> then(T then) { @@ -96,7 +115,118 @@ public final class CaseForEqBuilder { return then((Expression) NullExpression.DEFAULT); } - public > Cases> then(T then) { + // Boolean + + public Cases then(Boolean then) { + return thenBoolean(ConstantImpl.create(then)); + } + + public Cases then(BooleanExpression then) { + return thenBoolean(then); + } + + private Cases thenBoolean(Expression then) { + type = then.getType(); + return new Cases() { + @Override + protected BooleanExpression createResult(Class type, Expression last) { + return Expressions.booleanOperation(Ops.CASE_EQ, base, last); + } + + }.when(other).then(then); + } + + // Comparable + + public Cases> then(T then) { + return thenComparable(ConstantImpl.create(then)); + } + + public Cases> then(ComparableExpression then) { + return thenComparable(then); + } + + private Cases> thenComparable(Expression then) { + type = then.getType(); + return new Cases>() { + @Override + protected ComparableExpression createResult(Class type, Expression last) { + return Expressions.comparableOperation(type, Ops.CASE_EQ, base, last); + } + + }.when(other).then(then); + } + + // Date + + public Cases> then(java.sql.Date then) { + return thenDate(ConstantImpl.create(then)); + } + + public Cases> then(DateExpression then) { + return thenDate(then); + } + + private Cases> thenDate(Expression then) { + type = then.getType(); + return new Cases>() { + @Override + protected DateExpression createResult(Class type, Expression last) { + return Expressions.dateOperation(type, Ops.CASE_EQ, base, last); + } + + }.when(other).then(then); + } + + // DateTime + + public Cases> then(Date then) { + return thenDateTime(ConstantImpl.create(then)); + } + + public Cases> then(Timestamp then) { + return thenDateTime(ConstantImpl.create(then)); + } + + public Cases> then(DateTimeExpression then) { + return thenDateTime(then); + } + + private Cases> thenDateTime(Expression then) { + type = then.getType(); + return new Cases>() { + @Override + protected DateTimeExpression createResult(Class type, Expression last) { + return Expressions.dateTimeOperation(type, Ops.CASE_EQ, base, last); + } + + }.when(other).then(then); + } + + // Enum + + public > Cases> then(T then) { + return thenEnum(ConstantImpl.create(then)); + } + + public > Cases> then(EnumExpression then) { + return thenEnum(then); + } + + private > Cases> thenEnum(Expression then) { + type = then.getType(); + return new Cases>() { + @Override + protected EnumExpression createResult(Class type, Expression last) { + return Expressions.enumOperation(type, Ops.CASE_EQ, base, last); + } + + }.when(other).then(then); + } + + // Number + + public > Cases> then(T then) { return thenNumber(ConstantImpl.create(then)); } @@ -116,14 +246,16 @@ public final class CaseForEqBuilder { }.when(other).then(then); } - public Cases then(StringExpression then) { - return thenString(then); - } + // String public Cases then(String then) { return thenString(ConstantImpl.create(then)); } + public Cases then(StringExpression then) { + return thenString(then); + } + private Cases thenString(Expression then) { type = then.getType(); return new Cases() { @@ -136,6 +268,27 @@ public final class CaseForEqBuilder { }.when(other).then(then); } + // Time + + public Cases> then(java.sql.Time then) { + return thenTime(ConstantImpl.create(then)); + } + + public Cases> then(TimeExpression then) { + return thenTime(then); + } + + private Cases> thenTime(Expression then) { + type = then.getType(); + return new Cases>() { + @Override + protected TimeExpression createResult(Class type, Expression last) { + return Expressions.timeOperation(type, Ops.CASE_EQ, base, last); + } + + }.when(other).then(then); + } + /** * Intermediate step * diff --git a/querydsl-core/src/test/java/com/querydsl/core/types/CaseBuilderTest.java b/querydsl-core/src/test/java/com/querydsl/core/types/dsl/CaseBuilderTest.java similarity index 98% rename from querydsl-core/src/test/java/com/querydsl/core/types/CaseBuilderTest.java rename to querydsl-core/src/test/java/com/querydsl/core/types/dsl/CaseBuilderTest.java index 130462438..715d473f1 100644 --- a/querydsl-core/src/test/java/com/querydsl/core/types/CaseBuilderTest.java +++ b/querydsl-core/src/test/java/com/querydsl/core/types/dsl/CaseBuilderTest.java @@ -11,17 +11,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.querydsl.core.types; +package com.querydsl.core.types.dsl; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static com.querydsl.core.alias.Alias.$; import static com.querydsl.core.alias.Alias.alias; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import org.junit.Test; -import com.querydsl.core.types.dsl.*; - public class CaseBuilderTest { public enum Gender { diff --git a/querydsl-core/src/test/java/com/querydsl/core/types/CaseForEqBuilderTest.java b/querydsl-core/src/test/java/com/querydsl/core/types/dsl/CaseForEqBuilderTest.java similarity index 61% rename from querydsl-core/src/test/java/com/querydsl/core/types/CaseForEqBuilderTest.java rename to querydsl-core/src/test/java/com/querydsl/core/types/dsl/CaseForEqBuilderTest.java index c59e3abb0..a12cc5229 100644 --- a/querydsl-core/src/test/java/com/querydsl/core/types/CaseForEqBuilderTest.java +++ b/querydsl-core/src/test/java/com/querydsl/core/types/dsl/CaseForEqBuilderTest.java @@ -11,20 +11,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.querydsl.core.types; +package com.querydsl.core.types.dsl; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static com.querydsl.core.alias.Alias.$; import static com.querydsl.core.alias.Alias.alias; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.sql.Time; import org.junit.Test; -import com.querydsl.core.types.dsl.NumberExpression; -import com.querydsl.core.types.dsl.StringExpression; - public class CaseForEqBuilderTest { + public enum EnumExample { A, B } + public static class Customer { private long annualSpending; public long getAnnualSpending() { @@ -69,12 +70,56 @@ public class CaseForEqBuilderTest { public void BooleanTyped() { Customer c = alias(Customer.class, "customer"); - Expression cases = $(c.getAnnualSpending()) + BooleanExpression cases = $(c.getAnnualSpending()) .when(1000L).then(true) .otherwise(false); assertNotNull(cases); - } + @Test + public void DateType() { + Customer c = alias(Customer.class, "customer"); + + DateExpression cases = $(c.getAnnualSpending()) + .when(1000L).then(new java.sql.Date(0)) + .otherwise(new java.sql.Date(0)); + + assertNotNull(cases); + } + + @Test + public void DateTimeType() { + Customer c = alias(Customer.class, "customer"); + + DateTimeExpression cases = $(c.getAnnualSpending()) + .when(1000L).then(new java.util.Date(0)) + .otherwise(new java.util.Date(0)); + + assertNotNull(cases); + } + + @Test + public void TimeType() { + Customer c = alias(Customer.class, "customer"); + + TimeExpression