View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  
25  /**
26   * Utilities for class manipulation.
27   */
28  @InterfaceAudience.Private
29  public class Classes {
30  
31    /**
32     * Equivalent of {@link Class#forName(String)} which also returns classes for
33     * primitives like <code>boolean</code>, etc.
34     * 
35     * @param className
36     *          The name of the class to retrieve. Can be either a normal class or
37     *          a primitive class.
38     * @return The class specified by <code>className</code>
39     * @throws ClassNotFoundException
40     *           If the requested class can not be found.
41     */
42    public static Class<?> extendedForName(String className)
43        throws ClassNotFoundException {
44      Class<?> valueType;
45      if (className.equals("boolean")) {
46        valueType = boolean.class;
47      } else if (className.equals("byte")) {
48        valueType = byte.class;
49      } else if (className.equals("short")) {
50        valueType = short.class;
51      } else if (className.equals("int")) {
52        valueType = int.class;
53      } else if (className.equals("long")) {
54        valueType = long.class;
55      } else if (className.equals("float")) {
56        valueType = float.class;
57      } else if (className.equals("double")) {
58        valueType = double.class;
59      } else if (className.equals("char")) {
60        valueType = char.class;
61      } else {
62        valueType = Class.forName(className);
63      }
64      return valueType;
65    }
66  
67    public static String stringify(Class[] classes) {
68      StringBuilder buf = new StringBuilder();
69      if (classes != null) {
70        for (Class c : classes) {
71          if (buf.length() > 0) {
72            buf.append(",");
73          }
74          buf.append(c.getName());
75        }
76      } else {
77        buf.append("NULL");
78      }
79      return buf.toString();
80    }
81  }