View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.nio.ByteBuffer;
22  import java.nio.charset.StandardCharsets;
23  
24  
25  /** 
26   * Abstract Trie implementation.
27   * <p>Provides some common implementations, which may not be the most
28   * efficient. For byte operations, the assumption is made that the charset
29   * is ISO-8859-1</p>
30   * 
31   * @param <V> the type of object that the Trie holds
32   */
33  public abstract class AbstractTrie<V> implements Trie<V>
34  {
35      final boolean _caseInsensitive;
36      
37      protected AbstractTrie(boolean insensitive)
38      {
39          _caseInsensitive=insensitive;
40      }
41  
42      @Override
43      public boolean put(V v)
44      {
45          return put(v.toString(),v);
46      }
47  
48      @Override
49      public V remove(String s)
50      {
51          V o=get(s);
52          put(s,null);
53          return o;
54      }
55  
56      @Override
57      public V get(String s)
58      {
59          return get(s,0,s.length());
60      }
61  
62      @Override
63      public V get(ByteBuffer b)
64      {
65          return get(b,0,b.remaining());
66      }
67  
68      @Override
69      public V getBest(String s)
70      {
71          return getBest(s,0,s.length());
72      }
73      
74      @Override
75      public V getBest(byte[] b, int offset, int len)
76      {
77          return getBest(new String(b,offset,len,StandardCharsets.ISO_8859_1));
78      }
79  
80      @Override
81      public boolean isCaseInsensitive()
82      {
83          return _caseInsensitive;
84      }
85  
86  }