KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  *
22  * @author nn136682
23  */

24 package org.netbeans.modules.xml.schema.model.impl;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
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 JavaDoc> T parse(Class JavaDoc<T> type, String JavaDoc s) {
36         try {
37             Method JavaDoc m = type.getMethod("values", new Class JavaDoc[] {});
38             T[] values = (T[]) (m.invoke(null, new Object JavaDoc[0]));
39             for (T value : values) {
40                 if (value.toString().equals(s)) {
41                     return value;
42                 }
43             }
44         } catch(Exception JavaDoc e) {
45             throw new RuntimeException JavaDoc(e);
46         }
47         throw new IllegalArgumentException JavaDoc("Invalid String value " + s);
48     }
49     
50     public static <F extends Enum JavaDoc, T extends Enum JavaDoc> Set JavaDoc<T> convertEnumSet(Class JavaDoc<T> toType, Set JavaDoc<F> values) {
51         Set JavaDoc<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 JavaDoc> Set JavaDoc<T> valuesOf(Class JavaDoc<T> type, String JavaDoc s) {
60 // StringTokenizer tokenizer = new StringTokenizer(s, SEP);
61
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(s); // to escape tabs and new lines as well
62
Set JavaDoc<T> result = new DerivationSet<T>();
63         if(tokenizer.countTokens()==0) { // to consider blank ("") string
64
T value = parse(type, s);
65             result.add(value);
66         } else {
67             while (tokenizer.hasMoreTokens()) {
68                 String JavaDoc 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 JavaDoc SEP = " ";
77 }
78
Popular Tags