数据bean接口
import java.io.Serializable;
import java.util.List;
/**
* 数据Bean接口
* @version $Revision: 7611 $
*/
public interface IBean extends Serializable {
/** 键:结果 */
public static final String KEY_RESULT = "$RESULT$";
/** 键:例外 */
public static final String KEY_THROWABLE = "$THROWABLE$";
/**
* 获取key对应的值,如果没有返回null
* @param key key
* @return rtnObj rtnObj
*/
public Object get(String key);
/**
* 存储Key-Value(名-值)对
* @param key key
* @param obj value
*/
public void set(String key, Object obj);
/**
* 获取所有key
* @return rtnKeys rtnKeys/null
*/
public List<String> getKeys();
/**
* 比较两个Bean中的字段值 内容
* @param ibean 被比较Bean
* @return 字段值内容是否相同
*/
public boolean compare(IBean ibean);
/**
* 比较两个Bean中的指定字段值内容
* @param iBean 被比较Bean
* @param keyCodes 指定字段
* @return 字段值内容是否相同
*/
public boolean compare(IBean iBean, String keyCodes);
/**
* 检查该BEAN中是否存在此编码
* @param key 编码
* @return 编码对应值
*/
public boolean containsKey(String key);
/**
* 是否为空
* @return true/false
*/
public boolean isEmpty();
/**
* 获取字符串型字段值
* @param key 编码字段
* @param def 缺省值
* @return 字符串值
*/
public String get(String key, String def);
/**
* 获取整型字段值
* @param key 编码字段
* @param def 缺省值
* @return 整型字段值
*/
public int get(String key, int def);
/**
* 获取双精度浮点型字段值
* @param key 编码字段
* @param def 缺省值
* @return 双精度浮点型字段值
*/
public double get(String key, double def);
/**
* 获取布尔型字段值
* @param key 编码字段
* @param def 缺省值
* @return 布尔型字段值
*/
public boolean get(String key, boolean def);
/**
* 深度克隆对象
* @return IBean 克隆对象
*/
public IBean deepClone() throws CoreException;
}
数据bean的实现类
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Bean 基于HashMap实现
*/
public class Bean implements IBean {
private static final long serialVersionUID = -3530352869815683174L;
/** 数据map */
private HashMap<String, Object> data;
/**
* 构造函数
*/
public Bean() {
this.data = new HashMap<String, Object>();
}
/**
* 有参构造函数
* @param arg 初始参数
*/
public Bean(String[][] arg) {
this.data = new HashMap<String, Object>();
for (String[] tmp : arg) {
this.data.put(tmp[0], tmp[1]);
}
}
/**
* {@inheritDoc}
* @param iBean IBean
* @return boolean
*/
public boolean compare(IBean iBean) {
boolean isEqual = true;
for (String key : this.getKeys()) {
isEqual = compare(iBean, key);
if (!isEqual) {
break;
}
}
return isEqual;
}
/**
* {@inheritDoc}
* @param iBean IBean
* @param keyCodes String
* @return boolean
*/
public boolean compare(IBean iBean, String keyCodes) {
boolean isEqual = true;
String[] keyArray = keyCodes.split(",");
for (String key : keyArray) {
if (!CommonUtils.isNullOrEmpty(key)) {
String dataVal = Cast.to(get(key), "");
if (!dataVal.equals(Cast.to(iBean.get(key), ""))) {
isEqual = false;
break;
}
}
}
return isEqual;
}
/**
* {@inheritDoc}
* @param key String
* @return boolean
*/
public boolean containsKey(String key) {
return this.data.containsKey(key);
}
/**
* {@inheritDoc}
* @return Bean
*/
public Bean deepClone() throws CoreException {
Bean rtnObj = null;
ByteArrayOutputStream byteOut = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bi = null;
try {
byteOut = new ByteArrayOutputStream();
oos = new ObjectOutputStream(byteOut);
oos.writeObject(this);
bi = new ByteArrayInputStream(byteOut.toByteArray());
ois = new ObjectInputStream(bi);
rtnObj = (Bean) ois.readObject();
} catch (Exception e) {
throw new CoreException(e.getCause());
} finally {
try {
if (ois != null) {
ois.close();
}
if (bi != null) {
bi.close();
}
if (oos != null) {
oos.close();
}
if (byteOut != null) {
byteOut.close();
}
} catch (final IOException e) {
e.printStackTrace();
}
}
return rtnObj;
}
/**
* {@inheritDoc}
* @param key String
* @return Object
*/
public Object get(String key) {
return this.data.get(key);
}
/**
* {@inheritDoc}
* @return List<String>
*/
public List<String> getKeys() {
List<String> keyList = new ArrayList<String>();
for (String key : this.data.keySet()) {
keyList.add(key);
}
return keyList;
}
/**
* {@inheritDoc}
* @return boolean
*/
public boolean isEmpty() {
return this.data.size() == 0;
}
/**
* {@inheritDoc}
* @param key String
* @param obj Object
*/
public void set(String key, Object obj) {
this.data.put(key, obj);
}
/**
* 移除元素
* @param key
*/
public void remove(String key) {
this.data.remove(key);
}
/**
* {@inheritDoc}
* @return String
*/
public String toString() {
return this.data.toString();
}
/**
* 获取字符串型字段值
* @param key 编码字段
* @param def 缺省值
* @return 字符串值
*/
public String get(String key, String def) {
return Cast.to(get(key), def);
}
/**
* 获取整型字段值
* @param key 编码字段
* @param def 缺省值
* @return 整型字段值
*/
public int get(String key, int def) {
return Cast.to(get(key), def);
}
/**
* 获取双精度浮点型字段值
* @param key 编码字段
* @param def 缺省值
* @return 双精度浮点型字段值
*/
public double get(String key, double def) {
return Cast.to(get(key), def);
}
/**
* 获取布尔型字段值
* @param key 编码字段
* @param def 缺省值
* @return 布尔型字段值
*/
public boolean get(String key, boolean def) {
return Cast.to(get(key), def);
}
public HashMap<String, Object> getData(){
return data;
}
}