KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > conversion > StringToBooleanPrimitiveConverter


1 /*
2  * Copyright (C) 2005 db4objects Inc. http://www.db4o.com
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * db4objects - Initial API and implementation
11  */

12 package org.eclipse.core.internal.databinding.conversion;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.eclipse.core.databinding.conversion.IConverter;
20 import org.eclipse.core.internal.databinding.BindingMessages;
21
22 /**
23  * StringToBooleanPrimitiveConverter.
24  */

25 public class StringToBooleanPrimitiveConverter implements IConverter {
26     private static final String JavaDoc[] trueValues;
27
28     private static final String JavaDoc[] falseValues;
29
30     static {
31         String JavaDoc delimiter = BindingMessages.getString("ValueDelimiter"); //$NON-NLS-1$
32
String JavaDoc values = BindingMessages.getString("TrueStringValues"); //$NON-NLS-1$
33
trueValues = valuesToSortedArray(delimiter, values);
34
35         values = BindingMessages.getString("FalseStringValues"); //$NON-NLS-1$
36
falseValues = valuesToSortedArray(delimiter, values);
37     }
38
39     /**
40      * Returns a sorted array with all values converted to upper case.
41      *
42      * @param delimiter
43      * @param values
44      * @return sorted array of values
45      */

46     private static String JavaDoc[] valuesToSortedArray(String JavaDoc delimiter, String JavaDoc values) {
47         List JavaDoc list = new LinkedList JavaDoc();
48         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(values, delimiter);
49         while (tokenizer.hasMoreTokens()) {
50             list.add(tokenizer.nextToken().toUpperCase());
51         }
52
53         String JavaDoc[] array = (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
54         Arrays.sort(array);
55
56         return array;
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
63      */

64     public Object JavaDoc convert(Object JavaDoc source) {
65         String JavaDoc s = (String JavaDoc) source;
66         s = s.toUpperCase();
67
68         if (Arrays.binarySearch(trueValues, s) > -1) {
69             return Boolean.TRUE;
70         }
71
72         if (Arrays.binarySearch(falseValues, s) > -1) {
73             return Boolean.FALSE;
74         }
75
76         throw new IllegalArgumentException JavaDoc(s + " is not a legal boolean value"); //$NON-NLS-1$
77
}
78
79     public Object JavaDoc getFromType() {
80         return String JavaDoc.class;
81     }
82
83     public Object JavaDoc getToType() {
84         return Boolean.TYPE;
85     }
86
87 }
88
Popular Tags