001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.io;
016:
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.Map.Entry;
020:
021: import org.mortbay.util.StringMap;
022:
023: /* ------------------------------------------------------------------------------- */
024: /**
025: * Stores a collection of {@link Buffer} objects.
026: * Buffers are stored in an ordered collection and can retreived by index or value
027: * @author gregw
028: */
029: public class BufferCache {
030: private HashMap _bufferMap = new HashMap();
031: private StringMap _stringMap = new StringMap(
032: StringMap.CASE_INSENSTIVE);
033: private ArrayList _index = new ArrayList();
034:
035: /* ------------------------------------------------------------------------------- */
036: /** Add a buffer to the cache at the specified index.
037: * @param value The content of the buffer.
038: */
039: public CachedBuffer add(String value, int ordinal) {
040: CachedBuffer buffer = new CachedBuffer(value, ordinal);
041: _bufferMap.put(buffer, buffer);
042: _stringMap.put(value, buffer);
043: while ((ordinal - _index.size()) > 0)
044: _index.add(null);
045: _index.add(ordinal, buffer);
046: return buffer;
047: }
048:
049: public CachedBuffer get(int ordinal) {
050: if (ordinal < 0 || ordinal >= _index.size())
051: return null;
052: return (CachedBuffer) _index.get(ordinal);
053: }
054:
055: public CachedBuffer get(Buffer buffer) {
056: return (CachedBuffer) _bufferMap.get(buffer);
057: }
058:
059: public CachedBuffer get(String value) {
060: return (CachedBuffer) _stringMap.get(value);
061: }
062:
063: public Buffer lookup(Buffer buffer) {
064: Buffer b = get(buffer);
065: if (b == null) {
066: return buffer;
067: }
068:
069: return b;
070: }
071:
072: public CachedBuffer getBest(byte[] value, int offset, int maxLength) {
073: Entry entry = _stringMap.getBestEntry(value, offset, maxLength);
074: if (entry != null)
075: return (CachedBuffer) entry.getValue();
076: return null;
077: }
078:
079: public Buffer lookup(String value) {
080: Buffer b = get(value);
081: if (b == null) {
082: return new CachedBuffer(value, -1);
083: }
084: return b;
085: }
086:
087: public String toString(Buffer buffer) {
088: return lookup(buffer).toString();
089: }
090:
091: public int getOrdinal(Buffer buffer) {
092: if (buffer instanceof CachedBuffer)
093: return ((CachedBuffer) buffer).getOrdinal();
094: buffer = lookup(buffer);
095: if (buffer != null && buffer instanceof CachedBuffer)
096: return ((CachedBuffer) buffer).getOrdinal();
097: return -1;
098: }
099:
100: public static class CachedBuffer extends
101: ByteArrayBuffer.CaseInsensitive {
102: private int _ordinal;
103: private HashMap _associateMap = null;
104:
105: public CachedBuffer(String value, int ordinal) {
106: super (value);
107: _ordinal = ordinal;
108: }
109:
110: public int getOrdinal() {
111: return _ordinal;
112: }
113:
114: public CachedBuffer getAssociate(Object key) {
115: if (_associateMap == null)
116: return null;
117: return (CachedBuffer) _associateMap.get(key);
118: }
119:
120: public void setAssociate(Object key, CachedBuffer associate) {
121: // TODO should be synchronized - but lets try without
122: if (_associateMap == null)
123: _associateMap = new HashMap();
124: _associateMap.put(key, associate);
125: }
126: }
127:
128: public String toString() {
129: return "CACHE[" + "bufferMap=" + _bufferMap + ",stringMap="
130: + _stringMap + ",index=" + _index + "]";
131: }
132: }
|