commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <>
Subject [lang] Formats for enums
Date Thu, 24 Apr 2003 14:14:37 GMT

I'm working on an application that uses a lot of type-safe enumerations
and must be internationalized.  One thing I think is missing from the lang
package and could profitably be added to it is a set of Formats for Enums.
It would certainly help me, and I would be willing to work on it.
People might want it in a separate project, though.

For example:

package org.fide.chess;  // No, I do not work for FIDE.
import org.apache.commons.lang.enum.Enum;

public final class Piece extends Enum {
  public final static Piece KING   = new Piece("King");
  public final static Piece QUEEN  = new Piece("Queen");
  public final static Piece ROOK   = new Piece("Rook");
  public final static Piece BISHOP = new Piece("Bishop");
  public final static Piece KNIGHT = new Piece("Knight");
  public final static Piece PAWN   = new Piece("Pawn");

  private Piece(String piece) {

  public static Piece getEnum(String piece) {
    return (Piece) getEnum(Piece.class, piece);
  public static Map getEnumMap() {
    return getEnumMap(Piece.class);
  public static List getEnumList() {
    return getEnumList(Piece.class);
  public static Iterator iterator() {
    return iterator(Piece.class);

Now, for some applications I would want a format that would take a Piece
and output an internationalized version of the name:

  ChessFormat tf = new ChessFormat();
  String s = tf .format(Piece.KNIGHT);
  // In Locale.US, s is "knight".
  // In Locale.DE, s is "Springer".

For other applications, I might want the abbreviation.

  ChessFormat af= new ChessFormat(ChessFormat.SHORT);
  String s = af.format(Piece.KNIGHT);
  // s is "N".

And I might want figurine algebraic, although there are separate Unicode 
characters for white and black:

  ChessFormat ff= new ChessFormat(ChessFormat.FIGURINE);
  String s = ff.format(Piece.KNIGHT);
  // s is "\u2658" (or "\u265E").

I have some ideas for doing this; they involve finding a set of properties 
files alongside the enum class.  But my ideas are still fuzzy, and I'd like 
to know if I'm on the right track, or even if this is the right place to ask.

I have some similar ideas for MessageFormat, but that should come later.

Eric Jablow


View raw message