Reputation: 105
so im trying to implement the addition operator in my simpleDB database. ive got most of it done but i have no idea how to add the 2 parts. so this would be used in a query for the database , and i want to be able to add the left hand side and right hand side. what java methods can use to do that? since in the simpleDB there is no addition operator.
this was how i implemented the < operator
if (operator.equals("<") && lhsval.compareTo(rhsval) < 0) {
return true;
}
like right now i have
Constant lhsval = lhs.evaluate(s);
Constant rhsval = rhs.evaluate(s);
if (operator.equals("+")) {
// return ;
}
i have the left hand side and the right side so when the query sees the + sign , its goin to add em togther and return the answer. but i have no idea how. when i implemented the less than operator, i used the comprateTo method and when i did the equal operator i used the equals method. i have no idea what i can use for the addition operator.btw this is using java
constant class
package simpledb.query;
/**
* The interface that denotes values stored in the database.
* @author Edward Sciore
*/
public interface Constant extends Comparable<Constant> {
/**
* Returns the Java object corresponding to this constant.
* @return the Java value of the constant
*/
public Object asJavaVal();
}
Upvotes: 0
Views: 111
Reputation: 56
I think, it is better to check the type of lhsval and rhsval. After that you can get values of known type by asJavaVal:
Constant lhsval = lhs.evaluate(s);
Constant rhsval = rhs.evaluate(s);
Constant res = null;
if (operator.equals("+")) {
if (lhsval instanceof IntConstant) {
int sum = (Integer) lhsval.asJavaVal() + (Integer) rhsval.asJavaVal();
res = new IntConstant(sum);
} else if (lhsval instanceof StringConstant) {
String sum = ((String) lhsval.asJavaVal()).concat((String) rhsval.asJavaVal());
res = new StringConstant(sum);
} else {
throw new IllegalArgumentException("Unknown constant type");
}
}
Upvotes: 0
Reputation: 1234
A relational operator such as A lower than B has been implemented because the type Constant implements Comparable.
If you want to implement a relational operator such as A + B then the type Constant has to implement a type such as Addable? I think yes, it has to.
Constant lhsval = lhs.evaluate(s);
Constant rhsval = rhs.evaluate(s);
if (operator.equals("+")) {
return lhsval.add(rhsval);
}
The Addable interface has this contract:
interface Addable extends RelationalOperator {
Constant add(Constant lhs, Constant rhs);
}
You have to decide how the add operator has to work (concatenate, sum, depends on Constant subtype, ...)
Edited after update on question
A simple implementation is to use the method Object asJavaVal();
as follow:
Constant lhsval = lhs.evaluate(s);
Constant rhsval = rhs.evaluate(s);
if (operator.equals("+")) {
Object lhsObj = lhs.asJavaVal();
Object rhsObj = lhs.asJavaVal();
// here check for null and same type of lhs, rhs
...
// now use typed implementation of +
if (lhsObj instanceof BigDecimal)
return new ConstantImpl(lhsval.add(rhsval));
else if (lhsObj instanceof String)
return new ConstantImpl(lhsval + rhsval);
else if (lhsObj instanceof ...)
return new ConstantImpl(...);
else
throw new IllegalArgumentException("Not a valid type for + :"+ lhsObj.getClass());
}
Upvotes: 1