Package br.net.woodstock.rockframework.core.utils

Source Code of br.net.woodstock.rockframework.core.utils.Enums

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.core.utils;

import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.core.util.LabelSupport;

public abstract class Enums {

  private Enums() {
    //
  }

  @SuppressWarnings("rawtypes")
  public static String getLabel(final Enum e) {
    return Enums.getLabel(e, null);
  }

  @SuppressWarnings("rawtypes")
  public static String getLabel(final Enum e, final String defaultLabel) {
    if (e != null) {
      if (e instanceof LabelSupport) {
        LabelSupport labelSupport = (LabelSupport) e;
        return labelSupport.getLabel();
      }
      return e.name();
    }
    return defaultLabel;
  }

  @SuppressWarnings("rawtypes")
  public static String getName(final Enum e) {
    return Enums.getName(e, null);
  }

  @SuppressWarnings("rawtypes")
  public static String getName(final Enum e, final String defaultName) {
    if (e != null) {
      return e.name();
    }
    return defaultName;
  }

  @SuppressWarnings("rawtypes")
  public static int getOrdinal(final Enum e) {
    return Enums.getOrdinal(e, -1);
  }

  @SuppressWarnings("rawtypes")
  public static int getOrdinal(final Enum e, final int defaultOrdinal) {
    if (e != null) {
      return e.ordinal();
    }
    return defaultOrdinal;
  }

  @SuppressWarnings("rawtypes")
  public static <T extends Enum> T getByName(final Class<T> clazz, final String name) {
    Assert.notNull(clazz, "clazz");
    if (clazz.isEnum()) {
      if (Conditions.isNotEmpty(name)) {
        T[] array = clazz.getEnumConstants();
        for (T t : array) {
          if (t.name().equals(name)) {
            return t;
          }
        }
      }
    }
    return null;
  }

  @SuppressWarnings("rawtypes")
  public static <T extends Enum> T getByOrdinal(final Class<T> clazz, final int ordinal) {
    Assert.notNull(clazz, "clazz");
    if (clazz.isEnum()) {
      T[] array = clazz.getEnumConstants();
      if (array.length > ordinal) {
        return array[ordinal];
      }
    }
    return null;
  }

  @SuppressWarnings("rawtypes")
  public static <T extends Enum> T[] getValues(final Class<T> clazz) {
    Assert.notNull(clazz, "clazz");
    if (clazz.isEnum()) {
      T[] array = clazz.getEnumConstants();
      return array;
    }
    return null;
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.core.utils.Enums

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.