View Javadoc

1   /*
2    * Copyright 2004 Ronald Blaschke.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rblasch.convert.type;
17  
18  import org.rblasch.convert.assertion.Assertion;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  /***
24   * Created by IntelliJ IDEA.
25   * User: rb
26   * Date: Oct 27, 2004
27   * Time: 10:26:35 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class PrimitiveType extends AbstractType {
31      final static Map valueOfMap = new HashMap();
32      static {
33          valueOfMap.put("byte", new PrimitiveType(Byte.TYPE));
34          valueOfMap.put("short", new PrimitiveType(Short.TYPE));
35          valueOfMap.put("int", new PrimitiveType(Integer.TYPE));
36          valueOfMap.put("long", new PrimitiveType(Long.TYPE));
37          valueOfMap.put("float", new PrimitiveType(Float.TYPE));
38          valueOfMap.put("double", new PrimitiveType(Double.TYPE));
39      }
40  
41      static PrimitiveType valueOf(final String s) throws UnknownTypeException {
42          final PrimitiveType type = (PrimitiveType) valueOfMap.get(s);
43          if (type != null) {
44              return type;
45          } else {
46              throw new UnknownTypeException(s + " is not a known primitive type");
47          }
48      }
49  
50      private final Class t;
51  
52      PrimitiveType(final Class t) {
53          Assertion.ensure(t.isPrimitive(), "t must be primitive");
54          this.t = t;
55      }
56  
57      public Class getTheClass() {
58          return t;
59      }
60  
61      public String getName() {
62          return t.getName();
63      }
64  }