Revision 1956 trunk/libraries/libGDBMS/src/com/hardcode/gdbms/engine/values/IntValue.java

View differences:

IntValue.java
1 1
package com.hardcode.gdbms.engine.values;
2 2

  
3
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
4

  
5

  
6 3
/**
7 4
 * Wrapper sobre el tipo int
8 5
 *
9 6
 * @author Fernando Gonz?lez Cort?s
10 7
 */
11 8
public class IntValue extends NumericValue {
12
    private int value;
9
	private int value;
13 10

  
14
    /**
15
     * Creates a new IntValue object.
16
     *
17
     * @param value DOCUMENT ME!
18
     */
19
    public IntValue(int value) {
20
        this.value = value;
21
    }
11
	/**
12
	 * Creates a new IntValue object.
13
	 *
14
	 * @param value DOCUMENT ME!
15
	 */
16
	IntValue(int value) {
17
		this.value = value;
18
	}
22 19

  
23
    /**
24
     * Creates a new IntValue object.
25
     */
26
    public IntValue() {
27
    }
20
	/**
21
	 * Creates a new IntValue object.
22
	 */
23
	IntValue() {
24
	}
28 25

  
29
    /**
30
     * Establece el valor de este objeto
31
     *
32
     * @param value
33
     */
34
    public void setValue(int value) {
35
        this.value = value;
36
    }
26
	/**
27
	 * Establece el valor de este objeto
28
	 *
29
	 * @param value
30
	 */
31
	public void setValue(int value) {
32
		this.value = value;
33
	}
37 34

  
38
    /**
39
     * Obtiene el valor de este objeto
40
     *
41
     * @return
42
     */
43
    public int getValue() {
44
        return value;
45
    }
35
	/**
36
	 * Obtiene el valor de este objeto
37
	 *
38
	 * @return
39
	 */
40
	public int getValue() {
41
		return value;
42
	}
46 43

  
47
    /**
48
     * @see java.lang.Object#toString()
49
     */
50
    public String toString() {
51
        return "" + value;
52
    }
44
	/**
45
	 * @see java.lang.Object#toString()
46
	 */
47
	public String toString() {
48
		return "" + value;
49
	}
53 50

  
54
    /**
55
     * @see com.hardcode.gdbms.engine.instruction.expression.Operations#suma(com.hardcode.gdbms.engine.instruction.expression.Value)
56
     */
57
    public Value suma(Value v) throws IncompatibleTypesException {
58
        if (v instanceof IntValue) {
59
            IntValue ret = new IntValue();
60
            ret.setValue(this.value + ((IntValue) v).getValue());
61

  
62
            return ret;
63
        } else if (v instanceof LongValue) {
64
            LongValue ret = new LongValue();
65
            ret.setValue(this.value + ((LongValue) v).getValue());
66

  
67
            return ret;
68
        } else if (v instanceof FloatValue) {
69
            FloatValue ret = new FloatValue();
70
            ret.setValue(this.value + ((FloatValue) v).getValue());
71

  
72
            return ret;
73
        } else if (v instanceof DoubleValue) {
74
            DoubleValue ret = new DoubleValue();
75
            ret.setValue(this.value + ((DoubleValue) v).getValue());
76

  
77
            return ret;
78
        } else if (v instanceof StringValue) {
79
            try {
80
                DoubleValue ret = new DoubleValue();
81
                ret.setValue(this.value +
82
                    Double.parseDouble(((StringValue) v).getValue()));
83

  
84
                return ret;
85
            } catch (NumberFormatException e) {
86
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
87
                    " is not a number");
88
            }
89
        } else {
90
            throw new IncompatibleTypesException();
91
        }
92
    }
93

  
94
    /**
95
     * @see com.hardcode.gdbms.engine.instruction.expression.Operations#producto(com.hardcode.gdbms.engine.instruction.expression.Value)
96
     */
