001/*
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020package org.apache.hadoop.hbase.util;
021
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Utilities for class manipulation.
026 */
027@InterfaceAudience.Private
028public class Classes {
029
030  /**
031   * Equivalent of {@link Class#forName(String)} which also returns classes for
032   * primitives like <code>boolean</code>, etc.
033   *
034   * @param className
035   *          The name of the class to retrieve. Can be either a normal class or
036   *          a primitive class.
037   * @return The class specified by <code>className</code>
038   * @throws ClassNotFoundException
039   *           If the requested class can not be found.
040   */
041  public static Class<?> extendedForName(String className)
042      throws ClassNotFoundException {
043    Class<?> valueType;
044    if (className.equals("boolean")) {
045      valueType = boolean.class;
046    } else if (className.equals("byte")) {
047      valueType = byte.class;
048    } else if (className.equals("short")) {
049      valueType = short.class;
050    } else if (className.equals("int")) {
051      valueType = int.class;
052    } else if (className.equals("long")) {
053      valueType = long.class;
054    } else if (className.equals("float")) {
055      valueType = float.class;
056    } else if (className.equals("double")) {
057      valueType = double.class;
058    } else if (className.equals("char")) {
059      valueType = char.class;
060    } else {
061      valueType = Class.forName(className);
062    }
063    return valueType;
064  }
065
066  public static String stringify(Class<?>[] classes) {
067    StringBuilder buf = new StringBuilder();
068    if (classes != null) {
069      for (Class<?> c : classes) {
070        if (buf.length() > 0) {
071          buf.append(",");
072        }
073        buf.append(c.getName());
074      }
075    } else {
076      buf.append("NULL");
077    }
078    return buf.toString();
079  }
080
081  @SuppressWarnings("unchecked")
082  public static <T> Class<T> cast(Class<?> clazz) {
083    return (Class<T>) clazz;
084  }
085}