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.impl.client;
29  
30  import java.net.URI;
31  import java.util.AbstractList;
32  import java.util.ArrayList;
33  import java.util.HashSet;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Set;
37  
38  import org.apache.http.annotation.NotThreadSafe;
39  
40  /**
41   * This class represents a collection of {@link java.net.URI}s used
42   * as redirect locations.
43   *
44   * @since 4.0
45   */
46  @NotThreadSafe // HashSet/ArrayList are not synch.
47  public class RedirectLocations extends AbstractList<Object> {
48  
49      private final Set<URI> unique;
50      private final List<URI> all;
51  
52      public RedirectLocations() {
53          super();
54          this.unique = new HashSet<URI>();
55          this.all = new ArrayList<URI>();
56      }
57  
58      /**
59       * Test if the URI is present in the collection.
60       */
61      public boolean contains(final URI uri) {
62          return this.unique.contains(uri);
63      }
64  
65      /**
66       * Adds a new URI to the collection.
67       */
68      public void add(final URI uri) {
69          this.unique.add(uri);
70          this.all.add(uri);
71      }
72  
73      /**
74       * Removes a URI from the collection.
75       */
76      public boolean remove(final URI uri) {
77          final boolean removed = this.unique.remove(uri);
78          if (removed) {
79              final Iterator<URI> it = this.all.iterator();
80              while (it.hasNext()) {
81                  final URI current = it.next();
82                  if (current.equals(uri)) {
83                      it.remove();
84                  }
85              }
86          }
87          return removed;
88      }
89  
90      /**
91       * Returns all redirect {@link URI}s in the order they were added to the collection.
92       *
93       * @return list of all URIs
94       *
95       * @since 4.1
96       */
97      public List<URI> getAll() {
98          return new ArrayList<URI>(this.all);
99      }
100 
101     /**
102      * Returns the URI at the specified position in this list.
103      *
104      * @param index
105      *            index of the location to return
106      * @return the URI at the specified position in this list
107      * @throws IndexOutOfBoundsException
108      *             if the index is out of range (
109      *             <tt>index &lt; 0 || index &gt;= size()</tt>)
110      * @since 4.3
111      */
112     @Override
113     public URI get(final int index) {
114         return this.all.get(index);
115     }
116 
117     /**
118      * Returns the number of elements in this list. If this list contains more
119      * than <tt>Integer.MAX_VALUE</tt> elements, returns
120      * <tt>Integer.MAX_VALUE</tt>.
121      *
122      * @return the number of elements in this list
123      * @since 4.3
124      */
125     @Override
126     public int size() {
127         return this.all.size();
128     }
129 
130     /**
131      * Replaces the URI at the specified position in this list with the
132      * specified element (must be a URI).
133      *
134      * @param index
135      *            index of the element to replace
136      * @param element
137      *            URI to be stored at the specified position
138      * @return the URI previously at the specified position
139      * @throws UnsupportedOperationException
140      *             if the <tt>set</tt> operation is not supported by this list
141      * @throws ClassCastException
142      *             if the element is not a {@link URI}
143      * @throws NullPointerException
144      *             if the specified element is null and this list does not
145      *             permit null elements
146      * @throws IndexOutOfBoundsException
147      *             if the index is out of range (
148      *             <tt>index &lt; 0 || index &gt;= size()</tt>)
149      * @since 4.3
150      */
151     @Override
152     public Object set(final int index, final Object element) {
153         final URI removed = this.all.set(index, (URI) element);
154         this.unique.remove(removed);
155         this.unique.add((URI) element);
156         if (this.all.size() != this.unique.size()) {
157             this.unique.addAll(this.all);
158         }
159         return removed;
160     }
161 
162     /**
163      * Inserts the specified element at the specified position in this list
164      * (must be a URI). Shifts the URI currently at that position (if any) and
165      * any subsequent URIs to the right (adds one to their indices).
166      *
167      * @param index
168      *            index at which the specified element is to be inserted
169      * @param element
170      *            URI to be inserted
171      * @throws UnsupportedOperationException
172      *             if the <tt>add</tt> operation is not supported by this list
173      * @throws ClassCastException
174      *             if the element is not a {@link URI}
175      * @throws NullPointerException
176      *             if the specified element is null and this list does not
177      *             permit null elements
178      * @throws IndexOutOfBoundsException
179      *             if the index is out of range (
180      *             <tt>index &lt; 0 || index &gt; size()</tt>)
181      * @since 4.3
182      */
183     @Override
184     public void add(final int index, final Object element) {
185         this.all.add(index, (URI) element);
186         this.unique.add((URI) element);
187     }
188 
189     /**
190      * Removes the URI at the specified position in this list. Shifts any
191      * subsequent URIs to the left (subtracts one from their indices). Returns
192      * the URI that was removed from the list.
193      *
194      * @param index
195      *            the index of the URI to be removed
196      * @return the URI previously at the specified position
197      * @throws IndexOutOfBoundsException
198      *             if the index is out of range (
199      *             <tt>index &lt; 0 || index &gt;= size()</tt>)
200      * @since 4.3
201      */
202     @Override
203     public URI remove(final int index) {
204         final URI removed = this.all.remove(index);
205         this.unique.remove(removed);
206         if (this.all.size() != this.unique.size()) {
207             this.unique.addAll(this.all);
208         }
209         return removed;
210     }
211 
212     /**
213      * Returns <tt>true</tt> if this collection contains the specified element.
214      * More formally, returns <tt>true</tt> if and only if this collection
215      * contains at least one element <tt>e</tt> such that
216      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
217      *
218      * @param o element whose presence in this collection is to be tested
219      * @return <tt>true</tt> if this collection contains the specified
220      *         element
221      */
222     @Override
223     public boolean contains(final Object o) {
224         return this.unique.contains(o);
225     }
226 
227 }