1 16 package org.apache.cocoon.caching; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 29 public final class PipelineCacheKey 30 implements Serializable { 31 32 33 private final List keys; 34 35 36 private int hashCode; 37 38 41 public PipelineCacheKey() { 42 this.keys = new ArrayList (6); 43 } 44 45 48 public PipelineCacheKey(int size) { 49 this.keys = new ArrayList (size); 50 } 51 52 55 public void addKey(ComponentCacheKey key) { 56 this.keys.add(key); 57 this.hashCode = 0; 58 this.toString = null; 59 } 60 61 64 public void removeLastKey() { 65 this.keys.remove(this.keys.size()-1); 66 this.hashCode = 0; 67 this.toString = null; 68 } 69 70 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 91 public int size() { 92 return this.keys.size(); 93 } 94 95 98 public boolean equals(Object 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 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 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 toString; 142 143 147 public String toString() { 148 if (this.toString == null) { 149 StringBuffer buffer = new StringBuffer (); 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 |