97
    public Value producto(Value v) throws IncompatibleTypesException {
98
        if (v instanceof IntValue) {
99
            IntValue ret = new IntValue();
100
            ret.setValue(this.value * ((IntValue) v).getValue());
101

  
102
            return ret;
103
        } else if (v instanceof LongValue) {
104
            LongValue ret = new LongValue();
105
            ret.setValue(this.value * ((LongValue) v).getValue());
106

  
107
            return ret;
108
        } else if (v instanceof FloatValue) {
109
            FloatValue ret = new FloatValue();
110
            ret.setValue(this.value * ((FloatValue) v).getValue());
111

  
112
            return ret;
113
        } else if (v instanceof DoubleValue) {
114
            DoubleValue ret = new DoubleValue();
115
            ret.setValue(this.value * ((DoubleValue) v).getValue());
116

  
117
            return ret;
118
        } else if (v instanceof StringValue) {
119
            try {
120
                DoubleValue ret = new DoubleValue();
121
                ret.setValue(this.value *
122
                    Double.parseDouble(((StringValue) v).getValue()));
123

  
124
                return ret;
125
            } catch (NumberFormatException e) {
126
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
127
                    " is not a number");
128
            }
129
        } else {
130
            throw new IncompatibleTypesException();
131
        }
132
    }
133

  
134
    /**
135
     * @see com.hardcode.gdbms.engine.instruction.Operations#equals(com.hardcode.gdbms.engine.values.Value)
136
     */
137
    public Value equals(Value v) throws IncompatibleTypesException {
138
        if (v instanceof IntValue) {
139
            return new BooleanValue(this.value == ((IntValue) v).getValue());
140
        } else if (v instanceof LongValue) {
141
            return new BooleanValue(this.value == ((LongValue) v).getValue());
142
        } else if (v instanceof FloatValue) {
143
            return new BooleanValue(this.value == ((FloatValue) v).getValue());
144
        } else if (v instanceof DoubleValue) {
145
            return new BooleanValue(this.value == ((DoubleValue) v).getValue());
146
        } else if (v instanceof StringValue) {
147
            try {
148
                return new BooleanValue(this.value == Double.parseDouble(
149
                        ((StringValue) v).getValue()));
150
            } catch (NumberFormatException e) {
151
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
152
                    " is not a number");
153
            }
154
        } else {
155
            throw new IncompatibleTypesException();
156
        }
157
    }
158

  
159
    /**
160
     * @see com.hardcode.gdbms.engine.instruction.Operations#greater(com.hardcode.gdbms.engine.values.DoubleValue)
161
     */
162
    public Value greater(Value v) throws IncompatibleTypesException {
163
        if (v instanceof IntValue) {
164
            return new BooleanValue(this.value > ((IntValue) v).getValue());
165
        } else if (v instanceof LongValue) {
166
            return new BooleanValue(this.value > ((LongValue) v).getValue());
167
        } else if (v instanceof FloatValue) {
168
            return new BooleanValue(this.value > ((FloatValue) v).getValue());
169
        } else if (v instanceof DoubleValue) {
170
            return new BooleanValue(this.value > ((DoubleValue) v).getValue());
171
        } else if (v instanceof StringValue) {
172
            try {
173
                return new BooleanValue(this.value > Double.parseDouble(
174
                        ((StringValue) v).getValue()));
175
            } catch (NumberFormatException e) {
176
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
177
                    " is not a number");
178
            }
179
        } else {
180
            throw new IncompatibleTypesException();
181
        }
182
    }
183

  
184
    /**
185
     * @see com.hardcode.gdbms.engine.instruction.Operations#greaterEqual(com.hardcode.gdbms.engine.values.DoubleValue)
186
     */
187
    public Value greaterEqual(Value v)
188
        throws IncompatibleTypesException {
189
        if (v instanceof IntValue) {
190
            return new BooleanValue(this.value >= ((IntValue) v).getValue());
191
        } else if (v instanceof LongValue) {
192
            return new BooleanValue(this.value >= ((LongValue) v).getValue());
193
        } else if (v instanceof FloatValue) {
194
            return new BooleanValue(this.value >= ((FloatValue) v).getValue());
195
        } else if (v instanceof DoubleValue) {
196
            return new BooleanValue(this.value >= ((DoubleValue) v).getValue());
197
        } else if (v instanceof StringValue) {
198
            try {
199
                return new BooleanValue(this.value >= Double.parseDouble(
200
                        ((StringValue) v).getValue()));
201
            } catch (NumberFormatException e) {
202
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
203
                    " is not a number");
204
            }
205
        } else {
206
            throw new IncompatibleTypesException();
207
        }
208
    }
