001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.caching;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: /**
024: * This is the cache key for one pipeline (or the first part of a pipeline).
025: * It consists of one or more {@link ComponentCacheKey}s.
026: *
027: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
028: * @version CVS $Id: PipelineCacheKey.java 433543 2006-08-22 06:22:54Z crossley $
029: */
030: public final class PipelineCacheKey implements Serializable {
031:
032: /** The keys */
033: private final List keys;
034:
035: /** the hash code */
036: private int hashCode;
037:
038: /**
039: * Constructor
040: */
041: public PipelineCacheKey() {
042: this .keys = new ArrayList(6);
043: }
044:
045: /**
046: * Constructor
047: */
048: public PipelineCacheKey(int size) {
049: this .keys = new ArrayList(size);
050: }
051:
052: /**
053: * Add a key
054: */
055: public void addKey(ComponentCacheKey key) {
056: this .keys.add(key);
057: this .hashCode = 0;
058: this .toString = null;
059: }
060:
061: /**
062: * Remove the last key
063: */
064: public void removeLastKey() {
065: this .keys.remove(this .keys.size() - 1);
066: this .hashCode = 0;
067: this .toString = null;
068: }
069:
070: /**
071: * Remove unitl cachepoint (including cachePoint)
072: */
073: public void removeUntilCachePoint() {
074: this .hashCode = 0;
075: this .toString = null;
076: int keyCount = this .keys.size();
077:
078: while (keyCount > 0) {
079: if (((ComponentCacheKey) this .keys.get(keyCount - 1))
080: .isCachePoint()) {
081: this .keys.remove(keyCount - 1);
082: return;
083: }
084: this .keys.remove(keyCount - 1);
085: keyCount--;
086: }
087: }
088:
089: /**
090: * Return the number of keys
091: */
092: public int size() {
093: return this .keys.size();
094: }
095:
096: /**
097: * Compare
098: */
099: public boolean equals(Object object) {
100: if (object instanceof PipelineCacheKey) {
101: PipelineCacheKey pck = (PipelineCacheKey) object;
102: final int len = this .keys.size();
103: if (pck.keys.size() == len) {
104: boolean cont = true;
105: int i = 0;
106: while (i < len && cont) {
107: cont = this .keys.get(i).equals(pck.keys.get(i));
108: i++;
109: }
110: return cont;
111: }
112: }
113: return false;
114: }
115:
116: /**
117: * Generate a hash code
118: */
119: public int hashCode() {
120: if (this .hashCode == 0) {
121: final int len = this .keys.size();
122: for (int i = 0; i < len; i++) {
123: this .hashCode += this .keys.get(i).hashCode();
124: }
125: if (len % 2 == 0)
126: this .hashCode++;
127: }
128: return this .hashCode;
129: }
130:
131: /**
132: * Clone the object (but not the component keys)
133: */
134: public PipelineCacheKey copy() {
135: final int len = this .keys.size();
136: PipelineCacheKey pck = new PipelineCacheKey(len);
137: for (int i = 0; i < len; i++) {
138: pck.keys.add(this .keys.get(i));
139: }
140: return pck;
141: }
142:
143: private String toString;
144:
145: /**
146: * toString
147: * The FilesystemStore uses toString!
148: */
149: public String toString() {
150: if (this .toString == null) {
151: StringBuffer buffer = new StringBuffer();
152: buffer.append("PK");
153: final int len = this .keys.size();
154: for (int i = 0; i < len; i++) {
155: buffer.append('_').append(this.keys.get(i).toString());
156: }
157: this.toString = buffer.toString();
158: }
159: return toString;
160: }
161: }
|