01: /**
02: * Copyright (C) 2001-2004 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * 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: */package org.objectweb.speedo.lib;
18:
19: import java.util.Locale;
20: import java.util.StringTokenizer;
21:
22: import org.objectweb.speedo.api.UserFieldMapping;
23:
24: /**
25: *
26: * @author S.Chassande-Barrioz
27: */
28: public class LocaleFieldMapping implements UserFieldMapping {
29:
30: /**
31: * Retrieves the java type corresponding to the type into the data support.
32: * @return a Class object (never null).
33: */
34: public Class getStorageType() {
35: return String.class;
36: }
37:
38: /**
39: * Retrieves the java type corresponding to the type in memory.
40: * @return a Class object (never null).
41: */
42: public Class getMemoryType() {
43: return Locale.class;
44: }
45:
46: /**
47: * Converts a value from the data support into a value in memory
48: * @param storagevalue is the value store in the support (can be null).
49: * @return the value in memory (can be null).
50: */
51: public Object toMemory(Object storagevalue) {
52: if (storagevalue == null) {
53: return null;
54: }
55:
56: if (storagevalue instanceof Locale) {
57: return storagevalue;
58: } else {
59: StringTokenizer tokenizer = new StringTokenizer(
60: (String) storagevalue, "_");
61: String[] names = new String[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 token = tokenizer.nextToken();
68: names[i] = token;
69: }
70:
71: Locale locale;
72: locale = new Locale(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 toStorage(Object memoryvalue) {
83: if (memoryvalue == null) {
84: return null;
85: }
86: return ((Locale) memoryvalue).toString();
87: }
88: }
|