GWT Custom Seriallizable HashTable

Untuk memudahkan data exchange antara server & client serta memudahkan client mencapai data, aku create satu seriallizable class ala hashtable. Pengguna boleh panggil data guna index atau key. Senang bukan ?

Anyway seperti, code dia ada kat bawah ni.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.com.client;

import com.gwtext.client.data.BooleanFieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author Peyo
*/
public class MyHashTable implements Serializable {

private ArrayList alValue = new ArrayList();
private ArrayList alKeys = new ArrayList();

public MyHashTable() {
}

public MyHashTable(List list, Object[] keys) {
for (int i = 0; i < list.size(); i++) {
for (int k = 0; k < keys.length; k++) {
add(keys[k], list.get(k));
}
}
}

public MyHashTable(Record record, RecordDef recordDef) {
alValue.clear();
alKeys.clear();
for (int i = 0; i < recordDef.getFields().length; i++) {
String fieldname = recordDef.getFields()[i].getName();
if (recordDef.getFields()[i].getClass().equals(StringFieldDef.class)) {
String value = record.getAsString(fieldname);
add(fieldname, value);
} else if (recordDef.getFields()[i].getClass().equals(IntegerFieldDef.class)) {
Integer value = record.getAsInteger(fieldname);
add(fieldname, value);
} else if (recordDef.getFields()[i].getClass().equals(FloatFieldDef.class)) {
Float value = record.getAsFloat(fieldname);
add(fieldname, value);
} else if (recordDef.getFields()[i].getClass().equals(BooleanFieldDef.class)) {
Object value = record.getAsBoolean(fieldname);
} else {
Object value = record.getAsObject(fieldname);
add(fieldname, value);
}
}
}

public final ArrayList getKeys() {
return alKeys;
}

public Object getValue(
Object key) {
Integer index = checkKeyExist(key);
if (index > -1) {
return alValue.get(index);
}

return null;
}

public Object getValue(Integer index) {
return alValue.get(index);
}

public Integer getCount() {
return Integer.valueOf(alKeys.size());
}

public void add(Object key, Object object) {
alValue.add(object);
alKeys.add(key);
}

public void updateItem(Object key, Object value) {
int index = checkKeyExist(key);
if (index > -1) {
alValue.set(index, value);
}
}

public void deleteItem(Object key) {
int index = checkKeyExist(key);
if (index > -1) {
alValue.remove(index);
alKeys.remove(index);
}
}



private Integer checkKeyExist(Object key) {
for (int i = 0; i <
alKeys.size(); i++) {
if (alKeys.get(i).equals(key)) {
return Integer.valueOf(i);
}

}
return Integer.valueOf(-1);
}

public Object[] toArray() {
return alValue.toArray();
}

public Record toRecord(
RecordDef recordDef) {
Object[] obj = new Object[recordDef.getFields().length];
for (int i = 0; i <
recordDef.getFields().length; i++) {
obj[i] = getValue(recordDef.getFields()[i].getName());
}

Record record = recordDef.createRecord(obj);
return record;
}
}

0 comments:

 
Copyright © peyotest