Skip to content
Snippets Groups Projects
Commit e35683b2 authored by buczekp's avatar buczekp
Browse files

[LMS-1818] refactoring

SVN: 18322
parent 8f1e6836
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2010 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.generic.client.web.server.calculator;
import java.math.BigInteger;
import ch.systemsx.cisd.common.evaluator.Evaluator;
import ch.systemsx.cisd.common.evaluator.EvaluatorException;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
/**
* @author Piotr Buczek
*/
public class AbstractCalculator
{
protected final Evaluator evaluator;
public AbstractCalculator(Evaluator evaluator)
{
this.evaluator = evaluator;
}
public PrimitiveValue getTypedResult()
{
Object value = evaluator.eval();
if (value == null)
{
return PrimitiveValue.NULL;
}
if (value instanceof Long)
{
return new PrimitiveValue((Long) value);
} else if (value instanceof Double)
{
return new PrimitiveValue((Double) value);
} else
{
return new PrimitiveValue(value.toString());
}
}
public boolean evalToBoolean() throws EvaluatorException
{
return evaluator.evalToBoolean();
}
public int evalToInt() throws EvaluatorException
{
return evaluator.evalToInt();
}
public BigInteger evalToBigInt() throws EvaluatorException
{
return evaluator.evalToBigInt();
}
public double evalToDouble() throws EvaluatorException
{
return evaluator.evalToDouble();
}
public String evalAsString() throws EvaluatorException
{
return evaluator.evalAsString();
}
}
\ No newline at end of file
......@@ -14,12 +14,31 @@
* limitations under the License.
*/
package ch.systemsx.cisd.openbis.generic.client.web.server.calculator.property;
package ch.systemsx.cisd.openbis.generic.client.web.server.calculator;
import ch.systemsx.cisd.common.evaluator.Evaluator;
import ch.systemsx.cisd.openbis.generic.client.web.server.calculator.property.IEntityAdaptor;
/**
* @author Piotr Buczek
*/
public class DynamicPropertyCalculator
public class DynamicPropertyCalculator extends AbstractCalculator
{
// TODO
private static final String INITIAL_SCRIPT = "from "
+ StandardFunctions.class.getCanonicalName() + " import *\n"
+ "def int(x):return toInt(x)\n "
+ "def float(x):return toFloat(x)\n ";
private static final String ENTITY_VARIABLE_NAME = "entity";
public DynamicPropertyCalculator(String expression)
{
super(new Evaluator(expression, Math.class, INITIAL_SCRIPT));
}
public void setEntity(IEntityAdaptor entity)
{
evaluator.set(ENTITY_VARIABLE_NAME, entity);
}
}
......@@ -16,27 +16,23 @@
package ch.systemsx.cisd.openbis.generic.client.web.server.calculator;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import ch.systemsx.cisd.common.evaluator.Evaluator;
import ch.systemsx.cisd.common.evaluator.EvaluatorException;
import ch.systemsx.cisd.common.utilities.Template;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ParameterWithValue;
/**
* @author Franz-Josef Elmer
*/
class RowCalculator
class RowCalculator extends AbstractCalculator
{
private static final String INITIAL_SCRIPT = "from "
+ StandardFunctions.class.getCanonicalName() + " import *\n"
+ "def int(x):return toInt(x)\n" + "def float(x):return toFloat(x)\n";
private final Evaluator evaluator;
+ "def int(x):return toInt(x)\n "
+ "def float(x):return toFloat(x)\n ";
private final Row row;
......@@ -48,9 +44,8 @@ class RowCalculator
public RowCalculator(ITableDataProvider provider, String expression,
Set<ParameterWithValue> parameters)
{
evaluator =
new Evaluator(substitudeParameters(expression, parameters), Math.class,
INITIAL_SCRIPT);
super(new Evaluator(substitudeParameters(expression, parameters), Math.class,
INITIAL_SCRIPT));
row = new Row(provider);
evaluator.set("row", row);
}
......@@ -60,51 +55,7 @@ class RowCalculator
row.setRowData(rowValues);
}
public PrimitiveValue getTypedResult()
{
Object value = evaluator.eval();
if (value == null)
{
return PrimitiveValue.NULL;
}
if (value instanceof Long)
{
return new PrimitiveValue((Long) value);
} else if (value instanceof Double)
{
return new PrimitiveValue((Double) value);
} else
{
return new PrimitiveValue(value.toString());
}
}
public boolean evalToBoolean() throws EvaluatorException
{
return evaluator.evalToBoolean();
}
public int evalToInt() throws EvaluatorException
{
return evaluator.evalToInt();
}
public BigInteger evalToBigInt() throws EvaluatorException
{
return evaluator.evalToBigInt();
}
public double evalToDouble() throws EvaluatorException
{
return evaluator.evalToDouble();
}
public String evalAsString() throws EvaluatorException
{
return evaluator.evalAsString();
}
private String substitudeParameters(String originalExpression,
private static String substitudeParameters(String originalExpression,
Set<ParameterWithValue> parameters)
{
Template template = new Template(originalExpression);
......
......@@ -20,6 +20,8 @@ import java.util.Collection;
/**
* Interface implemented by all entity adaptors
* <p>
* All methods of this interface are part of the Dynamic Properties API.
*
* @author Piotr Buczek
*/
......
......@@ -37,7 +37,7 @@ public class SampleAdaptor implements IEntityAdaptor
private final SamplePE samplePE;
SampleAdaptor(SamplePE samplePE)
public SampleAdaptor(SamplePE samplePE)
{
this.samplePE = samplePE;
this.code = samplePE.getCode();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment