KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > parser > Key


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3   /**
4    * Key Class to get Key of NameOfRule and Position for making an
5    * entry in hashMap for falied and passed rule.
6    */

7
8 public class Key {
9
10    /**
11     * Name Of Rule for which entry is made to hashMap
12     */

13    String JavaDoc nameOfRule;
14
15    /**
16     * Parse Position for which rule is failed or passed
17     */

18    int position;
19
20    /*
21     * variable for holding hashCode for this key.
22     */

23    int hashCode;
24    String JavaDoc recursiveState;
25
26    public Key(String JavaDoc nameOfRule,int position){
27       this.nameOfRule = nameOfRule;
28       this.position = position;
29       hashCode = 1;
30    }
31
32    public Key(String JavaDoc nameOfRule,int position,String JavaDoc recursiveState){
33       this.nameOfRule = nameOfRule;
34       this.position = position;
35       hashCode = 1;
36       this.recursiveState = recursiveState;
37    }
38
39    /*
40     * Overriding hashCode of object.
41     * Used by HashMap.
42     */

43    public int hashCode() {
44       if ( hashCode != 1 )
45          return hashCode;
46       hashCode = 31*hashCode + (nameOfRule==null ? 0 : nameOfRule.hashCode());
47       hashCode += 31*hashCode + position;
48      hashCode += 31*hashCode + (recursiveState==null ? 0 : recursiveState.hashCode());
49       return hashCode;
50    }
51
52    /*
53     * Overriding Equals of object.
54     */

55    public boolean equals(Object JavaDoc object){
56       boolean flag = true;
57       if (!(object instanceof Key) )
58         return false;
59       Key key = (Key)object;
60       flag = nameOfRule == null ? true : nameOfRule.equals(key.nameOfRule);//compareTwoString(nameOfRule,key.nameOfRule);
61
flag = flag && (position == key.position ? true : false);
62       flag = flag && recursiveState == null ? true : recursiveState.equals(key.recursiveState);
63       return flag;
64    }
65
66    /**
67     * Method compares two string passed.
68     */

69    private boolean compareTwoString(String JavaDoc string1,String JavaDoc string2){
70       if ( string1.length() != string2.length() )
71          return false;
72       char []source = string1.toCharArray();
73       char []target = string2.toCharArray();
74       for(int j = 0; j < source.length; ++j)
75          if ( source[j] != target[j] )
76             return false;
77       return true;
78    }
79
80    public String JavaDoc toString(){
81       StringBuffer JavaDoc str=new StringBuffer JavaDoc("Key [");
82       str.append(nameOfRule).append("--").append(position).append("]");
83       return str.toString();
84    }
85 }
86
Popular Tags