KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > constraint > LiteralNode


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.constraint;
15
16 import java.io.*;
17
18
19 /** Represents a literal value */
20 public class LiteralNode extends ExprNode
21 {
22   private Value m_value;
23
24
25   public static final int BOOLEAN = 0;
26   public static final int NUMBER = 1;
27   public static final int STRING = 2;
28
29
30   private LiteralNode()
31   {
32   }
33
34
35   public LiteralNode(int literalType, String JavaDoc value)
36   {
37     determineType(literalType, value);
38
39     switch (getType().getId()) {
40       case ValueType.BOOLEAN:
41         m_value = ValueFactory.createBoolean(value.equals("TRUE"));
42         break;
43
44       case ValueType.DOUBLE: {
45           try {
46             Double JavaDoc d = Double.valueOf(value);
47             m_value = ValueFactory.createDouble(d.doubleValue());
48           }
49           catch (NumberFormatException JavaDoc e) {
50           }
51         }
52         break;
53
54       case ValueType.LONG: {
55           try {
56             Integer JavaDoc i = Integer.valueOf(value);
57             m_value = ValueFactory.createLong(i.intValue());
58           }
59           catch (NumberFormatException JavaDoc e) {
60           }
61         }
62         break;
63
64       case ValueType.STRING:
65         m_value = ValueFactory.createString(value);
66         break;
67     }
68   }
69
70
71   public void print(PrintStream ps)
72   {
73     ps.println("LiteralNode: type = " + getType() + " value = " + m_value);
74   }
75
76
77   public Value evaluate(PropertySource source)
78     throws MissingPropertyException
79   {
80     return m_value;
81   }
82
83
84   protected void determineType(int literalType, String JavaDoc value)
85   {
86     if (literalType == BOOLEAN)
87       setType(new ValueType(ValueType.BOOLEAN));
88     else if (literalType == STRING)
89       setType(new ValueType(ValueType.STRING));
90     else { // NUMBER
91
if (value.indexOf('.') >= 0 || value.indexOf('E') >= 0 ||
92           value.indexOf('e') >= 0)
93         setType(new ValueType(ValueType.DOUBLE));
94       else
95         setType(new ValueType(ValueType.LONG));
96     }
97   }
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
Popular Tags