1 12 package org.eclipse.core.internal.databinding.conversion; 13 14 import java.util.Arrays ; 15 import java.util.LinkedList ; 16 import java.util.List ; 17 import java.util.StringTokenizer ; 18 19 import org.eclipse.core.databinding.conversion.IConverter; 20 import org.eclipse.core.internal.databinding.BindingMessages; 21 22 25 public class StringToBooleanPrimitiveConverter implements IConverter { 26 private static final String [] trueValues; 27 28 private static final String [] falseValues; 29 30 static { 31 String delimiter = BindingMessages.getString("ValueDelimiter"); String values = BindingMessages.getString("TrueStringValues"); trueValues = valuesToSortedArray(delimiter, values); 34 35 values = BindingMessages.getString("FalseStringValues"); falseValues = valuesToSortedArray(delimiter, values); 37 } 38 39 46 private static String [] valuesToSortedArray(String delimiter, String values) { 47 List list = new LinkedList (); 48 StringTokenizer tokenizer = new StringTokenizer (values, delimiter); 49 while (tokenizer.hasMoreTokens()) { 50 list.add(tokenizer.nextToken().toUpperCase()); 51 } 52 53 String [] array = (String []) list.toArray(new String [list.size()]); 54 Arrays.sort(array); 55 56 return array; 57 } 58 59 64 public Object convert(Object source) { 65 String s = (String ) 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 (s + " is not a legal boolean value"); } 78 79 public Object getFromType() { 80 return String .class; 81 } 82 83 public Object getToType() { 84 return Boolean.TYPE; 85 } 86 87 } 88 | Popular Tags |