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 static org.xnio._private.Messages.msg;
23
24 /**
25 * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
26 */
27 final class SingleOption<T> extends Option<T> {
28
29 private static final long serialVersionUID = 2449094406108952764L;
30
31 private final transient Class<T> type;
32 private final transient ValueParser<T> parser;
33
34 SingleOption(final Class<?> declClass, final String name, final Class<T> type) {
35 super(declClass, name);
36 if (type == null) {
37 throw msg.nullParameter("type");
38 }
39 this.type = type;
40 parser = Option.getParser(type);
41 }
42
43 public T cast(final Object o) {
44 return type.cast(o);
45 }
46
47 public T parseValue(final String string, final ClassLoader classLoader) throws IllegalArgumentException {
48 return parser.parseValue(string, classLoader);
49 }
50 }
51