KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > impl > ValueImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.impl;
18
19 import org.apache.ws.jaxme.sqls.Value;
20
21 /** <p>Implementation of a value.</p>
22  *
23  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
24  */

25 public class ValueImpl implements Value {
26   private final Value.Type type;
27   private final Object JavaDoc value;
28
29   public ValueImpl(Value pValue) {
30     this(pValue.getType(), pValue.getValue());
31   }
32   public ValueImpl(Value.Type pType, Object JavaDoc pValue) {
33     if (pType == null) {
34       throw new NullPointerException JavaDoc("The value type must not be null.");
35     }
36     if ((pType.equals(Type.PLACEHOLDER) || pType.equals(Type.NULL)) &&
37     pValue != null) {
38       throw new IllegalArgumentException JavaDoc("The value argument must be null for the type " +
39                                           pType + ".");
40     }
41     type = pType;
42     value = pValue;
43   }
44
45   public Value.Type getType() { return type; }
46   public Object JavaDoc getValue() { return value; }
47   public int hashCode() {
48     int result = type.hashCode();
49     if (value != null) {
50       result = value.hashCode();
51     }
52     return result;
53   }
54   public boolean equals(Object JavaDoc o) {
55     if (o == null || !(o instanceof Value)) {
56       return false;
57     }
58     Value v = (Value) o;
59     if (!type.equals(v.getType())) { return false; }
60     if (value == null) {
61       return v.getValue() == null;
62     } else {
63       return value.equals(v.getValue());
64     }
65   }
66
67     public static Object JavaDoc asValue(Object JavaDoc pValue) {
68         if (pValue == null) {
69             return new ValueImpl(Value.Type.NULL, pValue);
70         } else if (pValue instanceof String JavaDoc) {
71             return new ValueImpl(Value.Type.STRING, pValue);
72         } else if (pValue instanceof Boolean JavaDoc) {
73             return new ValueImpl(Value.Type.BOOLEAN, pValue);
74         } else if (pValue instanceof Byte JavaDoc) {
75             return new ValueImpl(Value.Type.BYTE, pValue);
76         } else if (pValue instanceof Short JavaDoc) {
77             return new ValueImpl(Value.Type.SHORT, pValue);
78         } else if (pValue instanceof Integer JavaDoc) {
79             return new ValueImpl(Value.Type.INT, pValue);
80         } else if (pValue instanceof Long JavaDoc) {
81             return new ValueImpl(Value.Type.LONG, pValue);
82         } else if (pValue instanceof Float JavaDoc) {
83             return new ValueImpl(Value.Type.FLOAT, pValue);
84         } else if (pValue instanceof Double JavaDoc) {
85             return new ValueImpl(Value.Type.DOUBLE, pValue);
86         } else {
87             return pValue;
88         }
89     }
90 }
91
Popular Tags