1 19 20 24 package org.netbeans.modules.xml.schema.model.impl; 25 26 import java.lang.reflect.Method ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import java.util.StringTokenizer ; 30 import org.netbeans.modules.xml.schema.model.Derivation; 31 import org.netbeans.modules.xml.schema.model.impl.DerivationsImpl.DerivationSet; 32 33 public class Util { 34 35 public static <T extends Enum > T parse(Class <T> type, String s) { 36 try { 37 Method m = type.getMethod("values", new Class [] {}); 38 T[] values = (T[]) (m.invoke(null, new Object [0])); 39 for (T value : values) { 40 if (value.toString().equals(s)) { 41 return value; 42 } 43 } 44 } catch(Exception e) { 45 throw new RuntimeException (e); 46 } 47 throw new IllegalArgumentException ("Invalid String value " + s); 48 } 49 50 public static <F extends Enum , T extends Enum > Set <T> convertEnumSet(Class <T> toType, Set <F> values) { 51 Set <T> result = new DerivationSet<T>(); 52 for (F v : values) { 53 T t = toType.cast(Enum.valueOf(toType, v.name())); 54 result.add(t); 55 } 56 return result; 57 } 58 59 public static <T extends Enum > Set <T> valuesOf(Class <T> type, String s) { 60 StringTokenizer tokenizer = new StringTokenizer (s); Set <T> result = new DerivationSet<T>(); 63 if(tokenizer.countTokens()==0) { T value = parse(type, s); 65 result.add(value); 66 } else { 67 while (tokenizer.hasMoreTokens()) { 68 String token = tokenizer.nextToken(); 69 T value = parse(type, token); 70 result.add(value); 71 } 72 } 73 return result; 74 } 75 76 public static final String SEP = " "; 77 } 78 | Popular Tags |