View Javadoc

    1   // ========================================================================
    2   // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    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   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package org.mortbay.io;
    16
    17  import java.util.ArrayList;
    18  import java.util.HashMap;
    19  import java.util.Map.Entry;
    20
    21  import org.mortbay.util.StringMap;
    22
    23  /* ------------------------------------------------------------------------------- */
    24  /**
    25   * Stores a collection of {@link Buffer} objects.
    26   * Buffers are stored in an ordered collection and can retreived by index or value
    27   * @author gregw
    28   */
    29  public class BufferCache
    30  {
    31      private HashMap _bufferMap=new HashMap();
    32      private StringMap _stringMap=new StringMap(StringMap.CASE_INSENSTIVE);
    33      private ArrayList _index= new ArrayList();
    34
    35      /* ------------------------------------------------------------------------------- */
    36      /** Add a buffer to the cache at the specified index.
    37       * @param value The content of the buffer.
    38       */
    39      public CachedBuffer add(String value, int ordinal)
    40      {
    41          CachedBuffer buffer= new CachedBuffer(value, ordinal);
    42          _bufferMap.put(buffer, buffer);
    43          _stringMap.put(value, buffer);
    44          while ((ordinal - _index.size()) > 0)
    45              _index.add(null);
    46          _index.add(ordinal, buffer);
    47          return buffer;
    48      }
    49
    50      public CachedBuffer get(int ordinal)
    51      {
    52          if (ordinal < 0 || ordinal >= _index.size())
    53              return null;
    54          return (CachedBuffer)_index.get(ordinal);
    55      }
    56
    57      public CachedBuffer get(Buffer buffer)
    58      {
    59          return (CachedBuffer)_bufferMap.get(buffer);
    60      }
    61
    62      public CachedBuffer get(String value)
    63      {
    64          return (CachedBuffer)_stringMap.get(value);
    65      }
    66
    67      public Buffer lookup(Buffer buffer)
    68      {
    69          Buffer b= get(buffer);
    70          if (b == null)
    71          {
    72              if (buffer instanceof Buffer.CaseInsensitve)
    73                  return buffer;
    74              return new View.CaseInsensitive(buffer);
    75          }
    76
    77          return b;
    78      }
    79
    80      public CachedBuffer getBest(byte[] value, int offset, int maxLength)
    81      {
    82          Entry entry = _stringMap.getBestEntry(value, offset, maxLength);
    83          if (entry!=null)
    84              return (CachedBuffer)entry.getValue();
    85          return null;
    86      }
    87
    88      public Buffer lookup(String value)
    89      {
    90          Buffer b= get(value);
    91          if (b == null)
    92          {
    93              return new CachedBuffer(value,-1);
    94          }
    95          return b;
    96      }
    97
    98      public String toString(Buffer buffer)
    99      {
    100         return lookup(buffer).toString();
    101     }
    102
    103     public int getOrdinal(Buffer buffer)
    104     {
    105         if (buffer instanceof CachedBuffer)
    106             return ((CachedBuffer)buffer).getOrdinal();
    107         buffer=lookup(buffer);
    108         if (buffer!=null && buffer instanceof CachedBuffer)
    109             return ((CachedBuffer)buffer).getOrdinal();
    110         return -1;
    111     }
    112
    113     public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive
    114     {
    115         private int _ordinal;
    116         private HashMap _associateMap=null;
    117
    118         public CachedBuffer(String value, int ordinal)
    119         {
    120             super(value);
    121             _ordinal= ordinal;
    122         }
    123
    124         public int getOrdinal()
    125         {
    126             return _ordinal;
    127         }
    128
    129         public CachedBuffer getAssociate(Object key)
    130         {
    131             if (_associateMap==null)
    132                 return null;
    133             return (CachedBuffer)_associateMap.get(key);
    134         }
    135
    136         public void setAssociate(Object key, CachedBuffer associate)
    137         {
    138             // TODO should be synchronized - but lets try without
    139             if (_associateMap==null)
    140                 _associateMap=new HashMap();
    141             _associateMap.put(key,associate);
    142         }
    143     }
    144
    145
    146     public String toString()
    147     {
    148         return "CACHE["+
    149         	"bufferMap="+_bufferMap+
    150         	",stringMap="+_stringMap+
    151         	",index="+_index+
    152         	"]";
    153     }
    154 }