View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.protocol;
29  
30  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.LinkedList;
33  import java.util.Map;
34  
35  /**
36   * Builder class to build a linked list (chain) of unique class instances. Each class can have
37   * only one instance in the list. Useful for building lists of protocol interceptors.
38   *
39   * @see ImmutableHttpProcessor
40   *
41   * @since 4.3
42   */
43  final class ChainBuilder<E> {
44  
45      private final LinkedList<E> list;
46      private final Map<Class<?>, E> uniqueClasses;
47  
48      public ChainBuilder() {
49          this.list = new LinkedList<E>();
50          this.uniqueClasses = new HashMap<Class<?>, E>();
51      }
52  
53      private void ensureUnique(final E e) {
54          final E previous = this.uniqueClasses.remove(e.getClass());
55          if (previous != null) {
56              this.list.remove(previous);
57          }
58          this.uniqueClasses.put(e.getClass(), e);
59      }
60  
61      public ChainBuilder<E> addFirst(final E e) {
62          if (e == null) {
63              return this;
64          }
65          ensureUnique(e);
66          this.list.addFirst(e);
67          return this;
68      }
69  
70      public ChainBuilder<E> addLast(final E e) {
71          if (e == null) {
72              return this;
73          }
74          ensureUnique(e);
75          this.list.addLast(e);
76          return this;
77      }
78  
79      public ChainBuilder<E> addAllFirst(final Collection<E> c) {
80          if (c == null) {
81              return this;
82          }
83          for (final E e: c) {
84              addFirst(e);
85          }
86          return this;
87      }
88  
89      public ChainBuilder<E> addAllFirst(final E... c) {
90          if (c == null) {
91              return this;
92          }
93          for (final E e: c) {
94              addFirst(e);
95          }
96          return this;
97      }
98  
99      public ChainBuilder<E> addAllLast(final Collection<E> c) {
100         if (c == null) {
101             return this;
102         }
103         for (final E e: c) {
104             addLast(e);
105         }
106         return this;
107     }
108 
109     public ChainBuilder<E> addAllLast(final E... c) {
110         if (c == null) {
111             return this;
112         }
113         for (final E e: c) {
114             addLast(e);
115         }
116         return this;
117     }
118 
119     public LinkedList<E> build() {
120         return new LinkedList<E>(this.list);
121     }
122 
123 }