Value List 程式庫
piValueList 程式庫
package com.PowerIntegral;
import java.util.*;
import java.io.Serializable;
/**
* <p>
* Title: piValueList
* </p>
*
* <p>
* Description: piValueList
* </p>
*/
public class piValueList {
private Vector _ValueList = new Vector();
private String _ErrorMessage = "";
/**
* piValueList: 建構子
*/
public piValueList() {
}
/**
* Length: 元素數
*
* @return int
*/
public int Length() {
return this._ValueList.size();
}
/**
* Add: 新增一列
*
* @return Hashtable
*/
public Hashtable Add() {
Hashtable _ht = new Hashtable();
this._ValueList.add(_ht);
return _ht;
}
/**
* Load2: 載入 (使用 String.split)
*
* @param sourceString
* String => 來源字串
* @param deliRec
* String => 列間格字串
* @param deliField
* String => 欄間格字串
* @return boolean
*/
public boolean Load2(String sourceString, String deliRec, String deliField) {
int _ipos, _icount;
if (sourceString == null) {
return false;
}
if (sourceString.trim().equals("")) {
return false;
}
_ValueList.clear();
boolean _OK = false;
try {
String[] _sa = sourceString.split(deliRec);
for (_ipos = 0, _icount = _sa.length-1; _ipos < _icount; _ipos++) {
Hashtable _ht = this.Add();
String _tmp = _sa[_ipos];
if (_tmp == null) {
_OK = false;
break;
}
String[] _sb = _tmp.split(deliField);
if (_sb == null) {
_OK = false;
break;
}
int _jpos, _jcount;
for (_jpos = 0, _jcount = _sb.length-1; _jpos < _jcount; _jpos++) {
String _tmp2 = _sb[_jpos];
if (_tmp2 == null) {
_OK = false;
break;
}
int _Pos = _tmp2.indexOf("=");
String[] _sd = { _tmp2.substring(0, _Pos),
_tmp2.substring(_Pos + 1) };
if (!this.setField(_ht, _sd[0].trim(), _sd[1].trim())) {
_OK = false;
break;
}
_OK = true;
}
}
} catch (Exception ex) {
this._ErrorMessage = ex.getMessage();
}
return _OK;
}
/**
* Load: 載入 (使用 StringTokenizer)
*
* @param sourceString
* String => 來源字串
* @param deliRec
* String => 列間格字串
* @param deliField
* String => 欄間格字串
* @return boolean
*/
public boolean Load(String sourceString, String deliRec, String deliField) {
int _ipos, _icount;
if (sourceString == null) {
return false;
}
if (sourceString.trim().equals("")) {
return false;
}
_ValueList.clear();
boolean _OK = false;
try {
StringTokenizer _sa = new StringTokenizer(sourceString, deliRec);
for (_ipos = 0, _icount = _sa.countTokens(); _ipos < _icount; _ipos++) {
Hashtable _ht = this.Add();
String _tmp = _sa.nextToken();
if (_tmp == null) {
_OK = false;
break;
}
StringTokenizer _sb = new StringTokenizer(_tmp, deliField);
if (_sb == null) {
_OK = false;
break;
}
int _jpos, _jcount;
for (_jpos = 0, _jcount = _sb.countTokens(); _jpos < _jcount; _jpos++) {
String _tmp2 = _sb.nextToken();
if (_tmp2 == null) {
_OK = false;
break;
}
int _Pos = _tmp2.indexOf("=");
String[] _sd = { _tmp2.substring(0, _Pos),
_tmp2.substring(_Pos + 1) };
if (!this.setField(_ht, _sd[0].trim(), _sd[1].trim())) {
_OK = false;
break;
}
_OK = true;
}
}
} catch (Exception ex) {
this._ErrorMessage = ex.getMessage();
}
return _OK;
}
/**
* Save: 取得結果字串
*
* @param deliRec
* String => 列間格字串
* @param deliField
* String => 欄間格字串
* @return String
*/
public String Save(String deliRec, String deliField) {
StringBuffer _sb = new StringBuffer("");
int _ipos;
for (_ipos = 0; _ipos < this._ValueList.size(); _ipos++) {
Hashtable _ht = (Hashtable) _ValueList.get(_ipos);
int _jpos = 0, _jcount = _ht.size();
for (Enumeration e = _ht.keys(); e.hasMoreElements();) {
String _HashKey = (String) e.nextElement();
String _HashValue = (String) _ht.get(_HashKey);
_sb.append(_HashKey.trim() + "=" + _HashValue.trim());
if (_jpos++ < _jcount - 1) {
_sb.append(deliField);
}
}
if (_ipos < this._ValueList.size() - 1) {
_sb.append(deliRec);
}
}
return _sb.toString();
}
/**
* setField
*
* @param ht
* Hashtable
* @param KeyName
* String
* @param KeyValue
* Object
* @return boolean
*/
public boolean setField(Hashtable ht, String KeyName, Serializable KeyValue) {
if (ht == null) {
return false;
}
ht.put(KeyName, KeyValue);
return true;
}
/**
* setField: 加入元素
*
* @param RecNum
* int => 列號
* @param KeyName
* String => 索引
* @param KeyValue
* Serializable => 物件
* @return boolean
*/
public boolean setField(int RecNum, String KeyName, Serializable KeyValue) {
if (RecNum >= this._ValueList.size()) {
return false;
}
Hashtable _ht = (Hashtable) _ValueList.get(RecNum);
if (_ht == null) {
return false;
}
_ht.put(KeyName, KeyValue);
return true;
}
/**
* getField: 取得元素
*
* @param sourceContainer
* Hashtable => 來源物件容器
* @param KeyName
* String => 索引
* @return Serializable
*/
public Serializable getField(Hashtable sourceContainer, String KeyName) {
Serializable _KeyValue = null;
if (sourceContainer == null) {
return _KeyValue;
}
_KeyValue = (Serializable) sourceContainer.get(KeyName);
return _KeyValue;
}
/**
* getField: 取得元素
*
* @param RecNum
* int => 列號
* @param KeyName
* String => 索引
* @return Serializable
*/
public Serializable getField(int RecNum, String KeyName) {
Serializable _KeyValue = null;
if (RecNum >= this._ValueList.size()) {
return _KeyValue;
}
Hashtable _ht = (Hashtable) _ValueList.get(RecNum);
if (_ht == null) {
return _KeyValue;
}
_KeyValue = (Serializable) _ht.get(KeyName);
return _KeyValue;
}
/**
* getErrorMessage: 最近執行錯誤訊息
*
* @return String
*/
public String getErrorMessage() {
return this._ErrorMessage;
}
}