KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > numericvalueexpression > roundfunction


1 package com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression;
2
3 import java.math.*;
4
5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.common.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
9 import com.daffodilwoods.daffodildb.server.sql99.token.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
11 import com.daffodilwoods.daffodildb.utils.*;
12 import com.daffodilwoods.daffodildb.utils.field.*;
13 import com.daffodilwoods.database.resource.*;
14
15 public class roundfunction extends AbstractNumericValueFunction {
16
17    public parencommanumericvalueexpression _parencommanumericvalueexpression0;
18    public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442551;
19
20    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
21
22         FieldBase[] res = GeneralPurposeStaticClass.changeIntoFieldBase( (
23             Object JavaDoc[]) _parencommanumericvalueexpression0.run(object));
24         FieldBase result1 = res[0];
25         FieldBase result2 = res[1];
26         if (result1.isNull() || result2.isNull()) {
27           return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, Datatype.BIGDECIMAL);
28         }
29         try {
30           return getResult(getDataType(result1), result1.getObject(),
31                          getCount(result2, getDataType(result2)));
32         }
33        catch (DException ex) {
34        if(ex.getDseCode().equalsIgnoreCase("DSE5545")) {
35          throw new DException("DSE419", new Object JavaDoc[] {result2.getObject()});
36        }
37         throw ex;
38       }
39    }
40
41    public ParameterInfo[] getParameterInfo() throws DException {
42      ParameterInfo[] paramInfo = super.getParameterInfo();
43     for (int i = 0; i < paramInfo.length; i++) {
44       if (paramInfo[i].getQuestionMark()) {
45         paramInfo[i].setDataType(Datatypes.BIGDECIMAL);
46         paramInfo[i].setName("ROUND Arg");
47       }
48     }
49      return paramInfo;
50    }
51
52
53    private Object JavaDoc getResult(int type, Object JavaDoc object, Integer JavaDoc result2) throws DException {
54       int places = result2.intValue();
55       BigDecimal bigDecimal = null;
56       try {
57          switch (type) {
58             case BYTE:
59             case TINYINT:
60             case INTEGER:
61             case INT:
62             case REAL:
63             case DOUBLE:
64             case FLOAT:
65             case DOUBLEPRECISION:
66             case LONG:
67             case BIGINT:
68             case SHORT:
69             case SMALLINT:
70                             return places <0 ? getResult((Number JavaDoc)object,places,false ):new FieldLiteral(new BigDecimal( ( (Number JavaDoc) object).doubleValue()).setScale(places,BigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
71             case BIGDECIMAL:
72             case DEC:
73             case DECIMAL:
74             case NUMERIC:
75
76                /* Here we do not use the number type because in case of bigdecimal
77                   need of scale to compare with palces . Places have value to round the number*/

78                BigDecimal bigDecimal1 = (BigDecimal) object;
79                bigDecimal = new BigDecimal(bigDecimal1.doubleValue());
80                int sc = bigDecimal1.scale();
81                int u = places < sc ? places : sc;
82                return u < 0? getResult(bigDecimal,places,true) : new FieldLiteral( bigDecimal.setScale(places, bigDecimal.ROUND_HALF_EVEN), Datatype.BIGDECIMAL);
83
84             default:
85                throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type), "round"});
86          }
87       } catch (Exception JavaDoc eee) {
88          throw new DException("DSE419", new Object JavaDoc[] {"ROUND"});
89       }
90    }
91
92    /* private Object getResult(int type,Object object,Integer result2) throws DException {
93         int places = result2.intValue();
94         BigDecimal bigDecimal = null;
95         try{
96            switch(type){
97              case BYTE : case TINYINT :
98                  bigDecimal = new BigDecimal(((Byte)object).doubleValue());
99                  return new FieldLiteral(bigDecimal.setScale(places,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
100                case INTEGER : case INT :
101                  Integer integer = (Integer)object;
102                  bigDecimal = new BigDecimal(integer.doubleValue());
103                  return new FieldLiteral(bigDecimal.setScale(places,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
104               case REAL :
105                  Float floatV = (Float)object;
106                  bigDecimal = new BigDecimal(floatV.doubleValue());
107                  return new FieldLiteral(bigDecimal.setScale(places,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
108                case DOUBLE : case FLOAT : case DOUBLEPRECISION :
109                  Double doubleV = (Double)object;
110                  bigDecimal = new BigDecimal(doubleV.doubleValue());
111                  return new FieldLiteral(bigDecimal.setScale(places,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
112                case LONG : case BIGINT :
113                  Long longV = (Long)object;
114                  bigDecimal = new BigDecimal(longV.doubleValue());
115                  return new FieldLiteral(bigDecimal.setScale(places,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
116                case SHORT : case SMALLINT :
117                  Short shortV = (Short)object;
118                  bigDecimal = new BigDecimal(shortV.doubleValue());
119                  return new FieldLiteral(bigDecimal.setScale(places,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
120                case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
121                  BigDecimal bigDecimal1 = (BigDecimal)object;
122                  bigDecimal = new BigDecimal(bigDecimal1.doubleValue());
123                  int sc = bigDecimal1.scale();
124                  int u = places < sc ? places : sc ;
125                  return new FieldLiteral(bigDecimal.setScale(u,bigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL);
126               default :
127                 throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type),"round"});
128            }
129         } catch( Exception eee ){
130            throw new DException("DSE419",new Object[]{result2});
131         }
132      } */

