KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > naming > lib > MapPNameGetter


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.naming.lib;
19
20 import org.objectweb.jorm.naming.api.PNameGetter;
21 import org.objectweb.jorm.api.PException;
22
23 import java.util.Date JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.math.BigInteger JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28
29 /**
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class MapPNameGetter implements PNameGetter {
34
35     Map JavaDoc field2value;
36
37     public MapPNameGetter(String JavaDoc[] fieldNames, Object JavaDoc[] fieldValues) {
38         field2value = new HashMap JavaDoc();
39         for(int i=fieldNames.length-1; i>=0; i--) {
40             field2value.put(fieldNames[i], fieldValues[i]);
41         }
42     }
43     public MapPNameGetter(Map JavaDoc field2value) {
44         this.field2value = field2value;
45     }
46
47     public byte pngetByteField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
48         Object JavaDoc value = field2value.get(fn);
49         if (value == null) {
50             throw new PException("No field " + fn + " found among "
51                 + field2value.keySet());
52         }
53         if (value instanceof Byte JavaDoc) {
54             return ((Byte JavaDoc) value).byteValue();
55         }
56         throw new PException("Bad type: expected " + value.getClass().getName());
57     }
58
59     public Byte JavaDoc pngetObyteField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
60         Object JavaDoc value = field2value.get(fn);
61         if (value == null) {
62             if (field2value.containsKey(fn)) {
63                 return null;
64             } else {
65                 throw new PException("No field " + fn + " found among "
66                 + field2value.keySet());
67             }
68         }
69         if (value instanceof Byte JavaDoc) {
70             return (Byte JavaDoc) value;
71         }
72         throw new PException("Bad type: expected " + value.getClass().getName());
73     }
74
75     public char pngetCharField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
76         Object JavaDoc value = field2value.get(fn);
77         if (value == null) {
78             throw new PException("No field " + fn + " found among "
79                 + field2value.keySet());
80         }
81         if (value instanceof Character JavaDoc) {
82             return ((Character JavaDoc) value).charValue();
83         }
84         throw new PException("Bad type: expected " + value.getClass().getName());
85     }
86
87     public Character JavaDoc pngetOcharField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
88         Object JavaDoc value = field2value.get(fn);
89         if (value == null) {
90             if (field2value.containsKey(fn)) {
91                 return null;
92             } else {
93                 throw new PException("No field " + fn + " found among "
94                 + field2value.keySet());
95             }
96         }
97         if (value instanceof Character JavaDoc) {
98             return (Character JavaDoc) value;
99         }
100         throw new PException("Bad type: expected " + value.getClass().getName());
101     }
102
103     public short pngetShortField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
104         Object JavaDoc value = field2value.get(fn);
105         if (value == null) {
106             throw new PException("No field " + fn + " found among "
107                 + field2value.keySet());
108         }
109         if (value instanceof Short JavaDoc) {
110             return ((Short JavaDoc) value).shortValue();
111         }
112         throw new PException("Bad type: expected " + value.getClass().getName());
113     }
114
115     public Short JavaDoc pngetOshortField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
116         Object JavaDoc value = field2value.get(fn);
117         if (value == null) {
118             if (field2value.containsKey(fn)) {
119                 return null;
120             } else {
121                 throw new PException("No field " + fn + " found among "
122                 + field2value.keySet());
123             }
124         }
125         if (value instanceof Short JavaDoc) {
126             return (Short JavaDoc) value;
127         }
128         throw new PException("Bad type: expected " + value.getClass().getName());
129     }
130
131     public int pngetIntField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
132         Object JavaDoc value = field2value.get(fn);
133         if (value == null) {
134             throw new PException("No field " + fn + " found among "
135                 + field2value.keySet());
136         }
137         if (value instanceof Integer JavaDoc) {
138             return ((Integer JavaDoc) value).intValue();
139         }
140         throw new PException("Bad type: expected " + value.getClass().getName());
141     }
142
143     public Integer JavaDoc pngetOintField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
144         Object JavaDoc value = field2value.get(fn);
145         if (value == null) {
146             if (field2value.containsKey(fn)) {
147                 return null;
148             } else {
149                 throw new PException("No field " + fn + " found among "
150                 + field2value.keySet());
151             }
152         }
153         if (value instanceof Integer JavaDoc) {
154             return (Integer JavaDoc) value;
155         }
156         throw new PException("Bad type: expected " + value.getClass().getName());
157     }
158
159     public long pngetLongField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
160         Object JavaDoc value = field2value.get(fn);
161         if (value == null) {
162             throw new PException("No field " + fn + " found among "
163                 + field2value.keySet());
164         }
165         if (value instanceof Long JavaDoc) {
166             return ((Long JavaDoc) value).longValue();
167         }
168         throw new PException("Bad type: expected " + value.getClass().getName());
169     }
170
171     public Long JavaDoc pngetOlongField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
172         Object JavaDoc value = field2value.get(fn);
173         if (value == null) {
174             if (field2value.containsKey(fn)) {
175                 return null;
176             } else {
177                 throw new PException("No field " + fn + " found among "
178                 + field2value.keySet());
179             }
180         }
181         if (value instanceof Long JavaDoc) {
182             return (Long JavaDoc) value;
183         }
184         throw new PException("Bad type: expected " + value.getClass().getName());
185     }
186
187     public String JavaDoc pngetStringField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
188         Object JavaDoc value = field2value.get(fn);
189         if (value == null) {
190             if (field2value.containsKey(fn)) {
191                 return null;
192             } else {
193                 throw new PException("No field " + fn + " found among "
194                 + field2value.keySet());
195             }
196         }
197         if (value instanceof String JavaDoc) {
198             return (String JavaDoc) value;
199         }
200         throw new PException("Bad type: expected " + value.getClass().getName());
201     }
202
203     public byte[] pngetByteArrayField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
204         Object JavaDoc value = field2value.get(fn);
205         if (value == null) {
206             if (field2value.containsKey(fn)) {
207                 return null;
208             } else {
209                 throw new PException("No field " + fn + " found among "
210                 + field2value.keySet());
211             }
212         }
213         if (value instanceof byte[]) {
214             return (byte[]) value;
215         }
216         throw new PException("Bad type: expected " + value.getClass().getName());
217     }
218
219     public char[] pngetCharArrayField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
220         Object JavaDoc value = field2value.get(fn);
221         if (value == null) {
222             if (field2value.containsKey(fn)) {
223                 return null;
224             } else {
225                 throw new PException("No field " + fn + " found among "
226                 + field2value.keySet());
227             }
228         }
229         if (value instanceof char[]) {
230             return (char[]) value;
231         }
232         throw new PException("Bad type: expected " + value.getClass().getName());
233     }
234
235     public Date JavaDoc pngetDateField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
236         Object JavaDoc value = field2value.get(fn);
237         if (value == null) {
238             if (field2value.containsKey(fn)) {
239                 return null;
240             } else {
241                 throw new PException("No field " + fn + " found among "
242                 + field2value.keySet());
243             }
244         }
245         if (value instanceof Date JavaDoc) {
246             return (Date JavaDoc) value;
247         }
248         throw new PException("Bad type: expected " + value.getClass().getName());
249     }
250
251     public BigInteger JavaDoc pngetBigIntegerField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
252         Object JavaDoc value = field2value.get(fn);
253         if (value == null) {
254             if (field2value.containsKey(fn)) {
255                 return null;
256             } else {
257                 throw new PException("No field " + fn + " found among "
258                 + field2value.keySet());
259             }
260         }
261         if (value instanceof BigInteger JavaDoc) {
262             return (BigInteger JavaDoc) value;
263         }
264         throw new PException("Bad type: expected " + value.getClass().getName());
265     }
266
267     public BigDecimal JavaDoc pngetBigDecimalField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
268         Object JavaDoc value = field2value.get(fn);
269         if (value == null) {
270             if (field2value.containsKey(fn)) {
271                 return null;
272             } else {
273                 throw new PException("No field " + fn + " found among "
274                 + field2value.keySet());
275             }
276         }
277         if (value instanceof BigDecimal JavaDoc) {
278             return (BigDecimal JavaDoc) value;
279         }
280         throw new PException("Bad type: expected " + value.getClass().getName());
281     }
282 }
283
Popular Tags