KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > lib > Round


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  *
20  * Authors: S.Chassande-Barrioz.
21  * Created on 15 févr. 2005
22  *
23  */

24 package org.objectweb.medor.expression.lib;
25
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.jorm.type.api.PTypeSpace;
28 import org.objectweb.medor.expression.api.Expression;
29 import org.objectweb.medor.expression.api.ExpressionException;
30 import org.objectweb.medor.expression.api.MalformedExpressionException;
31 import org.objectweb.medor.expression.api.Operand;
32 import org.objectweb.medor.expression.api.ParameterOperand;
33
34 /**
35  *
36  *
37  * @author S.Chassande-Barrioz
38  */

39 public class Round extends BasicOperator {
40     
41     int round = 0;
42     
43     public Round() {
44     }
45
46     public Round(Expression e) {
47         this(e,0);
48     }
49
50     public Round(Expression e, int r) {
51         super(new Expression[]{e});
52         setRound(r);
53     }
54
55     public int getRound() {
56         return round;
57     }
58     
59     public void setRound(int r) {
60         round = r;
61     }
62     
63     public Operand compileExpression() throws ExpressionException,
64             MalformedExpressionException {
65         type = expressions[0].compileExpression().getType();
66         switch(type.getTypeCode()) {
67         case PType.TYPECODE_FLOAT:
68         case PType.TYPECODE_DOUBLE:
69             if (round <= 0) {
70                 type = PTypeSpace.LONG;
71             }
72         }
73         result = new BasicVariableOperand(type);
74         return result;
75     }
76     public Operand evaluate(ParameterOperand[] values, Object JavaDoc o)
77             throws ExpressionException {
78         Operand sub = expressions[0].evaluate(values, o);
79         switch(sub.getType().getTypeCode()) {
80         case PType.TYPECODE_FLOAT:
81             result.setValue(Math.round(sub.getFloat()));
82             break;
83         case PType.TYPECODE_OBJFLOAT:
84             Float JavaDoc f = (Float JavaDoc) sub.getObject();
85             if (f == null) {
86                 result.setValue((Object JavaDoc) null);
87             } else {
88                 result.setValue((long) Math.round(f.floatValue()));
89             }
90             break;
91         case PType.TYPECODE_DOUBLE:
92             result.setValue(Math.round(sub.getDouble()));
93             break;
94         case PType.TYPECODE_OBJDOUBLE:
95             Double JavaDoc d = (Double JavaDoc) sub.getObject();
96             if (d == null) {
97                 result.setValue((Object JavaDoc) null);
98             } else {
99                 result.setValue((long) Math.round(d.doubleValue()));
100             }
101             break;
102         default:
103             return sub;
104         }
105         return result;
106     }
107     public String JavaDoc getOperatorString() {
108         return "round";
109     }
110 }
111
Popular Tags