Skip to content

Commit

Permalink
Add setter builders to entity fields. This will later be used to opti…
Browse files Browse the repository at this point in the history
…mize update operations in like described in issue #27.
  • Loading branch information
Emil Forslund committed Aug 24, 2015
1 parent 413f109 commit 61b938f
Show file tree
Hide file tree
Showing 25 changed files with 348 additions and 39 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/speedment/core/field/BaseFunction.java
@@ -0,0 +1,11 @@
package com.speedment.core.field;

import java.util.function.Function;

/**
*
* @author Emil
*/
public abstract class BaseFunction<ENTITY> implements Function<ENTITY, ENTITY> {

}
Expand Up @@ -27,5 +27,5 @@ public interface BinaryPredicateBuilder<ENTITY, V> extends PredicateBuilder<ENTI
V getValueAsObject(); V getValueAsObject();


@Override @Override
public BinaryOperator getOperator(); BinaryOperator getOperator();
} }
12 changes: 12 additions & 0 deletions src/main/java/com/speedment/core/field/FunctionBuilder.java
@@ -0,0 +1,12 @@
package com.speedment.core.field;

/**
*
* @author Emil Forslund
* @param <ENTITY> Entity type
*/
public interface FunctionBuilder<ENTITY> {

Field<ENTITY> getField();

}
Expand Up @@ -34,7 +34,7 @@ public enum StandardBinaryOperator implements BinaryOperator {


private final IntPredicate comparator; private final IntPredicate comparator;


private StandardBinaryOperator(IntPredicate comparator) { StandardBinaryOperator(IntPredicate comparator) {
this.comparator = Objects.requireNonNull(comparator); this.comparator = Objects.requireNonNull(comparator);
} }


Expand Down
Expand Up @@ -33,7 +33,7 @@ public enum StandardStringBinaryOperator implements BinaryOperator {


private final BiPredicate<String, String> biPredicate; private final BiPredicate<String, String> biPredicate;


private StandardStringBinaryOperator(BiPredicate<String, String> biPredicate) { StandardStringBinaryOperator(BiPredicate<String, String> biPredicate) {
this.biPredicate = Objects.requireNonNull(biPredicate); this.biPredicate = Objects.requireNonNull(biPredicate);
} }


Expand Down
Expand Up @@ -30,7 +30,7 @@ public enum StandardUnaryOperator implements Operator {


private final Predicate<Object> predicate; private final Predicate<Object> predicate;


private StandardUnaryOperator(Predicate<Object> predicate) { StandardUnaryOperator(Predicate<Object> predicate) {
this.predicate = Objects.requireNonNull(predicate); this.predicate = Objects.requireNonNull(predicate);
} }


Expand Down
Expand Up @@ -24,6 +24,6 @@
public interface UnaryPredicateBuilder<ENTITY> extends PredicateBuilder<ENTITY> { public interface UnaryPredicateBuilder<ENTITY> extends PredicateBuilder<ENTITY> {


@Override @Override
public StandardUnaryOperator getOperator(); StandardUnaryOperator getOperator();


} }
14 changes: 12 additions & 2 deletions src/main/java/com/speedment/core/field/doubles/DoubleField.java
Expand Up @@ -33,9 +33,11 @@ public class DoubleField<ENTITY> implements Field<ENTITY> {


private final Supplier<Column> columnSupplier; private final Supplier<Column> columnSupplier;
private final ToDoubleFunction<ENTITY> getter; private final ToDoubleFunction<ENTITY> getter;
private final DoubleSetter<ENTITY> setter;


public DoubleField(Supplier<Column> columnSupplier, ToDoubleFunction<ENTITY> getter) { public DoubleField(Supplier<Column> columnSupplier, ToDoubleFunction<ENTITY> getter, DoubleSetter<ENTITY> setter) {
this.getter = getter; this.getter = getter;
this.setter = setter;
this.columnSupplier = columnSupplier; this.columnSupplier = columnSupplier;
} }


Expand Down Expand Up @@ -117,6 +119,10 @@ public DoubleBinaryPredicateBuilder<ENTITY> greaterOrEqual(double value) {
return newBinary(value, StandardBinaryOperator.GREATER_OR_EQUAL); return newBinary(value, StandardBinaryOperator.GREATER_OR_EQUAL);
} }


public DoubleFunctionBuilder<ENTITY> set(double value) {
return new DoubleFunctionBuilder<>(this, value);
}

@Override @Override
public boolean isNullIn(ENTITY entity) { public boolean isNullIn(ENTITY entity) {
return false; return false;
Expand All @@ -126,6 +132,10 @@ public double getFrom(ENTITY entity) {
return getter.applyAsDouble(entity); return getter.applyAsDouble(entity);
} }


public ENTITY setIn(ENTITY entity, double value) {
return setter.applyAsDouble(entity, value);
}

@Override @Override
public Column getColumn() { public Column getColumn() {
return columnSupplier.get(); return columnSupplier.get();
Expand Down
@@ -0,0 +1,56 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.core.field.doubles;

import com.speedment.core.field.BaseFunction;
import com.speedment.core.field.FunctionBuilder;
import com.speedment.core.field.ints.IntField;

import static java.util.Objects.requireNonNull;

/**
*
* @author pemi
* @param <ENTITY> Entity type
*/
public class DoubleFunctionBuilder<ENTITY> extends BaseFunction<ENTITY> implements FunctionBuilder<ENTITY> {

private final DoubleField<ENTITY> field;
private final double newValue;

public DoubleFunctionBuilder(
final DoubleField<ENTITY> field,
final double newValue
) {
this.field = requireNonNull(field);
this.newValue = requireNonNull(newValue);
}

@Override
public ENTITY apply(ENTITY entity) {
return field.setIn(entity, newValue);
}

@Override
public DoubleField<ENTITY> getField() {
return field;
}

public double getValue() {
return newValue;
}
}
@@ -0,0 +1,9 @@
package com.speedment.core.field.doubles;

/**
* @author Emil Forslund
* @param <ENTITY> The entity type
*/
public interface DoubleSetter<ENTITY> {
ENTITY applyAsDouble(ENTITY entity, double value);
}
18 changes: 15 additions & 3 deletions src/main/java/com/speedment/core/field/ints/IntField.java
Expand Up @@ -23,6 +23,8 @@
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.function.ToIntFunction; import java.util.function.ToIntFunction;


import static java.util.Objects.requireNonNull;

/** /**
* This class represents an {@code int} Field. * This class represents an {@code int} Field.
* *
Expand All @@ -33,10 +35,12 @@ public class IntField<ENTITY> implements Field<ENTITY> {


private final Supplier<Column> columnSupplier; private final Supplier<Column> columnSupplier;
private final ToIntFunction<ENTITY> getter; private final ToIntFunction<ENTITY> getter;
private final IntSetter<ENTITY> setter;


public IntField(Supplier<Column> columnSupplier, ToIntFunction<ENTITY> getter) { public IntField(Supplier<Column> columnSupplier, ToIntFunction<ENTITY> getter, IntSetter<ENTITY> setter) {
this.getter = getter; this.getter = requireNonNull(getter);
this.columnSupplier = columnSupplier; this.setter = requireNonNull(setter);
this.columnSupplier = requireNonNull(columnSupplier);
} }


/** /**
Expand Down Expand Up @@ -117,6 +121,10 @@ public IntBinaryPredicateBuilder<ENTITY> greaterOrEqual(int value) {
return newBinary(value, StandardBinaryOperator.GREATER_OR_EQUAL); return newBinary(value, StandardBinaryOperator.GREATER_OR_EQUAL);
} }


public IntFunctionBuilder<ENTITY> set(int value) {
return new IntFunctionBuilder<>(this, value);
}

@Override @Override
public boolean isNullIn(ENTITY entity) { public boolean isNullIn(ENTITY entity) {
return false; return false;
Expand All @@ -126,6 +134,10 @@ public int getFrom(ENTITY entity) {
return getter.applyAsInt(entity); return getter.applyAsInt(entity);
} }


public ENTITY setIn(ENTITY entity, int value) {
return setter.applyAsInt(entity, value);
}

@Override @Override
public Column getColumn() { public Column getColumn() {
return columnSupplier.get(); return columnSupplier.get();
Expand Down
@@ -0,0 +1,55 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.core.field.ints;

import com.speedment.core.field.BaseFunction;
import com.speedment.core.field.FunctionBuilder;

import static java.util.Objects.requireNonNull;

/**
*
* @author pemi
* @param <ENTITY> Entity type
*/
public class IntFunctionBuilder<ENTITY> extends BaseFunction<ENTITY> implements FunctionBuilder<ENTITY> {

private final IntField<ENTITY> field;
private final int newValue;

public IntFunctionBuilder(
final IntField<ENTITY> field,
final int newValue
) {
this.field = requireNonNull(field);
this.newValue = requireNonNull(newValue);
}

@Override
public ENTITY apply(ENTITY entity) {
return field.setIn(entity, newValue);
}

@Override
public IntField<ENTITY> getField() {
return field;
}

public int getValue() {
return newValue;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/speedment/core/field/ints/IntSetter.java
@@ -0,0 +1,9 @@
package com.speedment.core.field.ints;

/**
* @author Emil Forslund
* @param <ENTITY> The entity type
*/
public interface IntSetter<ENTITY> {
ENTITY applyAsInt(ENTITY entity, int value);
}
19 changes: 16 additions & 3 deletions src/main/java/com/speedment/core/field/longs/LongField.java
Expand Up @@ -20,9 +20,12 @@
import com.speedment.core.field.Field; import com.speedment.core.field.Field;
import com.speedment.core.field.StandardBinaryOperator; import com.speedment.core.field.StandardBinaryOperator;
import com.speedment.core.field.StandardUnaryOperator; import com.speedment.core.field.StandardUnaryOperator;

import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.function.ToLongFunction; import java.util.function.ToLongFunction;


import static java.util.Objects.requireNonNull;

/** /**
* This class represents a {@code long} Field. * This class represents a {@code long} Field.
* *
Expand All @@ -33,10 +36,12 @@ public class LongField<ENTITY> implements Field<ENTITY> {


private final Supplier<Column> columnSupplier; private final Supplier<Column> columnSupplier;
private final ToLongFunction<ENTITY> getter; private final ToLongFunction<ENTITY> getter;
private final LongSetter<ENTITY> setter;


public LongField(Supplier<Column> columnSupplier, ToLongFunction<ENTITY> getter) { public LongField(Supplier<Column> columnSupplier, ToLongFunction<ENTITY> getter, LongSetter<ENTITY> setter) {
this.getter = getter; this.getter = requireNonNull(getter);
this.columnSupplier = columnSupplier; this.setter = requireNonNull(setter);
this.columnSupplier = requireNonNull(columnSupplier);
} }


/** /**
Expand Down Expand Up @@ -117,6 +122,10 @@ public LongBinaryPredicateBuilder<ENTITY> greaterOrEqual(long value) {
return newBinary(value, StandardBinaryOperator.GREATER_OR_EQUAL); return newBinary(value, StandardBinaryOperator.GREATER_OR_EQUAL);
} }


public LongFunctionBuilder<ENTITY> set(long value) {
return new LongFunctionBuilder<>(this, value);
}

@Override @Override
public boolean isNullIn(ENTITY entity) { public boolean isNullIn(ENTITY entity) {
return false; return false;
Expand All @@ -126,6 +135,10 @@ public long getFrom(ENTITY entity) {
return getter.applyAsLong(entity); return getter.applyAsLong(entity);
} }


public ENTITY setIn(ENTITY entity, long value) {
return setter.applyAsLong(entity, value);
}

@Override @Override
public Column getColumn() { public Column getColumn() {
return columnSupplier.get(); return columnSupplier.get();
Expand Down
@@ -0,0 +1,55 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.core.field.longs;

import com.speedment.core.field.BaseFunction;
import com.speedment.core.field.FunctionBuilder;

import static java.util.Objects.requireNonNull;

/**
*
* @author pemi
* @param <ENTITY> Entity type
*/
public class LongFunctionBuilder<ENTITY> extends BaseFunction<ENTITY> implements FunctionBuilder<ENTITY> {

private final LongField<ENTITY> field;
private final long newValue;

public LongFunctionBuilder(
final LongField<ENTITY> field,
final long newValue
) {
this.field = requireNonNull(field);
this.newValue = requireNonNull(newValue);
}

@Override
public ENTITY apply(ENTITY entity) {
return field.setIn(entity, newValue);
}

@Override
public LongField<ENTITY> getField() {
return field;
}

public long getValue() {
return newValue;
}
}

0 comments on commit 61b938f

Please sign in to comment.