Java generics: defining obfuscated pattern
I'm not sure if the following is complicated enough or overly complicated.
It basically comes down to an interconnected 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 quick reflection, it seems overly complicated, I just wanted to use an object for context and I think I shouldn't have bound it to an EnumTableFormat.
(see my answer)
+2
a source to share
2 answers
edit: On quick reflection seems overly complicated, I just wanted to use an object for context and I think I shouldn't have bound 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);
}
}
0
a source to share
Does 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);
}
}
+1
a source to share