KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > RequestIdentifier


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: RequestIdentifier.java,v 1.3 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.util.Arrays JavaDoc;
14
15
16 class RequestIdentifier
17 {
18     private final ClassBaseTable table;
19     private final int[] fields;
20     private final Type type;
21     private final int hashCode;
22
23
24     public RequestIdentifier(ClassBaseTable table, int[] fields, Type type)
25     {
26         this.table = table;
27         this.type = type;
28
29         if (fields == null)
30             this.fields = null;
31         else
32         {
33             this.fields = new int[fields.length];
34             System.arraycopy(fields, 0, this.fields, 0, fields.length);
35
36             // The key uniqueness is dependent on fields being sorted
37
Arrays.sort(this.fields);
38         }
39
40         /*
41          * Since we are an immutable object, pre-compute the hash code for
42          * improved performance in equals().
43          */

44         int h = table.hashCode() ^ type.hashCode();
45
46         if (this.fields != null)
47         {
48             for (int i = 0; i < this.fields.length; ++i)
49                 h ^= this.fields[i];
50         }
51
52         hashCode = h;
53     }
54
55
56     public int hashCode()
57     {
58         return hashCode;
59     }
60
61
62     public boolean equals(Object JavaDoc o)
63     {
64         if (o == this)
65             return true;
66
67         if (!(o instanceof RequestIdentifier))
68             return false;
69
70         RequestIdentifier ri = (RequestIdentifier)o;
71
72         if (hashCode != ri.hashCode)
73             return false;
74
75         return table.equals(ri.table)
76             && type.equals(ri.type)
77             && Arrays.equals(fields, ri.fields);
78     }
79
80
81     public static class Type
82     {
83         private int typeId;
84
85         public static final Type INSERT = new Type(0);
86         public static final Type LOOKUP = new Type(1);
87         public static final Type FETCH = new Type(2);
88         public static final Type UPDATE = new Type(3);
89         public static final Type DELETE = new Type(4);
90
91         private static final String JavaDoc[] typeNames =
92         {
93             "Insert",
94             "Lookup",
95             "Fetch",
96             "Update",
97             "Delete"
98         };
99
100         private Type(int typeId)
101         {
102             this.typeId = typeId;
103         }
104
105         public String JavaDoc toString()
106         {
107             return typeNames[typeId];
108         }
109     }
110 }
111
Popular Tags