Debian Codegraph @

src/com/jidesoft/converter/MultipleEnumConverter.java

raw
1 /*
2 * @(#)MultipleEnumConverter.java 7/5/2008
3 *
4 * Copyright 2002 - 2008 JIDE Software Inc. All rights reserved.
5 *
6 */
7
8 package com.jidesoft.converter;
9
10 import java.lang.reflect.Array;
11
12 /**
13 * MultipleEnumConverter is a special ArrayConverter that converts a string to/from array. Each element in the element
14 * is converted to the object using a specified EnumConverter.
15 */
16 public class MultipleEnumConverter extends ArrayConverter {
17 private EnumConverter _enumConverter;
18
19 public MultipleEnumConverter(String separator, EnumConverter converter) {
20 super(separator, -1, converter.getType());
21 _enumConverter = converter;
22 }
23
24 public Class<?> getType() {
25 return Array.newInstance(_enumConverter.getType(), 0).getClass();
26 }
27
28 public EnumConverter getEnumConverter() {
29 return _enumConverter;
30 }
31
32 public void setEnumConverter(EnumConverter enumConverter) {
33 _enumConverter = enumConverter;
34 }
35
36 public String toString(Object object, ConverterContext context) {
37 if (object == null) {
38 return "";
39 }
40 if (object.getClass().isArray()) {
41 int length = Array.getLength(object);
42 Object[] values = new Object[length];
43 for (int i = 0; i < length; i++) {
44 Object o = Array.get(object, i);
45 values[i] = o;
46 }
47 return arrayToString(values, context);
48 }
49 return "";
50 }
51
52 public boolean supportToString(Object object, ConverterContext context) {
53 return true;
54 }
55
56 public Object fromString(String string, ConverterContext context) {
57 return arrayFromString(string, context);
58 }
59
60 public boolean supportFromString(String string, ConverterContext context) {
61 return true;
62 }
63
64 @Override
65 protected String toString(int i, Object o, ConverterContext context) {
66 return _enumConverter != null ? _enumConverter.toString(o, context) : "" + o;
67 }
68
69 @Override
70 protected Object fromString(int i, String s, ConverterContext context) {
71 return _enumConverter != null ? _enumConverter.fromString(s, context) : s;
72 }
73
74 transient private ConverterContext _conext;
75
76 /**
77 * Gets the converter context of this converter. The name of the context is the name of the converter where you pass
78 * in to EnumConverter's constructor.
79 *
80 * @return the converter context of this converter.
81 */
82 public ConverterContext getContext() {
83 if (_conext == null) {
84 _conext = ConverterContext.getArrayConverterContext(_enumConverter.getContext());
85 }
86 return _conext;
87 }
88
89 // public static void main(String[] args) {
90 // final EnumConverter genderConverter = new EnumConverter("Gender", String.class,
91 // new String[]{
92 // "*",
93 // "F",
94 // "M",
95 // },
96 // new String[]{
97 // "Any",
98 // "Female",
99 // "Male",
100 // },
101 // "");
102 // MultipleEnumConverter converter = new MultipleEnumConverter("; ", genderConverter);
103 // Object o = converter.fromString("Female; Male", null);
104 // String s = converter.toString(new String[]{"F", "M"}, null);
105 // System.out.println(s);
106 // }
107 }