KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > caching > PipelineCacheKey


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.caching;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * This is the cache key for one pipeline (or the first part of a pipeline).
24  * It consists of one or more {@link ComponentCacheKey}s.
25  *
26  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
27  * @version CVS $Id: PipelineCacheKey.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29 public final class PipelineCacheKey
30         implements Serializable JavaDoc {
31
32     /** The keys */
33     private final List JavaDoc keys;
34
35     /** the hash code */
36     private int hashCode;
37
38     /**
39      * Constructor
40      */

41     public PipelineCacheKey() {
42         this.keys = new ArrayList JavaDoc(6);
43     }
44
45     /**
46      * Constructor
47      */

48     public PipelineCacheKey(int size) {
49         this.keys = new ArrayList JavaDoc(size);
50     }
51
52     /**
53      * Add a key
54      */

55     public void addKey(ComponentCacheKey key) {
56         this.keys.add(key);
57         this.hashCode = 0;
58         this.toString = null;
59     }
60
61     /**
62      * Remove the last key
63      */

64     public void removeLastKey() {
65         this.keys.remove(this.keys.size()-1);
66         this.hashCode = 0;
67         this.toString = null;
68     }
69
70     /**
71      * Remove unitl cachepoint (including cachePoint)
72      */

73     public void removeUntilCachePoint() {
74         this.hashCode = 0;
75         this.toString = null;
76         int keyCount = this.keys.size();
77
78         while (keyCount > 0) {
79             if (((ComponentCacheKey)this.keys.get(keyCount-1)).isCachePoint()) {
80                 this.keys.remove(keyCount-1);
81                 return;
82             }
83             this.keys.remove(keyCount-1);
84             keyCount--;
85         }
86     }
87
88     /**
89      * Return the number of keys
90      */

91     public int size() {
92         return this.keys.size();
93     }
94
95     /**
96      * Compare
97      */

98     public boolean equals(Object JavaDoc object) {
99         if (object instanceof PipelineCacheKey) {
100             PipelineCacheKey pck = (PipelineCacheKey)object;
101             final int len = this.keys.size();
102             if (pck.keys.size() == len) {
103                 boolean cont = true;
104                 int i = 0;
105                 while (i < len && cont) {
106                     cont = this.keys.get(i).equals(pck.keys.get(i));
107                     i++;
108                 }
109                 return cont;
110             }
111         }
112         return false;
113     }
114
115     /**
116      * Generate a hash code
117      */

118     public int hashCode() {
119         if (this.hashCode == 0) {
120             final int len = this.keys.size();
121             for(int i=0; i < len; i++) {
122                 this.hashCode += this.keys.get(i).hashCode();
123             }
124             if (len % 2 == 0) this.hashCode++;
125         }
126         return this.hashCode;
127     }
128
129     /**
130      * Clone the object (but not the component keys)
131      */

132     public PipelineCacheKey copy() {
133         final int len = this.keys.size();
134         PipelineCacheKey pck = new PipelineCacheKey(len);
135         for(int i=0; i < len; i++) {
136             pck.keys.add(this.keys.get(i));
137         }
138         return pck;
139     }
140
141     private String JavaDoc toString;
142
143     /**
144      * toString
145      * The FilesystemStore uses toString!
146      */

147     public String JavaDoc toString() {
148         if (this.toString == null) {
149             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
150             buffer.append("PK");
151             final int len = this.keys.size();
152             for(int i=0; i < len; i++) {
153                 buffer.append('_').append(this.keys.get(i).toString());
154             }
155             this.toString = buffer.toString();
156         }
157         return toString;
158     }
159 }
160
Popular Tags