KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > query > AbstractExp


1 /*
2  * Copyright (c) 1998-2002 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jmx.query;
30
31 import javax.management.*;
32
33 /**
34  * Abstract implementation of an expression.
35  */

36 abstract public class AbstractExp extends QueryEval implements QueryExp {
37   /**
38    * Returns true for equality.
39    */

40   protected boolean eq(ValueExp v1, ValueExp v2)
41     throws BadBinaryOpValueExpException
42   {
43     if (v1 instanceof StringValueExp &&
44     v2 instanceof StringValueExp)
45       return toString(v1).equals(toString(v2));
46     else if (v1 instanceof BooleanValueExp &&
47          v2 instanceof BooleanValueExp)
48       return toBoolean(v1) == toBoolean(v2);
49     else if (v1 instanceof NumericValueExp &&
50          v2 instanceof NumericValueExp)
51       return toDouble(v1) == toDouble(v2);
52     else
53       throw new BadBinaryOpValueExpException(v1);
54   }
55   /**
56    * Returns true for less-than
57    */

58   protected boolean lt(ValueExp v1, ValueExp v2)
59     throws BadBinaryOpValueExpException
60   {
61     if (v1 instanceof StringValueExp &&
62     v2 instanceof StringValueExp)
63       return toString(v1).compareTo(toString(v2)) < 0;
64     else if (v1 instanceof BooleanValueExp &&
65          v2 instanceof BooleanValueExp) {
66       boolean b1 = toBoolean(v1);
67       boolean b2 = toBoolean(v2);
68       
69       return ! b1 && b2;
70     }
71     else if (v1 instanceof NumericValueExp &&
72          v2 instanceof NumericValueExp)
73       return toDouble(v1) < toDouble(v2);
74     else
75       throw new BadBinaryOpValueExpException(v1);
76   }
77   
78   /**
79    * Converts to a string value.
80    */

81   public static String toString(ValueExp exp)
82     throws BadBinaryOpValueExpException
83   {
84     if (exp instanceof StringValueExp)
85       return ((StringValueExp) exp).getValue();
86     else if (exp instanceof AbstractValueExp)
87       return ((AbstractValueExp) exp).getString();
88     else
89       throw new BadBinaryOpValueExpException(exp);
90   }
91   
92   /**
93    * Converts to a long value.
94    */

95   public static long toLong(ValueExp exp)
96     throws BadBinaryOpValueExpException
97   {
98     if (exp instanceof StringValueExp)
99       return Long.parseLong(((StringValueExp) exp).getValue());
100     else if (exp instanceof AbstractValueExp)
101       return ((AbstractValueExp) exp).getLong();
102     else
103       throw new BadBinaryOpValueExpException(exp);
104   }
105   /**
106    * Converts to a long value.
107    */

108   public static boolean toBoolean(ValueExp exp)
109     throws BadBinaryOpValueExpException
110   {
111     if (exp instanceof StringValueExp) {
112       String value = ((StringValueExp) exp).getValue();
113       
114       return value != null && ! value.equals("");
115     }
116     else if (exp instanceof AbstractValueExp)
117       return ((AbstractValueExp) exp).getBoolean();
118     else
119       throw new BadBinaryOpValueExpException(exp);
120   }
121   
122   /**
123    * Converts to a double value.
124    */

125   public static double toDouble(ValueExp exp)
126     throws BadBinaryOpValueExpException
127   {
128     if (exp instanceof StringValueExp)
129       return Double.parseDouble(((StringValueExp) exp).getValue());
130     else if (exp instanceof AbstractValueExp)
131       return ((AbstractValueExp) exp).getDouble();
132     else
133       throw new BadBinaryOpValueExpException(exp);
134   }
135 }
136
Popular Tags