Java Class.getEnumConstants Examples

Java Class.getEnumConstants - 30 examples found. These are the top rated real world Java examples of Class.getEnumConstants extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  protected static <T extends Enum<T>> T parseEnum(
      Class<T> enumClass, String name, T defaultValue, Pair<String, T>... additionalPairs) {
    if (name == null) {
      return defaultValue;
    }

    name = name.toLowerCase();
    for (T enumConstant : enumClass.getEnumConstants()) {
      if (enumConstant.name().equalsIgnoreCase(name)) {
        return enumConstant;
      }
    }
    for (T enumConstant : enumClass.getEnumConstants()) {
      if (enumConstant.name().toLowerCase().startsWith(name)) {
        return enumConstant;
      }
    }

    for (Pair<String, T> additional : additionalPairs) {
      if (additional.first().equalsIgnoreCase(name)) {
        return additional.other();
      }
    }
    for (Pair<String, T> additional : additionalPairs) {
      if (additional.first().toLowerCase().startsWith(name)) {
        return additional.other();
      }
    }

    throw new IllegalArgumentException("No '" + name + "' or '" + name + ".*' in " + enumClass);
  }
Example #2
0
 public void addMaterials(Class<?> c) {
   if (!c.isEnum()) return;
   if (!(c.getEnumConstants() instanceof Material[])) return;
   Material[] ms = (Material[]) c.getEnumConstants();
   for (Material m : ms) {
     materials.put(m.getSymbol(), m);
   }
 }
 public JSSPopupPropertyDescriptor(
     Object id, String displayName, Class<? extends JREnum> jrEnum, NullEnum type) {
   super(id, displayName, EnumHelper.getEnumNames(jrEnum.getEnumConstants(), type));
   this.type = type;
   jrEnums = jrEnum.getEnumConstants();
   this.start = jrEnums[0].getValue();
   setItemsToDefault();
 }
  @SuppressWarnings({"rawtypes", "unchecked"})
  public static void enterA2Mode() {
    System.err.println("APV::enterA2Mode");
    try {
      Class epdControllerClass = Class.forName("android.hardware.EpdController");
      Class epdControllerRegionClass = Class.forName("android.hardware.EpdController$Region");
      Class epdControllerRegionParamsClass =
          Class.forName("android.hardware.EpdController$RegionParams");
      Class epdControllerWaveClass = Class.forName("android.hardware.EpdController$Wave");

      Object[] waveEnums = null;
      if (epdControllerWaveClass.isEnum()) {
        System.err.println("EpdController Wave Enum successfully retrived.");
        waveEnums = epdControllerWaveClass.getEnumConstants();
      }

      Object[] regionEnums = null;
      if (epdControllerRegionClass.isEnum()) {
        System.err.println("EpdController Region Enum successfully retrived.");
        regionEnums = epdControllerRegionClass.getEnumConstants();
      }

      Constructor RegionParamsConstructor =
          epdControllerRegionParamsClass.getConstructor(
              new Class[] {
                Integer.TYPE,
                Integer.TYPE,
                Integer.TYPE,
                Integer.TYPE,
                epdControllerWaveClass,
                Integer.TYPE
              });

      Object localRegionParams =
          RegionParamsConstructor.newInstance(
              new Object[] {0, 0, 600, 800, waveEnums[2], 16}); // Wave = DU

      Method epdControllerSetRegionMethod =
          epdControllerClass.getMethod(
              "setRegion",
              new Class[] {String.class, epdControllerRegionClass, epdControllerRegionParamsClass});
      epdControllerSetRegionMethod.invoke(
          null, new Object[] {"APV-ReadingView", regionEnums[2], localRegionParams});

      Thread.sleep(100L);
      localRegionParams =
          RegionParamsConstructor.newInstance(
              new Object[] {0, 0, 600, 800, waveEnums[3], 14}); // Wave = A2
      epdControllerSetRegionMethod.invoke(
          null, new Object[] {"APV-ReadingView", regionEnums[2], localRegionParams});

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public JSSPopupPropertyDescriptor(
     Object id,
     String displayName,
     Class<? extends JREnum> jrEnum,
     NullEnum type,
     List<ComboItem> items) {
   super(id, displayName, EnumHelper.getEnumNames(jrEnum.getEnumConstants(), type));
   jrEnums = jrEnum.getEnumConstants();
   this.start = jrEnums[0].getValue();
   this.items = items;
 }
Example #6
0
  /**
   * Get the enum value with the passed ID
   *
   * @param <ENUMTYPE> The enum type
   * @param aClass The enum class
   * @param nID The ID to search
   * @param aDefault The default value to be returned, if the ID was not found.
   * @return The default parameter if no enum item with the given ID is present.
   */
  @Nullable
  public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasSimpleIntID> ENUMTYPE getFromIDOrDefault(
      @Nonnull final Class<ENUMTYPE> aClass, final int nID, @Nullable final ENUMTYPE aDefault) {
    ValueEnforcer.notNull(aClass, "Class");

    final String sCacheKey = aClass.getName();
    Object[] aCachedData;
    s_aRWLockInt.readLock().lock();
    try {
      aCachedData = s_aIntCache.get(sCacheKey);
    } finally {
      s_aRWLockInt.readLock().unlock();
    }
    if (aCachedData == null) {
      s_aRWLockInt.writeLock().lock();
      try {
        // Try again in write lock
        aCachedData = s_aIntCache.get(sCacheKey);
        if (aCachedData == null) {
          // Create new cache entry
          int nMinID = Integer.MAX_VALUE;
          int nMaxID = Integer.MIN_VALUE;
          for (final ENUMTYPE aElement : aClass.getEnumConstants()) {
            final int nElementID = aElement.getID();
            if (nElementID < nMinID) nMinID = nElementID;
            if (nElementID > nMaxID) nMaxID = nElementID;
          }
          if (nMinID >= 0 && nMaxID <= CGlobal.MAX_BYTE_VALUE) {
            // Cachable!
            aCachedData = new Object[nMaxID + 1];
            for (final ENUMTYPE aElement : aClass.getEnumConstants())
              aCachedData[aElement.getID()] = aElement;
          } else {
            // Enum not cachable
            aCachedData = NOT_CACHABLE;
          }
          s_aIntCache.put(sCacheKey, aCachedData);
        }
      } finally {
        s_aRWLockInt.writeLock().unlock();
      }
    }

    if (aCachedData != NOT_CACHABLE) {
      if (nID < 0 || nID >= aCachedData.length) return aDefault;
      return GenericReflection.<Object, ENUMTYPE>uncheckedCast(aCachedData[nID]);
    }

    // Object is not cachable - traverse as ususal
    for (final ENUMTYPE aElement : aClass.getEnumConstants())
      if (aElement.getID() == nID) return aElement;
    return aDefault;
  }