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 import org.apache.http.annotation.NotThreadSafe;
36
37 /**
38 * Builder class to build a linked list (chain) of unique class instances. Each class can have
39 * only one instance in the list. Useful for building lists of protocol interceptors.
40 *
41 * @see ImmutableHttpProcessor
42 *
43 * @since 4.3
44 */
45 @NotThreadSafe
46 final class ChainBuilder<E> {
47
48 private final LinkedList<E> list;
49 private final Map<Class<?>, E> uniqueClasses;
50
51 public ChainBuilder() {
52 this.list = new LinkedList<E>();
53 this.uniqueClasses = new HashMap<Class<?>, E>();
54 }
55
56 private void ensureUnique(final E e) {
57 final E previous = this.uniqueClasses.remove(e.getClass());
58 if (previous != null) {
59 this.list.remove(previous);
60 }
61 this.uniqueClasses.put(e.getClass(), e);
62 }
63
64 public ChainBuilder<E> addFirst(final E e) {
65 if (e == null) {
66 return this;
67 }
68 ensureUnique(e);
69 this.list.addFirst(e);
70 return this;
71 }
72
73 public ChainBuilder<E> addLast(final E e) {
74 if (e == null) {
75 return this;
76 }
77 ensureUnique(e);
78 this.list.addLast(e);
79 return this;
80 }
81
82 public ChainBuilder<E> addAllFirst(final Collection<E> c) {
83 if (c == null) {
84 return this;
85 }
86 for (final E e: c) {
87 addFirst(e);
88 }
89 return this;
90 }
91
92 public ChainBuilder<E> addAllFirst(final E... c) {
93 if (c == null) {
94 return this;
95 }
96 for (final E e: c) {
97 addFirst(e);
98 }
99 return this;
100 }
101
102 public ChainBuilder<E> addAllLast(final Collection<E> c) {
103 if (c == null) {
104 return this;
105 }
106 for (final E e: c) {
107 addLast(e);
108 }
109 return this;
110 }
111
112 public ChainBuilder<E> addAllLast(final E... c) {
113 if (c == null) {
114 return this;
115 }
116 for (final E e: c) {
117 addLast(e);
118 }
119 return this;
120 }
121
122 public LinkedList<E> build() {
123 return new LinkedList<E>(this.list);
124 }
125
126 }