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.ArrayList;
32  import java.util.HashSet;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Set;
36  
37  import org.apache.http.annotation.NotThreadSafe;
38  
39  /**
40   * This class represents a collection of {@link URI}s used as redirect locations.
41   *
42   * @since 4.0
43   */
44  @NotThreadSafe // HashSet is not synch.
45  public class RedirectLocations {
46  
47      private final Set<URI> unique;
48      private final List<URI> all;
49  
50      public RedirectLocations() {
51          super();
52          this.unique = new HashSet<URI>();
53          this.all = new ArrayList<URI>();
54      }
55  
56      /**
57       * Test if the URI is present in the collection.
58       */
59      public boolean contains(final URI uri) {
60          return this.unique.contains(uri);
61      }
62  
63      /**
64       * Adds a new URI to the collection.
65       */
66      public void add(final URI uri) {
67          this.unique.add(uri);
68          this.all.add(uri);
69      }
70  
71      /**
72       * Removes a URI from the collection.
73       */
74      public boolean remove(final URI uri) {
75          final boolean removed = this.unique.remove(uri);
76          if (removed) {
77              final Iterator<URI> it = this.all.iterator();
78              while (it.hasNext()) {
79                  final URI current = it.next();
80                  if (current.equals(uri)) {
81                      it.remove();
82                  }
83              }
84          }
85          return removed;
86      }
87  
88      /**
89       * Returns all redirect {@link URI}s in the order they were added to the collection.
90       *
91       * @return list of all URIs
92       *
93       * @since 4.1
94       */
95      public List<URI> getAll() {
96          return new ArrayList<URI>(this.all);
97      }
98  
99  }