KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > database > general > PrimaryKey


1 package com.daffodilwoods.database.general;
2
3 import java.io.ObjectInput JavaDoc;
4 import com.daffodilwoods.database.resource.*;
5 import java.io.ObjectOutput JavaDoc;
6 import java.io.IOException JavaDoc;
7 import com.daffodilwoods.database.utility.P;
8 import java.util.*;
9 public class PrimaryKey implements java.io.Externalizable JavaDoc{
10
11   private Object JavaDoc[] values;
12   private int[] indexes;
13
14   public PrimaryKey() throws DException {
15   }
16
17   public PrimaryKey(Object JavaDoc[] values) throws DException {
18     this.values = values;
19     indexes = new int[values.length];
20     for (int i = 0; i < values.length; i++)
21       indexes[i] = i;
22   }
23   public Object JavaDoc[] getValues(){
24     return values;
25   }
26   public void setValues(Object JavaDoc[] values) throws DException {
27     this.values = values;
28     indexes = new int[values.length];
29     for (int i = 0; i < values.length; i++)
30       indexes[i] = i;
31   }
32
33   public void setIndexes(int[] indexes){
34       this.indexes = indexes;
35   }
36
37   public int[] getIndexes(){
38       return indexes;
39   }
40
41   public boolean equals(Object JavaDoc object){
42     if(!(object instanceof PrimaryKey) || object == null)
43        return false;
44     PrimaryKey pk = (PrimaryKey)object;
45     Object JavaDoc[] pkValues = pk.getValues();
46     int len1 = indexes.length;
47     int len2 = pk.getIndexes().length;
48     int len = len1 < len2 ? len1 : len2;
49     for(int i =0;i< len; i++){
50       if(pkValues[indexes[i]] == null && values[indexes[i]] == null)
51           continue;
52       if(values[indexes[i]] == null)
53         return false;
54       if(pkValues[indexes[i]] == null)
55         return false;
56       if(! pkValues[indexes[i]].equals(values[indexes[i]]))
57         return false;
58     }
59     return true;
60   }
61
62   public void readExternal(ObjectInput JavaDoc objectInput) {
63     try {
64         values = (Object JavaDoc[])objectInput.readObject();
65         indexes = (int[])objectInput.readObject();
66     }catch(IOException JavaDoc ioe) {
67     }catch(ClassNotFoundException JavaDoc cnf) {
68     }
69   }
70
71   public void writeExternal(ObjectOutput JavaDoc objectOutput) {
72     try {
73         objectOutput.writeObject(values);
74         objectOutput.writeObject(indexes);
75     }catch(IOException JavaDoc ioe) {
76     }
77   }
78
79   public String JavaDoc toString(){
80      return java.util.Arrays.asList(values).toString();
81   }
82
83    public Object JavaDoc[] getObject(){
84       int length = values.length;
85       ArrayList result = new ArrayList(5);
86       for(int i = 0 ; i < length ; i++){
87          if(values[i] instanceof PrimaryKey)
88             result.addAll(Arrays.asList(((PrimaryKey)values[i]).getObject()));
89          else
90             result.add(values[i]);
91       }
92       return result.toArray(new Object JavaDoc[0]);
93    }
94 }
95
Popular Tags