Esempio n. 1
1
  /**
   * 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;
  }
Esempio n. 2
0
  /**
   * Get the enum value with the passed name
   *
   * @param <ENUMTYPE> The enum type
   * @param aClass The enum class
   * @param sName The name to search
   * @param aDefault The default value to be returned, if the name was not found.
   * @return The default parameter if no enum item with the given name is present.
   */
  @Nullable
  public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameOrDefault(
      @Nonnull final Class<ENUMTYPE> aClass,
      @Nullable final String sName,
      @Nullable final ENUMTYPE aDefault) {
    ValueEnforcer.notNull(aClass, "Class");

    if (StringHelper.hasText(sName))
      for (final ENUMTYPE aElement : aClass.getEnumConstants())
        if (aElement.getName().equals(sName)) return aElement;
    return aDefault;
  }
Esempio n. 3
0
  /**
   * Get the enum value with the passed ID
   *
   * @param <KEYTYPE> The ID type
   * @param <ENUMTYPE> The enum type
   * @param aClass The enum class
   * @param aID 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 <KEYTYPE, ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>>
      ENUMTYPE getFromIDOrDefault(
          @Nonnull final Class<ENUMTYPE> aClass,
          @Nullable final KEYTYPE aID,
          @Nullable final ENUMTYPE aDefault) {
    ValueEnforcer.notNull(aClass, "Class");

    if (aID != null)
      for (final ENUMTYPE aElement : aClass.getEnumConstants())
        if (aElement.getID().equals(aID)) return aElement;
    return aDefault;
  }
Esempio n. 4
0
  /**
   * Get the enum value with the passed string ID case insensitive
   *
   * @param <ENUMTYPE> The enum type
   * @param aClass The enum class
   * @param sID 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> & IHasID<String>>
      ENUMTYPE getFromIDCaseInsensitiveOrDefault(
          @Nonnull final Class<ENUMTYPE> aClass,
          @Nullable final String sID,
          @Nullable final ENUMTYPE aDefault) {
    ValueEnforcer.notNull(aClass, "Class");

    if (sID != null)
      for (final ENUMTYPE aElement : aClass.getEnumConstants())
        if (aElement.getID().equalsIgnoreCase(sID)) return aElement;
    return aDefault;
  }