tags:

views:

61

answers:

1

I am not sure if the following is either sufficiently complicated or excessively complicated.

It basically boils down to an interrelated class and interface. (See below) Suggestions appreciated....

/**
 * @param <T> item type
 * @param <TF> table format type
 */
interface EnumTableFormatItem<T, TF extends TableFormat<T>> {
    Object getValue(TF context, T item);
    String getLabel();
}

/**
 * @param <T> item type
 * @param <E> enum type which specifies each column
 */
static public class EnumTableFormat
    <
       T, 
       E extends Enum<E> 
                & EnumTableFormatItem<T, EnumTableFormat<T, E>>
    > implements TableFormat<T>
{
    final private List<E> fields = new ArrayList<E>();

    public EnumTableFormat(List<E> fieldList) { this.fields.addAll(fieldList); }

    @Override public int getColumnCount() { return this.fields.size(); }
    @Override public String getColumnName(int column) {
        return this.fields.get(column).getLabel();
    }                       
    @Override public Object getColumnValue(T item, int column) {
        return this.fields.get(column).getValue(this, item);
    }
}

edit: On a quick reflection, it seems excessively complicated, I just wanted to use an object for context & I think I shouldn't have coupled it to the EnumTableFormat.

/**
 * @param <T> item type
 * @param <C> context object type
 */
public interface EnumTableFormatItem<T, C> {
    Object getValue(C context, T item);
    String getLabel();
}

/**
 * @param <T> item type
 * @param <C> context object type
 * @param <E> enum type which specifies each column
 */
static public class EnumTableFormat
    <
       T, C, 
       E extends Enum<E> 
                & EnumTableFormatItem<T, C>
    > implements TableFormat<T>
{
    final private List<E> fields = new ArrayList<E>();
    final private C context;

    public EnumTableFormat(C context, List<E> fieldList) { 
        this.fields.addAll(fieldList);
        this.context = context;
    }

    @Override public int getColumnCount() { return this.fields.size(); }
    @Override public String getColumnName(int column) {
        return this.fields.get(column).getLabel();
    }                       
    @Override public Object getColumnValue(T item, int column) {
        return this.fields.get(column).getValue(this.context, item);
    }
}
+1  A: 

Would something like this work?

/**
 * @param <T> item type
 */
interface EnumTableFormatItem<T> {
    Object getValue(TableFormat<T, ?> context, T item);
    String getLabel();
}

/**
 * @param <T> item type
 * @param <E> enum type which specifies each column
 */
static public class EnumTableFormat<T, E extends Enum<E> & EnumTableFormatItem<T>>
        implements TableFormat<T>
{
    final private List<E> fields = new ArrayList<E>();

    public EnumTableFormat(List<? extends E> fieldList) {
        this.fields.addAll(fieldList);
    }

    @Override public int getColumnCount() {
        return this.fields.size();
    }

    @Override public String getColumnName(int column) {
        return this.fields.get(column).getLabel();
    }

    @Override public Object getColumnValue(T item, int column) {
        return this.fields.get(column).getValue(this, item);
    }
}
ILMTitan
+1 for your `List<? extends E>` in the constructor. Otherwise I like separating out my context object.
Jason S