01: /*
02: * Copyright 2004 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: RequestIdentifier.java,v 1.3 2004/01/18 03:01:06 jackknifebarber Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import java.util.Arrays;
14:
15: class RequestIdentifier {
16: private final ClassBaseTable table;
17: private final int[] fields;
18: private final Type type;
19: private final int hashCode;
20:
21: public RequestIdentifier(ClassBaseTable table, int[] fields,
22: Type type) {
23: this .table = table;
24: this .type = type;
25:
26: if (fields == null)
27: this .fields = null;
28: else {
29: this .fields = new int[fields.length];
30: System.arraycopy(fields, 0, this .fields, 0, fields.length);
31:
32: // The key uniqueness is dependent on fields being sorted
33: Arrays.sort(this .fields);
34: }
35:
36: /*
37: * Since we are an immutable object, pre-compute the hash code for
38: * improved performance in equals().
39: */
40: int h = table.hashCode() ^ type.hashCode();
41:
42: if (this .fields != null) {
43: for (int i = 0; i < this .fields.length; ++i)
44: h ^= this .fields[i];
45: }
46:
47: hashCode = h;
48: }
49:
50: public int hashCode() {
51: return hashCode;
52: }
53:
54: public boolean equals(Object o) {
55: if (o == this )
56: return true;
57:
58: if (!(o instanceof RequestIdentifier))
59: return false;
60:
61: RequestIdentifier ri = (RequestIdentifier) o;
62:
63: if (hashCode != ri.hashCode)
64: return false;
65:
66: return table.equals(ri.table) && type.equals(ri.type)
67: && Arrays.equals(fields, ri.fields);
68: }
69:
70: public static class Type {
71: private int typeId;
72:
73: public static final Type INSERT = new Type(0);
74: public static final Type LOOKUP = new Type(1);
75: public static final Type FETCH = new Type(2);
76: public static final Type UPDATE = new Type(3);
77: public static final Type DELETE = new Type(4);
78:
79: private static final String[] typeNames = { "Insert", "Lookup",
80: "Fetch", "Update", "Delete" };
81:
82: private Type(int typeId) {
83: this .typeId = typeId;
84: }
85:
86: public String toString() {
87: return typeNames[typeId];
88: }
89: }
90: }
|