001: /*
002: * Derby - class org.apache.derby.impl.drda.Pkgnamcsn
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
016: * implied. See the License for the specific language governing
017: * permissions and limitations under the License.
018: *
019: */
020:
021: package org.apache.derby.impl.drda;
022:
023: /**
024: * Class representing a PKGNAMCSN object (RDB Package Name,
025: * Consistency Token, and Section Number).
026: */
027: final class Pkgnamcsn {
028: /** Database name. */
029: private final String rdbnam;
030: /** RDB Package Collection Identifier. */
031: private final String rdbcolid;
032: /** RDB Package Identifier. */
033: private final String pkgid;
034: /** RDB Package Section Number. */
035: private final int pkgsn;
036: /** RDB Package Consistency Token. */
037: private final ConsistencyToken pkgcnstkn;
038:
039: /** Object which can be used for hashing when the consistency
040: * token can be ignored. */
041: private Object statementKey = null;
042:
043: /**
044: * Create a new <code>Pkgnamcsn</code> instance.
045: *
046: * @param rdbnam database name
047: * @param rdbcolid RDB Package Collection Identifier
048: * @param pkgid RDB Package Identifier
049: * @param pkgsn RDB Package Section Number
050: * @param pkgcnstkn RDB Package Consistency Token
051: */
052: Pkgnamcsn(String rdbnam, String rdbcolid, String pkgid, int pkgsn,
053: ConsistencyToken pkgcnstkn) {
054: this .rdbnam = rdbnam;
055: this .rdbcolid = rdbcolid;
056: this .pkgid = pkgid;
057: this .pkgsn = pkgsn;
058: this .pkgcnstkn = pkgcnstkn;
059: }
060:
061: /**
062: * Get RDBNAM.
063: *
064: * @return database name
065: */
066: public String getRdbnam() {
067: return rdbnam;
068: }
069:
070: /**
071: * Get RDBCOLID.
072: *
073: * @return RDB Package Collection Identifier
074: */
075: public String getRdbcolid() {
076: return rdbcolid;
077: }
078:
079: /**
080: * Get PKGID.
081: *
082: * @return RDB Package Identifier
083: */
084: public String getPkgid() {
085: return pkgid;
086: }
087:
088: /**
089: * Get PKGSN.
090: *
091: * @return RDB Package Section Number
092: */
093: public int getPkgsn() {
094: return pkgsn;
095: }
096:
097: /**
098: * Get PKGCNSTKN.
099: *
100: * @return RDB Package Consistency Token
101: */
102: public ConsistencyToken getPkgcnstkn() {
103: return pkgcnstkn;
104: }
105:
106: /**
107: * Return string representation.
108: *
109: * @return a <code>String</code> value
110: */
111: public String toString() {
112: return super .toString() + "(\"" + rdbnam + "\", \"" + rdbcolid
113: + "\", \"" + pkgid + "\", " + pkgsn + ", " + pkgcnstkn
114: + ")";
115: }
116:
117: /**
118: * Return an object which can be used as a key in a hash table
119: * when the value of the consistency token can be ignored. The
120: * object has <code>equals()</code> and <code>hashCode()</code>
121: * methods which consider other objects returned from
122: * <code>getStatementKey()</code> equal if RDBNAM, RDBCOLID, PKGID
123: * and PKGSN are equal.
124: *
125: * @return an <code>Object</code> value
126: * @see Database#getDRDAStatement(Pkgnamcsn)
127: * @see Database#storeStatement(DRDAStatement)
128: * @see Database#removeStatement(DRDAStatement)
129: */
130: public Object getStatementKey() {
131: if (statementKey == null) {
132: statementKey = new StatementKey();
133: }
134: return statementKey;
135: }
136:
137: /**
138: * Class for objects used as keys in the hash table
139: * <code>stmtTable</code> found in the <code>Database</code>
140: * class. The <code>equals()</code> and <code>hashCode()</code>
141: * methods consider other <code>StatementKey</code> objects equal
142: * to this object if they are associated with a
143: * <code>Pkgnamcsn</code> object with the same values for RDBNAM,
144: * RDBCOLID, PKGID and PKGSN.
145: *
146: * @see Database
147: */
148: private final class StatementKey {
149: /** Cached hash code. */
150: private int hash = 0;
151:
152: /**
153: * Check whether RDBNAM, RDBCOLID, PKGID and PKGSN of another
154: * <code>StatementKey</code> object matches this object.
155: *
156: * @param obj another object
157: * @return true if the objects are equal
158: */
159: public boolean equals(Object obj) {
160: if (StatementKey.this == obj) {
161: return true;
162: } else if (obj instanceof StatementKey) {
163: return ((StatementKey) obj).isKeyFor(Pkgnamcsn.this );
164: } else {
165: return false;
166: }
167: }
168:
169: /**
170: * Calculate hash code.
171: *
172: * @return hash code
173: */
174: public int hashCode() {
175: if (hash == 0) {
176: hash = rdbnam.hashCode() ^ rdbcolid.hashCode()
177: ^ pkgid.hashCode() ^ pkgsn;
178: }
179: return hash;
180: }
181:
182: /**
183: * Check whether this object can be used as a key for a
184: * <code>Pkgnamcsn</code> object.
185: *
186: * @param p a <code>Pkgnamcsn</code> value
187: * @return true if this object can be key for the
188: * <code>Pkgnamcsn</code> object
189: */
190: private boolean isKeyFor(Pkgnamcsn p) {
191: return rdbnam.equals(p.rdbnam)
192: && rdbcolid.equals(p.rdbcolid)
193: && pkgid.equals(p.pkgid) && pkgsn == p.pkgsn;
194: }
195: }
196: }
|