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  import org.apache.hadoop.classification.InterfaceStability;
25  
26  /**
27   * Utilities for class manipulation.
28   */
29  @InterfaceAudience.Public
30  @InterfaceStability.Stable
31  public class Classes {
32  
33    /**
34     * Equivalent of {@link Class#forName(String)} which also returns classes for
35     * primitives like <code>boolean</code>, etc.
36     * 
37     * @param className
38     *          The name of the class to retrieve. Can be either a normal class or
39     *          a primitive class.
40     * @return The class specified by <code>className</code>
41     * @throws ClassNotFoundException
42     *           If the requested class can not be found.
43     */
44    public static Class<?> extendedForName(String className)
45        throws ClassNotFoundException {
46      Class<?> valueType;
47      if (className.equals("boolean")) {
48        valueType = boolean.class;
49      } else if (className.equals("byte")) {
50        valueType = byte.class;
51      } else if (className.equals("short")) {
52        valueType = short.class;
53      } else if (className.equals("int")) {
54        valueType = int.class;
55      } else if (className.equals("long")) {
56        valueType = long.class;
57      } else if (className.equals("float")) {
58        valueType = float.class;
59      } else if (className.equals("double")) {
60        valueType = double.class;
61      } else if (className.equals("char")) {
62        valueType = char.class;
63      } else {
64        valueType = Class.forName(className);
65      }
66      return valueType;
67    }
68  
69    public static String stringify(Class[] classes) {
70      StringBuilder buf = new StringBuilder();
71      if (classes != null) {
72        for (Class c : classes) {
73          if (buf.length() > 0) {
74            buf.append(",");
75          }
76          buf.append(c.getName());
77        }
78      } else {
79        buf.append("NULL");
80      }
81      return buf.toString();
82    }
83  }