Skip to content Skip to sidebar Skip to footer

API Design Regarding File Tokenization, Object Value Setting And Enums Of Token Positions

In my Android application I need to read a large amount of data from a set of .ini files that were originally (and still are) deployed with a Windows application. The application i

Solution 1:

What you are trying to do is not possible with Java enums. However it could be easily achieved in the other way:

public class IniParam<T> {

  public static final IniParam<String> NAME = new IniParam<String>(2);

  public static final IniParam<String> UNITS = new IniParam<String>(3);

  public static final IniParam<Integer> LOWER = new IniParam<Integer>(4);

  public static final IniParam<Integer> UPPER = new IniParam<Integer>(5);

  private final int position;

  private IniParam(int position) {
    this.position = position;
  }

  public int getPosition() {
    return position;
  }    
}

Tokenizer then may look like:

public class Tokenizer {

  public String get(IniParam<String> iniParam) {
    int position = iniParam.getPosition();
    //...
    return "some string from .ini";
  }

  public int get(IniParam<Integer> iniParam) {
    // ... 
    // return some integer from .ini
  }
}

Usage example:

    Tokenizer t = new Tokenizer();
    String name = t.get(IniParam.NAME);
    int lower = t.get(IniParam.LOWER);
    someObject.setName( t.get(IniParam.NAME) ).setUnits( t.get(IniParam.UNITS) ).setLowerUpper( t.get(IniParam.LOWER), t.get(IniParam.UPPER) );

 

UPDATE

Unfortunately Tokenizer class I provided above will not compile with JDK 7/Eclipse 3.6+ compilers (I can't check it myself right now but the fixes for the following Oracle and Eclipse bugs suppose compilation errors in get(...) methods). If you face this issue here is the workaround:

public class IniParam<T> {

  public static final IniParam<String> NAME = new IniParam<String>(2, String.class);

  public static final IniParam<String> UNITS = new IniParam<String>(3, String.class);

  public static final IniParam<Integer> LOWER = new IniParam<Integer>(4, Integer.class);

  public static final IniParam<Integer> UPPER = new IniParam<Integer>(5, Integer.class);

  private final int position;

  private final Class<? extends T> type;

  private IniParam(int position, Class<? extends T> type) {
    this.position = position;
    this.type = type;
  }

  public int getPosition() {
    return position;
  }

  public Class<? extends T> getType() {
    return type;
  }
}


public class Tokenizer {

  public <T> T get(IniParam<T> iniParam) {
    int position = iniParam.getPosition();
    Class<? extends T> type = iniParam.getType();
    if (type == String.class) {
      //...
      return type.cast("some string from .ini");        
    } else if (type == Integer.class) {
      //...
      // Integer result = ...;
      return type.cast(result);
    } else {
      throw new IllegalArgumentException("Unexpected IniParam type: " + type);
    }      
  }
}

Post a Comment for "API Design Regarding File Tokenization, Object Value Setting And Enums Of Token Positions"