KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > beans > BaseProbe


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.common.beans;
17
18 import java.util.List JavaDoc;
19
20 /**
21  * Abstract class used to help development of Probe implementations
22  */

23 public abstract class BaseProbe implements Probe {
24
25   protected abstract void setProperty(Object JavaDoc object, String JavaDoc property, Object JavaDoc value);
26
27   protected abstract Object JavaDoc getProperty(Object JavaDoc object, String JavaDoc property);
28
29   /**
30    * Returns an array of the readable properties exposed by an object
31    *
32    * @param object - the object
33    * @return The array of property names
34    */

35   public abstract String JavaDoc[] getReadablePropertyNames(Object JavaDoc object);
36
37   /**
38    * Returns an array of the writeable properties exposed by an object
39    *
40    * @param object - the object
41    * @return The array of property names
42    */

43   public abstract String JavaDoc[] getWriteablePropertyNames(Object JavaDoc object);
44
45   protected Object JavaDoc getIndexedProperty(Object JavaDoc object, String JavaDoc indexedName) {
46
47     Object JavaDoc value = null;
48
49     try {
50       String JavaDoc name = indexedName.substring(0, indexedName.indexOf("["));
51       int i = Integer.parseInt(indexedName.substring(indexedName.indexOf("[") + 1, indexedName.indexOf("]")));
52       Object JavaDoc list = getProperty(object, name);
53       if (list instanceof List JavaDoc) {
54         value = ((List JavaDoc) list).get(i);
55       } else if (list instanceof Object JavaDoc[]) {
56         value = ((Object JavaDoc[]) list)[i];
57       } else if (list instanceof char[]) {
58         value = new Character JavaDoc(((char[]) list)[i]);
59       } else if (list instanceof boolean[]) {
60         value = new Boolean JavaDoc(((boolean[]) list)[i]);
61       } else if (list instanceof byte[]) {
62         value = new Byte JavaDoc(((byte[]) list)[i]);
63       } else if (list instanceof double[]) {
64         value = new Double JavaDoc(((double[]) list)[i]);
65       } else if (list instanceof float[]) {
66         value = new Float JavaDoc(((float[]) list)[i]);
67       } else if (list instanceof int[]) {
68         value = new Integer JavaDoc(((int[]) list)[i]);
69       } else if (list instanceof long[]) {
70         value = new Long JavaDoc(((long[]) list)[i]);
71       } else if (list instanceof short[]) {
72         value = new Short JavaDoc(((short[]) list)[i]);
73       } else {
74         throw new ProbeException("The '" + name + "' property of the " + object.getClass().getName() + " class is not a List or Array.");
75       }
76
77     } catch (ProbeException e) {
78       throw e;
79     } catch (Exception JavaDoc e) {
80       throw new ProbeException("Error getting ordinal list from JavaBean. Cause " + e, e);
81     }
82
83     return value;
84   }
85
86   protected void setIndexedProperty(Object JavaDoc object, String JavaDoc indexedName, Object JavaDoc value) {
87
88     try {
89       String JavaDoc name = indexedName.substring(0, indexedName.indexOf("["));
90       int i = Integer.parseInt(indexedName.substring(indexedName.indexOf("[") + 1, indexedName.indexOf("]")));
91       Object JavaDoc list = getProperty(object, name);
92       if (list instanceof List JavaDoc) {
93         ((List JavaDoc) list).set(i, value);
94       } else if (list instanceof Object JavaDoc[]) {
95         ((Object JavaDoc[]) list)[i] = value;
96       } else if (list instanceof char[]) {
97         ((char[]) list)[i] = ((Character JavaDoc) value).charValue();
98       } else if (list instanceof boolean[]) {
99         ((boolean[]) list)[i] = ((Boolean JavaDoc) value).booleanValue();
100       } else if (list instanceof byte[]) {
101         ((byte[]) list)[i] = ((Byte JavaDoc) value).byteValue();
102       } else if (list instanceof double[]) {
103         ((double[]) list)[i] = ((Double JavaDoc) value).doubleValue();
104       } else if (list instanceof float[]) {
105         ((float[]) list)[i] = ((Float JavaDoc) value).floatValue();
106       } else if (list instanceof int[]) {
107         ((int[]) list)[i] = ((Integer JavaDoc) value).intValue();
108       } else if (list instanceof long[]) {
109         ((long[]) list)[i] = ((Long JavaDoc) value).longValue();
110       } else if (list instanceof short[]) {
111         ((short[]) list)[i] = ((Short JavaDoc) value).shortValue();
112       } else {
113         throw new ProbeException("The '" + name + "' property of the " + object.getClass().getName() + " class is not a List or Array.");
114       }
115     } catch (ProbeException e) {
116       throw e;
117     } catch (Exception JavaDoc e) {
118       throw new ProbeException("Error getting ordinal value from JavaBean. Cause " + e, e);
119     }
120   }
121
122
123 }
124
Popular Tags