Skip to content

Instantly share code, notes, and snippets.

@naosim
Created August 17, 2017 22:24
Show Gist options
  • Save naosim/93218301cf91db80f83c2c414d4ac4f3 to your computer and use it in GitHub Desktop.
Save naosim/93218301cf91db80f83c2c414d4ac4f3 to your computer and use it in GitHub Desktop.
VariousColumnTable
package com.naosim.table;
import java.util.HashMap;
import java.util.Map;
public class VariousColumnTable<ROW> {
private final Map<Class, Integer> columnIndex;
private final Map<ROW, Object[]> rows;
public VariousColumnTable(Class ...columns) {
this.rows = new HashMap<>();
this.columnIndex = new HashMap<>();
for(int i = 0; i < columns.length; i++) {
columnIndex.put(columns[i], i);
}
}
public VariousColumnTable put(ROW row, Object ...cells) {
rows.put(row, cells);
return this;
}
public <T> T get(ROW row, Class<T> column) {
Object result = rows.get(row)[columnIndex.get(column)];
return column.cast(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment