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