KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > lib > LocaleFieldMapping


1 /**
2  * Copyright (C) 2001-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.speedo.lib;
19
20 import java.util.Locale JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.objectweb.speedo.api.UserFieldMapping;
24
25 /**
26  *
27  * @author S.Chassande-Barrioz
28  */

29 public class LocaleFieldMapping implements UserFieldMapping {
30
31     /**
32      * Retrieves the java type corresponding to the type into the data support.
33      * @return a Class object (never null).
34      */

35     public Class JavaDoc getStorageType() {
36         return String JavaDoc.class;
37     }
38
39     /**
40      * Retrieves the java type corresponding to the type in memory.
41      * @return a Class object (never null).
42      */

43     public Class JavaDoc getMemoryType() {
44         return Locale JavaDoc.class;
45     }
46
47     /**
48      * Converts a value from the data support into a value in memory
49      * @param storagevalue is the value store in the support (can be null).
50      * @return the value in memory (can be null).
51      */

52     public Object JavaDoc toMemory(Object JavaDoc storagevalue) {
53         if (storagevalue == null) {
54             return null;
55         }
56         
57         if (storagevalue instanceof Locale JavaDoc) {
58             return storagevalue;
59         } else {
60             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc((String JavaDoc)storagevalue, "_");
61             String JavaDoc[] names = new String JavaDoc[3];
62             int max = tokenizer.countTokens();
63             for (int i=0; i<3; i++) {
64                 names[i] = "";
65             }
66             for (int i=0; i<max; i++) {
67                 String JavaDoc token = tokenizer.nextToken();
68                 names[i] = token;
69             }
70             
71             Locale JavaDoc locale;
72             locale = new Locale JavaDoc(names[0], names[1], names[2]);
73             return locale;
74         }
75     }
76
77     /**
78      * Converts a value from the memory into a value in data support
79      * @param memoryvalue the value in memory (can be null).
80      * @return is the value store in the support (can be null).
81      */

82     public Object JavaDoc toStorage(Object JavaDoc memoryvalue) {
83         if (memoryvalue == null) {
84             return null;
85         }
86         return ((Locale JavaDoc)memoryvalue).toString();
87     }
88 }
89
Popular Tags