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. The name of the class to retrieve. Can be either a normal class or a
031   * primitive class.
032   * @return The class specified by <code>className</code> If the requested class can not be found.
033   */
034  public static Class<?> extendedForName(String className) throws ClassNotFoundException {
035    Class<?> valueType;
036    if (className.equals("boolean")) {
037      valueType = boolean.class;
038    } else if (className.equals("byte")) {
039      valueType = byte.class;
040    } else if (className.equals("short")) {
041      valueType = short.class;
042    } else if (className.equals("int")) {
043      valueType = int.class;
044    } else if (className.equals("long")) {
045      valueType = long.class;
046    } else if (className.equals("float")) {
047      valueType = float.class;
048    } else if (className.equals("double")) {
049      valueType = double.class;
050    } else if (className.equals("char")) {
051      valueType = char.class;
052    } else {
053      valueType = Class.forName(className);
054    }
055    return valueType;
056  }
057
058  public static String stringify(Class<?>[] classes) {
059    StringBuilder buf = new StringBuilder();
060    if (classes != null) {
061      for (Class<?> c : classes) {
062        if (buf.length() > 0) {
063          buf.append(",");
064        }
065        buf.append(c.getName());
066      }
067    } else {
068      buf.append("NULL");
069    }
070    return buf.toString();
071  }
072
073  @SuppressWarnings("unchecked")
074  public static <T> Class<T> cast(Class<?> clazz) {
075    return (Class<T>) clazz;
076  }
077}