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