forked from LupusNobilis/java-org.hwo
ObjectTable hinzugefügt
parent
9458dff0f0
commit
1b29de87ef
|
@ -0,0 +1,147 @@
|
||||||
|
package org.hwo;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ObjectTable {
|
||||||
|
|
||||||
|
private int width;
|
||||||
|
private List<ObjectSet> rows;
|
||||||
|
|
||||||
|
public ObjectTable(int width){
|
||||||
|
this.width = width;
|
||||||
|
this.rows = new ArrayList<ObjectTable.ObjectSet>();
|
||||||
|
}
|
||||||
|
public ObjectTable(){
|
||||||
|
this.width = 0;
|
||||||
|
this.rows = new ArrayList<ObjectTable.ObjectSet>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectTable selectColums(int[] columns){
|
||||||
|
ObjectTable ot = new ObjectTable( columns.length );
|
||||||
|
|
||||||
|
for (ObjectSet row: this.rows){
|
||||||
|
Object[] sv = new Object[ columns.length ];
|
||||||
|
|
||||||
|
for (int i=0;i<columns.length;i++){
|
||||||
|
sv[i] = row.getValues()[columns[i]];
|
||||||
|
}
|
||||||
|
ot.add(sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectSet getRow(int row){
|
||||||
|
return this.rows.get(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectSet[] getRows(){
|
||||||
|
return this.rows.toArray(new ObjectSet[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(ObjectSet row){
|
||||||
|
this.rows.add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(Object[] values){
|
||||||
|
if ((this.width == 0) && (this.rows.size()==0)){
|
||||||
|
this.width = values.length;
|
||||||
|
}
|
||||||
|
add(new ObjectSet(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(int row){
|
||||||
|
this.rows.remove(row);
|
||||||
|
}
|
||||||
|
public void remove(ObjectSet row){
|
||||||
|
this.rows.remove(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int row,Object[] values){
|
||||||
|
this.rows.get(row).set(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int row,int cell,Object value){
|
||||||
|
this.rows.get(row).set(cell, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString(int row,int cell){
|
||||||
|
return this.rows.get(row).getString(cell);
|
||||||
|
}
|
||||||
|
public int getInteger(int row,int cell){
|
||||||
|
return this.rows.get(row).getInteger(cell);
|
||||||
|
}
|
||||||
|
public float getFloat(int row,int cell){
|
||||||
|
return this.rows.get(row).getFloat(cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class ObjectSet{
|
||||||
|
|
||||||
|
private Object[] values;
|
||||||
|
|
||||||
|
public ObjectSet(){
|
||||||
|
this.values = new Object[ width ];
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectSet(Object[] values){
|
||||||
|
this.values = new Object[ width ];
|
||||||
|
set(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(Object[] values){
|
||||||
|
if (values.length > width){
|
||||||
|
throw new ArrayIndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int n=0;n<values.length;n++){
|
||||||
|
this.values[n] = values[n];
|
||||||
|
}
|
||||||
|
for (int n=values.length;n<width;n++){
|
||||||
|
this.values[n] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void set(int n,Object o){
|
||||||
|
values[n] = o;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] getValues(){
|
||||||
|
return this.values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString(int n){
|
||||||
|
return values[n].toString();
|
||||||
|
}
|
||||||
|
public int getInteger(int n){
|
||||||
|
if (values[n]==null){
|
||||||
|
return 0;
|
||||||
|
} else if (Integer.class.isInstance(values[n])){
|
||||||
|
return (Integer)values[n];
|
||||||
|
} else {
|
||||||
|
return Integer.decode(values[n].toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public float getFloat(int n){
|
||||||
|
if (values[n]==null){
|
||||||
|
return 0;
|
||||||
|
} else if (Float.class.isInstance(values[n])){
|
||||||
|
return (Float)values[n];
|
||||||
|
} else {
|
||||||
|
return Float.parseFloat(values[n].toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] selectColums(int[] columns){
|
||||||
|
Object[] sv = new Object[ columns.length ];
|
||||||
|
|
||||||
|
for (int i=0;i<columns.length;i++){
|
||||||
|
sv[i] = this.values[columns[i]];
|
||||||
|
}
|
||||||
|
return sv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue