All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.mmm.util.nls.AbstractResourceBundle Maven / Gradle / Ivy

The newest version!
/* $Id: AbstractResourceBundle.java 402 2008-01-14 21:09:00Z hohwille $
 * Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.nls;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * This is the abstract base class for {@link ResourceBundle} implementations
 * using this NLS support. Create your {@link ResourceBundle}s by sub-classing
 * this class and simply define some public static final fields that will be
 * automatically added to the bundle using reflection (only from constructor).
 * Please note that your sub-class must also be public or you need to set
 * privileges in the security manager to allow this class reading the fields via
 * reflection.
 * 
 * @author Joerg Hohwiller (hohwille at users.sourceforge.net)
 */
public abstract class AbstractResourceBundle extends ResourceBundle {

  /**
   * The key value pairs; maps keys (String) to values (Object). No Map because
   * Enumeration is required...
   */
  private Hashtable bundle;

  /** the inverse map of {@link #bundle} */
  private Map reverse;

  /**
   * The constructor.
   */
  public AbstractResourceBundle() {

    super();
    try {
      Field[] fields = getClass().getFields();
      this.bundle = new Hashtable(fields.length);
      this.reverse = new HashMap(fields.length);
      for (int i = 0; i < fields.length; i++) {
        int modifiers = fields[i].getModifiers();
        if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
            && !Modifier.isPrivate(modifiers)) {
          if (fields[i].getType() == String.class) {
            String key = fields[i].getName();
            Object value = fields[i].get(null);
            this.bundle.put(key, value);
            this.reverse.put(value, key);
          }
        }
      }
    } catch (Exception e) {
      throw new IllegalStateException("Failed to initialize " + getClass().getName(), e);
    }
  }

  /**
   * This method is the inverse of {@link #getObject(String)}.
   * 
   * @param object is the object (potentially) retrieved via
   *        {@link #getObject(String)}.
   * @return the key for the given object or null
   *         if it was NOT retrieved via {@link #getObject(String)} from this
   *         instance.
   */
  public String getKey(Object object) {

    return this.reverse.get(object);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Enumeration getKeys() {

    return this.bundle.keys();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected Object handleGetObject(String key) {

    return this.bundle.get(key);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy