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

📄 dictionary.java

📁 自己封装的数据字典,可以配置要查询的表和字段快速得到结果
💻 JAVA
字号:
package com.gisinfo.Dictionary;

import java.util.*;

/**
  * Author: Jimmy lin
 * Date: Nov 10, 2003
 * Time: 9:55:39 PM
 */
/**
 * Dictionary is a base class ,which  contains all data discribe a table in database
 * such as chinese name of fields or tablenames,In addtional,  suppling abundant
 * easy method.
 * Note: Dictionary will work with a file calling dictionary.propertis, Commonly, it locates
 *          in WEB-INFO/classes.
 */

public class Dictionary {
    protected Hashtable dataSet = null;
    protected DictionaryCreater creater = null;
    //if dictionarykey is null, mean that dictionary is a mutiple dictionary
    //otherwise it is a single dictionayKey
    protected DictionaryKey dictionaryKey = null;

    /**
     *  Dictionary constructor
     * @param creater              a creater to create data set of dictionary
     * @param dictionaryKey     indicate dictionary data set range,
     *                                            creater only create those data belong to it.
     */
    public Dictionary(DictionaryCreater creater, DictionaryKey dictionaryKey) {
        this.creater = creater;
        this.dictionaryKey = dictionaryKey;
    }
     /**
      * Dictionary constructor
      * @param creater
      */
    public Dictionary(DictionaryCreater creater) {
        this.creater = creater;
    }
    /**
     * Create a dictionary once.
     */
    public void createDictionary() {
        if (this.dataSet != null)
            return;
        reCreateDictonary();
    }
    /**
     *  Recreate a new dictionary no matter dictionary is already created.
     */
    public void reCreateDictonary() {
        if (this.dictionaryKey == null) {//indicate to create global dictioanry
            this.creater.createDictionary(this);
        } else {
            this.creater.createDictionaryByKey(this, this.dictionaryKey);
        }
    }

    /**
     * Get dataset of dictionary.
     * @return
     */
    public Hashtable getDataSet() {
        return dataSet;
    }
   /**
    * Set dataset of dictionary
    * @param dataSet
    */
    public void setDataSet(Hashtable dataSet) {
        this.dataSet = dataSet;
    }


    /**
     * Get all fields of same table
     * @param key        key must be instance of  DictionaryKey ,not allow DictionaryFieldKey
     * @return all fields of same table
     */
    public Vector search(DictionaryKey key) {
        if (dataSet == null){
            try{
                throw new Exception("\nDictionary is not been initialized!\n");
            }catch(Exception e){
             e.printStackTrace() ;
            }
            return null;
        }

        return dataSet.get(key) == null ? null : (Vector)dataSet.get(key);
    }
    /**
     * Get all fields of same table .Considering invisible field.
    * @param key        key must be instance of  DictionaryKey ,not allow DictionaryFieldKey
     * @param ignoreInvisible   ture only retrun visible fields. otherwise all fields
     * @return all fields of same table
     */
    public Vector search(DictionaryKey key, boolean ignoreInvisible){
          Vector tmpVector  =  search(key);
          if (!ignoreInvisible) return tmpVector;

          //consider ignore in visible field.
          tmpVector = (Vector) tmpVector.clone() ;
          for (int i=0; i < tmpVector.size() ; i ++){
              DictionaryField dictionaryField = (DictionaryField)tmpVector.get(i);
              if (!dictionaryField.isVisible() ) tmpVector.remove(dictionaryField);
          }
         return tmpVector;
    }
    /**
     * Get first found field in dictionary.
     * @param key
     * @return  dictionaryfield
     */
    public DictionaryField search(DictionaryFieldKey key) {
        DictionaryKey dk = new DictionaryKey();
        dk.setTableName(key.getTableName());
        Vector allField = search(dk);
        if (allField == null){
             return null;
        }
        for (int i = 0; i < allField.size(); i++) {
            DictionaryField df = (DictionaryField) allField.elementAt(i);
            if (key.equals(df.getKey())) {
                return df;
            }
        }
        System.out.println("Cannot find this field! \n");
        key.displayKey();
        return null;
    }
       /**
        * Get first found field in dictionary.
        * @param key
        * @param ignoreInvisible
        * @return dictionary field
        */
    public DictionaryField search(DictionaryFieldKey key,boolean ignoreInvisible) {
        DictionaryKey dk = new DictionaryKey();
        dk.setTableName(key.getTableName());
        Vector allField =  search(dk, ignoreInvisible);
        if (allField == null){
             return null;
        }
        for (int i = 0; i < allField.size(); i++) {
            DictionaryField df = (DictionaryField) allField.elementAt(i);
            if (key.equals(df.getKey())) {
                return df;
            }
        }
        System.out.println("Cannot find this field! \n");
        key.displayKey();
        return null;
    }
    /**
     *  Union two dictionarys
     * @param dictionary
     */
    public void union(Dictionary dictionary) {
       Hashtable tmpDataSet = dictionary.getDataSet();
        Enumeration keys = tmpDataSet.keys();
        while(keys .hasMoreElements()){
            Object aKey = keys.nextElement();
            if (!dataSet.containsKey(aKey )){
                   dataSet.put(aKey,tmpDataSet.get(aKey) );
              }else{
                   Vector tmpValues = (Vector)dataSet.get(aKey);
                   Vector tmpValues2 = (Vector)tmpDataSet.get(aKey);
                   for (int i = 0; i < tmpValues2.size() ; i++){
                          if (! tmpValues.contains(tmpValues2.get(i) ) )
                            tmpValues.add(tmpValues2.get(i) );
                   }
              }
       }

    }


    /**
     * get sub dictionary according to key,
     * sub dictionary only represent all fields with same table which specified by key object
     * @param key  key object
     * @return a new dictionary.
     */
    public Dictionary getSubDictionaryByKey(DictionaryKey key) {
        Dictionary d = new Dictionary(this.creater, key);
        DictionaryKey dk = key;
        if (key instanceof DictionaryFieldKey){
            dk = new DictionaryKey();
            dk.setTableName(key.getTableName());
        }
        Vector result = search(dk);
        if (result == null){
            result = new Vector();
        }
        Hashtable ht = new Hashtable();
        ht.put(dk,result);
        d.setDataSet(ht);
        return d;
    }

   /**
    * Get dictionary key of dictionary
    * @return
    */
    public DictionaryKey getDictionaryKey() {
        return dictionaryKey;
    }
    /**
     * Sort dictionary by display order
     */
    public void dictionarySorting(){
        Iterator values = this.dataSet .values().iterator() ;
        while(values.hasNext()){
            Vector singleDicFields = (Vector)values.next() ;
            Collections.sort(singleDicFields);
        }
    }
    /**
     *  Ignore all invisble fields
     *   @deprecated  This method remove all ignored fields,
     *                              Thus calling search method will return no result
     */
    public void ignoreInvisibleFields(){
           Iterator values = this.dataSet .values().iterator() ;
         while(values.hasNext()){
            Vector singleDicFields = (Vector)values.next() ;
            for(int i = 0; i < singleDicFields.size(); ){
                DictionaryField dictionaryField = (DictionaryField)singleDicFields.get(i);
                if (!dictionaryField.isVisible()){
                      singleDicFields.remove(dictionaryField);
                  }else{
                      i++;
                }
            }
        }
    }
}

⌨️ 快捷键说明

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