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.hc.core5.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 DefaultHttpProcessor
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<>();
50          this.uniqueClasses = new HashMap<>();
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      @SafeVarargs
90      public final ChainBuilder<E> addAllFirst(final E... c) {
91          if (c == null) {
92              return this;
93          }
94          for (final E e: c) {
95              addFirst(e);
96          }
97          return this;
98      }
99  
100     public ChainBuilder<E> addAllLast(final Collection<E> c) {
101         if (c == null) {
102             return this;
103         }
104         for (final E e: c) {
105             addLast(e);
106         }
107         return this;
108     }
109 
110     @SafeVarargs
111     public final ChainBuilder<E> addAllLast(final E... c) {
112         if (c == null) {
113             return this;
114         }
115         for (final E e: c) {
116             addLast(e);
117         }
118         return this;
119     }
120 
121     public LinkedList<E> build() {
122         return new LinkedList<>(this.list);
123     }
124 
125 }