001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.protocol; 029 030import java.util.Collection; 031import java.util.HashMap; 032import java.util.LinkedList; 033import java.util.Map; 034 035/** 036 * Builder class to build a linked list (chain) of unique class instances. Each class can have 037 * only one instance in the list. Useful for building lists of protocol interceptors. 038 * 039 * @see ImmutableHttpProcessor 040 * 041 * @since 4.3 042 */ 043final class ChainBuilder<E> { 044 045 private final LinkedList<E> list; 046 private final Map<Class<?>, E> uniqueClasses; 047 048 public ChainBuilder() { 049 this.list = new LinkedList<E>(); 050 this.uniqueClasses = new HashMap<Class<?>, E>(); 051 } 052 053 private void ensureUnique(final E e) { 054 final E previous = this.uniqueClasses.remove(e.getClass()); 055 if (previous != null) { 056 this.list.remove(previous); 057 } 058 this.uniqueClasses.put(e.getClass(), e); 059 } 060 061 public ChainBuilder<E> addFirst(final E e) { 062 if (e == null) { 063 return this; 064 } 065 ensureUnique(e); 066 this.list.addFirst(e); 067 return this; 068 } 069 070 public ChainBuilder<E> addLast(final E e) { 071 if (e == null) { 072 return this; 073 } 074 ensureUnique(e); 075 this.list.addLast(e); 076 return this; 077 } 078 079 public ChainBuilder<E> addAllFirst(final Collection<E> c) { 080 if (c == null) { 081 return this; 082 } 083 for (final E e: c) { 084 addFirst(e); 085 } 086 return this; 087 } 088 089 public ChainBuilder<E> addAllFirst(final E... c) { 090 if (c == null) { 091 return this; 092 } 093 for (final E e: c) { 094 addFirst(e); 095 } 096 return this; 097 } 098 099 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}