⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataholder.java

📁 利用java反射机制
💻 JAVA
字号:
package org.julp;    /**     * This object holds original values from database.     * The values are using to build UPDATE and WHERE clauses      */public class DataHolder implements java.io.Serializable, Cloneable {        public DataHolder() {}        public DataHolder(int fieldCount) {        this.fieldCount = fieldCount;        data = new Object[fieldCount];    }        protected int fieldCount = 0;    protected Object[] data = null;    protected String[] fieldName = null;    protected boolean ignoreWrongFieldName = false;        public int getFieldCount() {        return fieldCount;    }        public void setFieldCount(int fieldCount) {        this.fieldCount = fieldCount;    }        public void setObject(int fieldIndex, Object value) {        this.data[fieldIndex - 1] = value;    }        public Object getObject(int fieldIndex) {        return this.data[fieldIndex - 1];    }        public void setFieldNameAndValue(int fieldIndex, String name, Object value) {        this.data[fieldIndex - 1] = value;        if (this.fieldName == null){            this.fieldName = new String[this.fieldCount];        }        this.fieldName[fieldIndex - 1] = name;    }        public void setObject(String name, Object value) {        int fieldIndex = findFieldIndex(name);        if (fieldIndex < 0){            if (this.fieldName == null){                if (!ignoreWrongFieldName){                    throw new IllegalArgumentException("Field Name not set");                }                System.out.println("Field Name not set");            }else{                if (!ignoreWrongFieldName){                    throw new IllegalArgumentException("No such field: " + name);                }                System.out.println("No such field: " + name);            }        }        this.data[fieldIndex - 1] = value;    }        public Object getObject(String name) {        if (name == null){            return null;        }        int fieldIndex = findFieldIndex(name);        if (fieldIndex < 0){            if (this.fieldName == null){                if (!ignoreWrongFieldName){                    throw new IllegalArgumentException("Field Name not set");                                    }                return null;            }else{                if (!ignoreWrongFieldName){                    throw new IllegalArgumentException("No such field: " + name);                }                System.out.println("No such field: " + name);                return null;            }        }        return this.data[fieldIndex - 1];    }        public void setFieldName(int fieldIndex, String name) {        if (this.fieldName == null){            this.fieldName = new String[this.fieldCount];        }        this.fieldName[fieldIndex - 1] = name;    }        public String getFieldName(int fieldIndex) {        if (this.fieldName == null || this.fieldName.length == 0){            return null;        }        return this.fieldName[fieldIndex - 1];    }        public int findFieldIndex(String name) {        int fieldIndex = -1;        if (this.fieldName == null || name == null){            return fieldIndex;        }        for (int i = 0;i < this.fieldCount;i++){            if (fieldName[i] == null) {                continue;            }            if(fieldName[i].equals(name)){                fieldIndex = i + 1;                break;            }        }        return fieldIndex;    }        public String toString(){        StringBuffer sb = new StringBuffer();        if (this.fieldName != null && this.fieldName.length == fieldCount){            for (int i = 0;i < fieldCount;i++){                sb.append(this.fieldName[i]).append("=");                if (this.data[i] == null){                }else{                    sb.append(this.data[i]);                }                sb.append("&");            }        }else{            for (int i = 0;i < fieldCount;i++){                if (this.data[i] == null){                    sb.append("null");                }else{                    sb.append(this.data[i]);                }                sb.append(",");            }        }        sb.deleteCharAt(sb.length() - 1);        return sb.toString();    }        public java.lang.Object[] getData() {        return this.data;    }        public void setData(java.lang.Object[] data) {        this.data = data;    }        /** Getter for property ignoreWrongFieldName.     * @return Value of property ignoreWrongFieldName.     *     */    public boolean isIgnoreWrongFieldName() {        return ignoreWrongFieldName;    }        /** Setter for property ignoreWrongFieldName.     * @param ignoreWrongFieldName New value of property ignoreWrongFieldName.     *     */    public void setIgnoreWrongFieldName(boolean ignoreWrongFieldName) {        this.ignoreWrongFieldName = ignoreWrongFieldName;    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -