/******************************************************************************
 * Copyright (c) 2009-2011 Ascent Dimension, Inc. All rights reserved.
 ******************************************************************************/

package com.github.haixing_hu.lang;

import javax.annotation.Nullable;


/**
 * Provides utilities functions for {@link Enum}.
 *
 * @author Haixing Hu
 */
public class EnumUtils {

  /**
   * Gets the short name of an {@link Enum} value.
   * <p>
   * A short name of an {@link Enum} value comes from lowercase the enumeration
   * name of the {@link Enum} value and replacing all '_' with '-'.
   * </p>
   *
   * @param <E>
   *          The class object of a {@link Enum} class.
   * @param e
   *          A {@link Enum} value.
   * @return The short name of the {@link Enum} value.
   */
  public static <E extends Enum<E>> String getShortName(final E e) {
    return e.name().toLowerCase().replace('_', '-');
  }

  /**
   * Gets the enumeration name of a short name of an {@link Enum} value.
   * <p>
   * A short name of an {@link Enum} value comes from lowercase the enumeration
   * name of the {@link Enum} value and replacing all '_' with '-'.
   * </p>
   *
   * @param shortName
   *          The short name of an {@link Enum} value.
   * @return The enumeration name of the {@link Enum} value.
   */
  public static String getFullName(final String shortName) {
    return shortName.replace('-', '_').toUpperCase();
  }

  /**
   * Gets the {@link Enum} value accroding to its name.
   *
   * @param <E>
   *          The class object of a {@link Enum} class.
   * @param name
   *          The name of an {@link Enum} value, either the enumeration name or
   *          the short name.
   * @param isShortName
   *          whether the name is the short name.
   * @param ignoreCase
   *          whether to ignore the case while comparing strings.
   * @param enumClass
   *          The class object of the {@link Enum} class.
   * @return
   */
  public static <E extends Enum<E>> E forName(final String name,
      final boolean isShortName, final boolean ignoreCase,
      final Class<E> enumClass) {
    final String enumName = (isShortName ? getFullName(name) : name);
    final E[] enumValues = enumClass.getEnumConstants();
    for (int i = 0; i < enumValues.length; ++i) {
      if (StringUtils.equals(enumName, enumValues[i].name(), ignoreCase)) {
        return enumValues[i];
      }
    }
    return null;
  }

  public static <E extends Enum<E>> String toString(@Nullable final E value,
      final boolean useShortName) {
    if (value == null) {
      return null;
    } else if (useShortName) {
      return getShortName(value);
    } else {
      return value.name();
    }
  }
}

@@ -1026,33 +1026,101 @@
  value.%0A   *
- %3Cp%3E
 %0A   *
+ @param shortName%0A   *          The short name of an %7B@link Enum%7D value.
  A short nam
@@ -1118,32 +1118,46 @@
 short name of an
+%0A   *         
  %7B@link Enum%7D va
@@ -1192,37 +1192,32 @@
  enumeration
-%0A   *
  name of
  the %7B@link 
@@ -1196,32 +1196,46 @@
 meration name of
+%0A   *         
  the %7B@link Enum
@@ -1278,100 +1278,8 @@
 -'.%0A
-   * %3C/p%3E%0A   *%0A   * @param shortName%0A   *          The short name of an %7B@link Enum%7D value.%0A
    *
@@ -1718,32 +1718,198 @@
  the short name.
+ A short name of an %7B@link Enum%7D value comes from%0A   *          lowercase the enumeration name of the %7B@link Enum%7D value and%0A   *          replacing all '_' with '-'.
 %0A   * @param isS
@@ -2632,16 +2632,625 @@
 l;%0A  %7D%0A%0A
+  /**%0A   * Converts an enumerator to its string representation.%0A   *%0A   * @param value%0A   *          an enumerator.%0A   * @param useShortName%0A   *          indicates whether to convert to short name of the enumerator. A%0A   *          short name of an %7B@link Enum%7D value comes from lowercase the%0A   *          enumeration name of the %7B@link Enum%7D value and replacing all '_'%0A   *          with '-'. If this argument is %3Ccode%3Efalse%3C/code%3E, the string%0A   *          representation of an enumerator is its name (without case%0A   *          changing).%0A   * @return the string representation of the enumerator.%0A   */%0A
   public

modify some comments