133
134    private Integer JavaDoc getCount(FieldBase field, int type) throws DException {
135       Object JavaDoc object=field.getObject();
136       switch (type) {
137          case BYTE:
138          case TINYINT:
139          case INTEGER:
140          case INT:
141          case SHORT:
142          case SMALLINT:
143             return new Integer JavaDoc(((Number JavaDoc)object).intValue());
144          case REAL:
145            TypeValidityHandler.checkIntegerWithFloat(field);
146            return new Integer JavaDoc(((Number JavaDoc)object).intValue());
147          case DOUBLE:
148          case FLOAT:
149          case DOUBLEPRECISION:
150           TypeValidityHandler.checkIntegerWithDouble(field);
151           return new Integer JavaDoc(((Number JavaDoc)object).intValue());
152          case LONG:
153          case BIGINT:
154          TypeValidityHandler.checkIntegerWithNonDecimal(field);
155          return new Integer JavaDoc(((Number JavaDoc)object).intValue());
156          case BIGDECIMAL:
157          case DEC:
158          case DECIMAL:
159          case NUMERIC:
160             TypeValidityHandler.checkIntegerWithBigDecimal(field);
161             return new Integer JavaDoc(((Number JavaDoc)object).intValue());
162          case CHARACTER:
163          case VARCHAR:
164          case CHAR:
165          case CHARACTERVARYING:
166             String JavaDoc string = (String JavaDoc)object ;
167             Double JavaDoc d = null;
168             try {
169                d = new Double JavaDoc(string);
170             } catch (Exception JavaDoc e) {
171                throw new DException("DSE4104", new Object JavaDoc[] {"character string", "double"});
172             }
173             return new Integer JavaDoc(d.intValue());
174          default:
175             throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type), "round"});
176       }
177    }
178
179
180
181
182    /* private Integer getCount(Object object, int type) throws DException {
183        switch(type){
184          case BYTE : case TINYINT :
185              return new Integer(((Byte)object).intValue());
186            case INTEGER : case INT :
187              Integer integer = (Integer)object;
188              return new Integer(integer.intValue());
189           case REAL :
190              Float floatV = (Float)object;
191              return new Integer(floatV.intValue());
192            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
193              Double doubleV = (Double)object;
194              return new Integer(doubleV.intValue());
195            case LONG : case BIGINT :
196              Long longV = (Long)object;
197              return new Integer(longV.intValue());
198            case SHORT : case SMALLINT :
199              Short shortV = (Short)object;
200              return new Integer(shortV.intValue());
201            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
202              BigDecimal bigDecimal = (BigDecimal)object;
203              return new Integer(bigDecimal.intValue());
204            case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
205              String string = (String)object;
206              Double d = null;
207              try{
208                 d = new Double(string);
209              }catch(Exception e){
210                 throw new DException("DSE4104",new Object[]{"character string","double"});
211              }
212              return new Integer(d.intValue());
213           default :
214               throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type),"round"});
215        }
216     } */

217
218    public AbstractRowValueExpression[] getChilds() {
219       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {_parencommanumericvalueexpression0};
220       return childs;
221    }
222
223    public String JavaDoc getType() throws DException {
224       return (String JavaDoc) _SNONRESERVEDWORD1364442551.run(null);
225    }
226
227    public String JavaDoc toString() {
228       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
229       sb.append(" ");
230       sb.append(_SNONRESERVEDWORD1364442551);
231       sb.append(_parencommanumericvalueexpression0);
232       return sb.toString();
233    }
234
235    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
236       roundfunction tempClass = new roundfunction();
237       tempClass._parencommanumericvalueexpression0 = (parencommanumericvalueexpression) _parencommanumericvalueexpression0.clone();
238       tempClass._SNONRESERVEDWORD1364442551 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442551.clone();
239       return tempClass;
240    }
241
242    public ByteComparison getByteComparison(Object JavaDoc object) throws DException {
243    ByteComparison byteComparison = new ByteComparison(false, new int[] {BIGDECIMAL/*getDatatype(object)*/});
244    byteComparison.setSize(getColumnSize(object));
245    return byteComparison;
246    }
247
248
249    /**
250    * getDatatype not called in getByteComparison method for set data type.
251    * Because Data type of this function has been fixed which is BIGDECIMAL.
252    * This has been done to resolve the bug 12430 --- sube singh
253    * @param object Object
254    * @throws DException
255    * @return int
256    */

