Skip to content

Commit

Permalink
消除ModelProperty类和其子类中不必要的类级别的范型参数,用范型方法替代
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed May 25, 2018
1 parent 7b4c199 commit 967b64e
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 174 deletions.
Expand Up @@ -111,7 +111,7 @@ public ExpressionBuilder<T> set(String propertyName, Value value) {
return this;
}

public ExpressionBuilder<T> eq(String propertyName, ModelProperty<?, ?> p) {
public ExpressionBuilder<T> eq(String propertyName, ModelProperty<?> p) {
ExpressionColumn left = model.getExpressionColumn(propertyName);
ExpressionColumn right = Model.getExpressionColumn(p);
Comparison c = new Comparison(getTable().getSession(), Comparison.EQUAL, left, right);
Expand Down
12 changes: 6 additions & 6 deletions lealone-orm/src/main/java/org/lealone/orm/Model.java
Expand Up @@ -75,7 +75,7 @@ public abstract class Model<T> {
private static final ConcurrentSkipListMap<Long, ServerSession> currentSessions = new ConcurrentSkipListMap<>();
private static final ConcurrentSkipListMap<Integer, List<ServerSession>> sessionMap = new ConcurrentSkipListMap<>();

private class PRowId extends PBaseNumber<T, Long, PRowId> {
private class PRowId extends PBaseNumber<T, Long> {

private long value;

Expand Down Expand Up @@ -291,13 +291,13 @@ public String getTableName() {
}

@SafeVarargs
public final T select(ModelProperty<?, ?>... properties) {
public final T select(ModelProperty<?>... properties) {
Model<T> m = maybeCopy();
if (m != this) {
return m.select(properties);
}
selectExpressions = new ArrayList<>();
for (ModelProperty<?, ?> p : properties) {
for (ModelProperty<?> p : properties) {
ExpressionColumn c = getExpressionColumn(p);
selectExpressions.add(c);
}
Expand All @@ -315,20 +315,20 @@ public T orderBy() {
}

@SafeVarargs
public final T groupBy(ModelProperty<?, ?>... properties) {
public final T groupBy(ModelProperty<?>... properties) {
Model<T> m = maybeCopy();
if (m != this) {
return m.groupBy(properties);
}
groupExpressions = new ArrayList<>();
for (ModelProperty<?, ?> p : properties) {
for (ModelProperty<?> p : properties) {
ExpressionColumn c = getExpressionColumn(p);
groupExpressions.add(c);
}
return root;
}

static ExpressionColumn getExpressionColumn(ModelProperty<?, ?> p) {
static ExpressionColumn getExpressionColumn(ModelProperty<?> p) {
return new ExpressionColumn(p.getDatabaseName(), p.getSchemaName(), p.getTableName(), p.getName());
}

Expand Down
36 changes: 20 additions & 16 deletions lealone-orm/src/main/java/org/lealone/orm/ModelProperty.java
Expand Up @@ -33,7 +33,7 @@
* @param <R> The type of the owning root bean
*/
@SuppressWarnings("unchecked")
public abstract class ModelProperty<R, P> {
public abstract class ModelProperty<R> {

protected final String name;
protected final R root;
Expand Down Expand Up @@ -72,10 +72,14 @@ protected Model<?> getModel() {
return ((Model<?>) root).maybeCopy();
}

protected P getModelProperty(Model<?> model) {
protected <P> P getModelProperty(Model<?> model) {
return (P) model.getModelProperty(name);
}

private ModelProperty<R> P(Model<?> model) {
return this.<ModelProperty<R>> getModelProperty(model);
}

/**
* Internal method to return the underlying expression builder.
*/
Expand All @@ -89,7 +93,7 @@ protected ExpressionBuilder<?> expr() {
public R isNull() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).isNull();
return P(model).isNull();
}
expr().isNull(name);
return root;
Expand All @@ -101,7 +105,7 @@ public R isNull() {
public R isNotNull() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).isNotNull();
return P(model).isNotNull();
}
expr().isNotNull(name);
return root;
Expand All @@ -113,7 +117,7 @@ public R isNotNull() {
public R asc() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).asc();
return P(model).asc();
}
expr().orderBy(name, false);
return root;
Expand All @@ -125,7 +129,7 @@ public R asc() {
public R desc() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).desc();
return P(model).desc();
}
expr().orderBy(name, true);
return root;
Expand All @@ -138,10 +142,17 @@ public String getName() {
return name;
}

public final R eq(ModelProperty<?, ?> p) {
protected String getFullName() {
if (fullName == null) {
fullName = getSchemaName() + "." + getTableName() + "." + name;
}
return fullName;
}

public final R eq(ModelProperty<?> p) {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).eq(p);
return P(model).eq(p);
}
expr().eq(name, p);
return root;
Expand All @@ -150,7 +161,7 @@ public final R eq(ModelProperty<?, ?> p) {
public R set(Object value) {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).set(value);
return P(model).set(value);
}
return root;
}
Expand All @@ -175,13 +186,6 @@ protected JsonNode getJsonNode(JsonNode node) {
return n;
}

protected String getFullName() {
if (fullName == null) {
fullName = getSchemaName() + "." + getTableName() + "." + name;
}
return fullName;
}

/**
* Helper method to check if two objects are equal.
*/
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <R> the root model bean type
*/
public class PArray<R> extends ModelProperty<R, PArray<R>> {
public class PArray<R> extends ModelProperty<R> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -24,7 +24,7 @@
* @param <T> the type of the scalar property
*/
@SuppressWarnings("rawtypes")
public abstract class PBaseComparable<R, T extends Comparable, P> extends PBaseValueEqual<R, T, P> {
public abstract class PBaseComparable<R, T extends Comparable> extends PBaseValueEqual<R, T> {

/**
* Construct with a property name and root instance.
Expand Down Expand Up @@ -93,48 +93,4 @@ public final R between(T lower, T upper) {
return root;
}

/**
* Greater than.
*
* @param value the bind value
* @return the root model bean instance
*/
public final R greaterThan(T value) {
expr().gt(name, value);
return root;
}

/**
* Greater than or Equal to.
*
* @param value the bind value
* @return the root model bean instance
*/
public final R greaterOrEqualTo(T value) {
expr().ge(name, value);
return root;
}

/**
* Less than.
*
* @param value the bind value
* @return the root model bean instance
*/
public final R lessThan(T value) {
expr().lt(name, value);
return root;
}

/**
* Less than or Equal to.
*
* @param value the bind value
* @return the root model bean instance
*/
public final R lessOrEqualTo(T value) {
expr().le(name, value);
return root;
}

}
Expand Up @@ -24,7 +24,7 @@
* @param <D> the date time type
*/
@SuppressWarnings("rawtypes")
public abstract class PBaseDate<R, D extends Comparable, P> extends PBaseComparable<R, D, P> {
public abstract class PBaseDate<R, D extends Comparable> extends PBaseComparable<R, D> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -24,7 +24,7 @@
* @param <T> the number type
*/
@SuppressWarnings("rawtypes")
public abstract class PBaseNumber<R, T extends Comparable, P> extends PBaseComparable<R, T, P> {
public abstract class PBaseNumber<R, T extends Comparable> extends PBaseComparable<R, T> {

/**
* Construct with a property name and root instance.
Expand All @@ -38,39 +38,6 @@ public PBaseNumber(String name, R root) {

// Additional int versions -- seems the right thing to do

/**
* Is equal to.
*
* @param value the equal to bind value
* @return the root model bean instance
*/
public R equalTo(int value) {
expr().eq(name, value);
return root;
}

/**
* Greater than.
*
* @param value the equal to bind value
* @return the root model bean instance
*/
public R greaterThan(int value) {
expr().gt(name, value);
return root;
}

/**
* Less than.
*
* @param value the equal to bind value
* @return the root model bean instance
*/
public R lessThan(int value) {
expr().lt(name, value);
return root;
}

/**
* Is equal to.
*
Expand Down

0 comments on commit 967b64e

Please sign in to comment.