1 /*
2  * JBoss, Home of Professional Open Source.
3  *
4  * Copyright 2011 Red Hat, Inc. and/or its affiliates, and individual
5  * contributors as indicated by the @author tags.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.xnio;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25
26 import static org.xnio._private.Messages.msg;
27
28 /**
29  * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
30  */

31 final class TypeSequenceOption<T> extends Option<Sequence<Class<? extends T>>> {
32
33     private static final long serialVersionUID = -4328676629293125136L;
34
35     private final transient Class<T> elementDeclType;
36     private final transient ValueParser<Class<? extends T>> parser;
37
38     TypeSequenceOption(final Class<?> declClass, final String name, final Class<T> elementDeclType) {
39         super(declClass, name);
40         if (elementDeclType == null) {
41             throw msg.nullParameter("elementDeclType");
42         }
43         this.elementDeclType = elementDeclType;
44         parser = Option.getClassParser(elementDeclType);
45     }
46
47     public Sequence<Class<? extends T>> cast(final Object o) {
48         if (o == null) {
49             return null;
50         } else if (o instanceof Sequence) {
51             return castSeq((Sequence<?>) o, elementDeclType);
52         } else if (o instanceof Object[]){
53             return castSeq(Sequence.of((Object[]) o), elementDeclType);
54         } else if (o instanceof Collection) {
55             return castSeq(Sequence.of((Collection<?>) o), elementDeclType);
56         } else {
57             throw new ClassCastException("Not a sequence");
58         }
59     }
60
61     @SuppressWarnings("unchecked")
62     static <T> Sequence<Class<? extends T>> castSeq(Sequence<?> seq, Class<T> type) {
63         for (Object o : seq) {
64             ((Class<?>)o).asSubclass(type);
65         }
66         return (Sequence<Class<? extends T>>) seq;
67     }
68
69     public Sequence<Class<? extends T>> parseValue(final String string, final ClassLoader classLoader) throws IllegalArgumentException {
70         final List<Class<? extends T>> list = new ArrayList<Class<? extends T>>();
71         if (string.isEmpty()) {
72             return Sequence.empty();
73         }
74         for (String value : string.split(",")) {
75             list.add(parser.parseValue(value, classLoader));
76         }
77         return Sequence.of(list);
78     }
79 }
80