Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use an interface to designate the order-by direction, instead of expo…

…sing a public method
  • Loading branch information...
commit bdad7e65a6091c95c155d70af77d0024a6074457 1 parent d916d43
Kazuho Oku authored
7 net/examp1e/picoorm/Condition.java
View
@@ -9,7 +9,10 @@
public class Condition<Row extends AbstractRow> {
public static abstract class OrderBy {
- public abstract String toOrderBySQL();
+ public abstract String getFieldName();
+ }
+
+ public static interface OrderByIsDescending {
}
final TableDefinition<Row> tableDefinition;
@@ -41,7 +44,7 @@ public Condition(TableDefinition<Row> tableDefinition, String term, AnyType... p
public Condition<Row> orderBy(OrderBy... orders) {
for (OrderBy order : orders) {
- orderBy.add(order.toOrderBySQL());
+ orderBy.add(order.getFieldName() + (order instanceof OrderByIsDescending ? " DESC" : " ASC"));
}
return this;
}
25 net/examp1e/picoorm/types/AnyType.java
View
@@ -16,23 +16,24 @@
public static abstract class Predicate<Row extends AbstractRow> extends Condition.OrderBy {
- public static class OrderPredicate<Row extends AbstractRow> extends Condition.OrderBy {
- Predicate<Row> predicate;
- boolean isAsc;
- OrderPredicate(Predicate<Row> predicate, boolean isAsc) {
+ private static class OrderPredicate<Row extends AbstractRow> extends Condition.OrderBy {
+ private Predicate<Row> predicate;
+ OrderPredicate<Row> init(Predicate<Row> predicate) {
this.predicate = predicate;
- this.isAsc = isAsc;
+ return this;
}
@Override
- public String toOrderBySQL() {
- return this.predicate.fieldName + (isAsc ? " ASC" : " DESC");
+ public String getFieldName() {
+ return this.predicate.getFieldName();
}
}
+ private static class DescendingOrderPredicate<Row extends AbstractRow> extends OrderPredicate<Row> implements Condition.OrderByIsDescending {
+ }
protected TableDefinition<Row> tableDefinition;
protected String fieldName;
- public final OrderPredicate<Row> asc = new OrderPredicate<Row>(this, true);
- public final OrderPredicate<Row> desc = new OrderPredicate<Row>(this, false);
+ public final OrderPredicate<Row> asc = new OrderPredicate<Row>().init(this);
+ public final OrderPredicate<Row> desc = new DescendingOrderPredicate<Row>().init(this);
protected void _init(TableDefinition<Row> tableDefinition, String fieldName) {
this.tableDefinition = tableDefinition;
@@ -40,14 +41,10 @@ protected void _init(TableDefinition<Row> tableDefinition, String fieldName) {
tableDefinition.addColumnDefinition(this);
}
+ @Override
public String getFieldName() {
return fieldName;
}
-
- @Override
- public String toOrderBySQL() {
- return this.asc.toOrderBySQL();
- }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.