209

  
210
    /**
211
     * @see com.hardcode.gdbms.engine.instruction.Operations#less(com.hardcode.gdbms.engine.values.Value)
212
     */
213
    public Value less(Value v) throws IncompatibleTypesException {
214
        if (v instanceof IntValue) {
215
            return new BooleanValue(this.value < ((IntValue) v).getValue());
216
        } else if (v instanceof LongValue) {
217
            return new BooleanValue(this.value < ((LongValue) v).getValue());
218
        } else if (v instanceof FloatValue) {
219
            return new BooleanValue(this.value < ((FloatValue) v).getValue());
220
        } else if (v instanceof DoubleValue) {
221
            return new BooleanValue(this.value < ((DoubleValue) v).getValue());
222
        } else if (v instanceof StringValue) {
223
            try {
224
                return new BooleanValue(this.value < Double.parseDouble(
225
                        ((StringValue) v).getValue()));
226
            } catch (NumberFormatException e) {
227
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
228
                    " is not a number");
229
            }
230
        } else {
231
            throw new IncompatibleTypesException();
232
        }
233
    }
234

  
235
    /**
236
     * @see com.hardcode.gdbms.engine.instruction.Operations#lessEqual(com.hardcode.gdbms.engine.values.Value)
237
     */
238
    public Value lessEqual(Value v) throws IncompatibleTypesException {
239
        if (v instanceof IntValue) {
240
            return new BooleanValue(this.value <= ((IntValue) v).getValue());
241
        } else if (v instanceof LongValue) {
242
            return new BooleanValue(this.value <= ((LongValue) v).getValue());
243
        } else if (v instanceof FloatValue) {
244
            return new BooleanValue(this.value <= ((FloatValue) v).getValue());
245
        } else if (v instanceof DoubleValue) {
246
            return new BooleanValue(this.value <= ((DoubleValue) v).getValue());
247
        } else if (v instanceof StringValue) {
248
            try {
249
                return new BooleanValue(this.value <= Double.parseDouble(
250
                        ((StringValue) v).getValue()));
251
            } catch (NumberFormatException e) {
252
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
253
                    " is not a number");
254
            }
255
        } else {
256
            throw new IncompatibleTypesException();
257
        }
258
    }
259

  
260
    /**
261
     * @see com.hardcode.gdbms.engine.instruction.Operations#notEquals(com.hardcode.gdbms.engine.values.Value)
262
     */
263
    public Value notEquals(Value v) throws IncompatibleTypesException {
264
        if (v instanceof IntValue) {
265
            return new BooleanValue(this.value != ((IntValue) v).getValue());
266
        } else if (v instanceof LongValue) {
267
            return new BooleanValue(this.value != ((LongValue) v).getValue());
268
        } else if (v instanceof FloatValue) {
269
            return new BooleanValue(this.value != ((FloatValue) v).getValue());
270
        } else if (v instanceof DoubleValue) {
271
            return new BooleanValue(this.value != ((DoubleValue) v).getValue());
272
        } else if (v instanceof StringValue) {
273
            try {
274
                return new BooleanValue(this.value != Double.parseDouble(
275
                        ((StringValue) v).getValue()));
276
            } catch (NumberFormatException e) {
277
                throw new IncompatibleTypesException(((StringValue) v).getValue() +
278
                    " is not a number");
279
            }
280
        } else {
281
            throw new IncompatibleTypesException();
282
        }
283
    }
284

  
285
    /**
51
	/**
286 52
	 * @see com.hardcode.gdbms.engine.values.NumericValue#intValue()
287 53
	 */
288 54
	public int intValue() {
......
309 75
	public double doubleValue() {
310 76
		return (double) value;
311 77
	}
78

  
79
	/**
80
	 * @see com.hardcode.gdbms.engine.values.NumericValue#getType()
81
	 */
82
	public int getType() {
83
		return ValueFactory.INTEGER;
84
	}
85

  
86
	/**
87
	 * @see com.hardcode.gdbms.engine.values.NumericValue#byteValue()
88
	 */
89
	public byte byteValue() {
90
		return (byte) value;
91
	}
92

  
93
	/**
94
	 * @see com.hardcode.gdbms.engine.values.NumericValue#shortValue()
95
	 */
96
	public short shortValue() {
97
		return (short) value;
98
	}
312 99
}

Also available in: Unified diff