View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.handler.codec;
16  
17  import java.util.ArrayList;
18  import java.util.Comparator;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map.Entry;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  public class DefaultConvertibleHeaders<UnconvertedType, ConvertedType> extends DefaultHeaders<UnconvertedType>
26          implements ConvertibleHeaders<UnconvertedType, ConvertedType> {
27  
28      private final TypeConverter<UnconvertedType, ConvertedType> typeConverter;
29  
30      public DefaultConvertibleHeaders(Comparator<? super UnconvertedType> keyComparator,
31              Comparator<? super UnconvertedType> valueComparator,
32              HashCodeGenerator<UnconvertedType> hashCodeGenerator,
33              ValueConverter<UnconvertedType> valueConverter,
34              TypeConverter<UnconvertedType, ConvertedType> typeConverter) {
35          super(keyComparator, valueComparator, hashCodeGenerator, valueConverter);
36          this.typeConverter = typeConverter;
37      }
38  
39      public DefaultConvertibleHeaders(Comparator<? super UnconvertedType> keyComparator,
40              Comparator<? super UnconvertedType> valueComparator,
41              HashCodeGenerator<UnconvertedType> hashCodeGenerator,
42              ValueConverter<UnconvertedType> valueConverter,
43              TypeConverter<UnconvertedType, ConvertedType> typeConverter,
44              NameConverter<UnconvertedType> nameConverter) {
45          super(keyComparator, valueComparator, hashCodeGenerator, valueConverter, nameConverter);
46          this.typeConverter = typeConverter;
47      }
48  
49      @Override
50      public ConvertedType getAndConvert(UnconvertedType name) {
51          return getAndConvert(name, null);
52      }
53  
54      @Override
55      public ConvertedType getAndConvert(UnconvertedType name, ConvertedType defaultValue) {
56          UnconvertedType v = get(name);
57          if (v == null) {
58              return defaultValue;
59          }
60          return typeConverter.toConvertedType(v);
61      }
62  
63      @Override
64      public ConvertedType getAndRemoveAndConvert(UnconvertedType name) {
65          return getAndRemoveAndConvert(name, null);
66      }
67  
68      @Override
69      public ConvertedType getAndRemoveAndConvert(UnconvertedType name, ConvertedType defaultValue) {
70          UnconvertedType v = getAndRemove(name);
71          if (v == null) {
72              return defaultValue;
73          }
74          return typeConverter.toConvertedType(v);
75      }
76  
77      @Override
78      public List<ConvertedType> getAllAndConvert(UnconvertedType name) {
79          List<UnconvertedType> all = getAll(name);
80          List<ConvertedType> allConverted = new ArrayList<ConvertedType>(all.size());
81          for (int i = 0; i < all.size(); ++i) {
82              allConverted.add(typeConverter.toConvertedType(all.get(i)));
83          }
84          return allConverted;
85      }
86  
87      @Override
88      public List<ConvertedType> getAllAndRemoveAndConvert(UnconvertedType name) {
89          List<UnconvertedType> all = getAllAndRemove(name);
90          List<ConvertedType> allConverted = new ArrayList<ConvertedType>(all.size());
91          for (int i = 0; i < all.size(); ++i) {
92              allConverted.add(typeConverter.toConvertedType(all.get(i)));
93          }
94          return allConverted;
95      }
96  
97      @Override
98      public List<Entry<ConvertedType, ConvertedType>> entriesConverted() {
99          List<Entry<UnconvertedType, UnconvertedType>> entries = entries();
100         List<Entry<ConvertedType, ConvertedType>> entriesConverted = new ArrayList<Entry<ConvertedType, ConvertedType>>(
101                 entries.size());
102         for (int i = 0; i < entries.size(); ++i) {
103             entriesConverted.add(new ConvertedEntry(entries.get(i)));
104         }
105         return entriesConverted;
106     }
107 
108     @Override
109     public Iterator<Entry<ConvertedType, ConvertedType>> iteratorConverted() {
110         return new ConvertedIterator();
111     }
112 
113     @Override
114     public Set<ConvertedType> namesAndConvert(Comparator<ConvertedType> comparator) {
115         Set<UnconvertedType> names = names();
116         Set<ConvertedType> namesConverted = new TreeSet<ConvertedType>(comparator);
117         for (UnconvertedType unconverted : names) {
118             namesConverted.add(typeConverter.toConvertedType(unconverted));
119         }
120         return namesConverted;
121     }
122 
123     private final class ConvertedIterator implements Iterator<Entry<ConvertedType, ConvertedType>> {
124         private final Iterator<Entry<UnconvertedType, UnconvertedType>> iter = iterator();
125 
126         @Override
127         public boolean hasNext() {
128             return iter.hasNext();
129         }
130 
131         @Override
132         public Entry<ConvertedType, ConvertedType> next() {
133             Entry<UnconvertedType, UnconvertedType> next = iter.next();
134 
135             return new ConvertedEntry(next);
136         }
137 
138         @Override
139         public void remove() {
140             throw new UnsupportedOperationException();
141         }
142     }
143 
144     private final class ConvertedEntry implements Entry<ConvertedType, ConvertedType> {
145         private final Entry<UnconvertedType, UnconvertedType> entry;
146         private ConvertedType name;
147         private ConvertedType value;
148 
149         ConvertedEntry(Entry<UnconvertedType, UnconvertedType> entry) {
150             this.entry = entry;
151         }
152 
153         @Override
154         public ConvertedType getKey() {
155             if (name == null) {
156                 name = typeConverter.toConvertedType(entry.getKey());
157             }
158             return name;
159         }
160 
161         @Override
162         public ConvertedType getValue() {
163             if (value == null) {
164                 value = typeConverter.toConvertedType(entry.getValue());
165             }
166             return value;
167         }
168 
169         @Override
170         public ConvertedType setValue(ConvertedType value) {
171             ConvertedType old = getValue();
172             entry.setValue(typeConverter.toUnconvertedType(value));
173             return old;
174         }
175 
176         @Override
177         public String toString() {
178             return entry.toString();
179         }
180     }
181 }