257
258    private int getDatatype(Object JavaDoc object) throws DException {
259         int type = getDataTypeForByte(_parencommanumericvalueexpression0.getByteComparison(object));
260         switch (type) {
261            case TINYINT:
262            case BYTE:
263            case SHORT:
264            case SMALLINT:
265            case INT:
266            case INTEGER:
267               return INTEGER;
268            case BIGINT:
269            case LONG:
270               return LONG;
271            case REAL:
272               return FLOAT;
273            case DOUBLE:
274            case FLOAT:
275            case DOUBLEPRECISION:
276               return DOUBLE;
277            /*In case of question mark I get data type -1
278              So to resolve the bug 11823 data type BigDecimal
279             is set. ---- Sube Singh
280             */

281            case -1 :
282            case BIGDECIMAL:
283            case DEC:
284            case DECIMAL:
285            case NUMERIC:
286               return BIGDECIMAL;
287            default:
288               throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type), "ROUND"});
289         }
290      }
291
292
293    public _Reference[] checkSemantic(_ServerSession parent) throws DException {
294       _Reference[] ref = super.checkSemantic(parent);
295       if(ref!=null) {
296         return ref;
297        }
298       int type[] = _parencommanumericvalueexpression0.getByteComparison(parent).getDataTypes();
299
300       if (type[0] == -1 || type[0] <= 15) {
301          if (type[1] == -1 || type[1] <= 15) {
302             return ref;
303          }
304          throw new DException("DSE4108",
305                               new Object JavaDoc[] {StaticClass.getDataTypeName(type[1]),
306                               "ROUND"});
307       }
308       throw new DException("DSE4108",
309                            new Object JavaDoc[] {StaticClass.getDataTypeName(type[0]),
310                            "ROUND"});
311
312    }
313
314  public int getColumnSize(Object JavaDoc object) throws DException {
315      if(columnDetails[0].getType()!=TypeConstants.CONSTANT) {
316        int datatype = columnDetails[0].getDatatype();
317          switch (datatype) {
318              case BYTE:
319              case TINYINT:
320              case SHORT:
321              case SMALLINT:
322               return SHORTSIZE;
323              case INTEGER:
324              case INT:
325                return INTSIZE;
326              case REAL:
327                return FLOATSIZE;
328              case DOUBLE:
329              case FLOAT:
330              case DOUBLEPRECISION:
331               return DOUBLESIZE;
332              case LONG:
333              case BIGINT:
334                return LONGSIZE;
335                default :
336                if (columnDetails[0].getSize() != -5) {
337                  return columnDetails[0].getSize();
338                }
339
340       }
341       } else if(columnDetails[0].getQuestion()){
342          return Datatypes.DOUBLESIZE;
343       } else {
344         FieldBase[] field = GeneralPurposeStaticClass.changeIntoFieldBase( (
345             Object JavaDoc[]) _parencommanumericvalueexpression0.run(object));
346         field[0].setDatatype(columnDetails[0].getDatatype());
347         return field[0].getLength();
348       }
349     return -1;
350    }
351
352    /** This nmethod is written for giving the result for negative scale :
353        * ROUND(23434.3434 ,-1) it give :23430
354        * ROUND( -5 ,-1) it give : -10
355        *
356        * @param Number to whiche it will round
357        * @param scale take the scale
358        * @param flag --true if number is instanceOf BigDecimal,otherwise false.
359        * @since After release 4.1
360        * @author Sandeep Kadiyan
361        * */

362
363       private Object JavaDoc getResult(Number JavaDoc number, int scale,boolean flag) {
364       Object JavaDoc obj=null;
365       if (flag) {
366         BigDecimal val = ( (BigDecimal) number);
367         BigDecimal D = new BigDecimal( (long) Math.pow(10, Math.abs(scale)) + "");
368         obj = val.signum() == -1 ?
369             val.negate().divide(D, BigDecimal.ROUND_HALF_UP).setScale(0,
370             BigDecimal.ROUND_HALF_EVEN).multiply(D).negate()
371             :
372             val.divide(D, BigDecimal.ROUND_HALF_UP).setScale(0, BigDecimal.ROUND_HALF_EVEN).
373             multiply(D);
374
375       }
376       else {
377         double val = number.doubleValue();
378         long D = (long) Math.pow(10, Math.abs(scale));
379         obj = val < 0 ? new BigDecimal(Math.round(Math.abs(val) / D) * -D) :
380             new BigDecimal(Math.round(val / D) * D);
381       }
382
383
384         return new FieldLiteral(obj, Datatype.BIGDECIMAL);
385      }
386
387 }
388
Popular Tags