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.client5.http.protocol;
29  
30  import java.net.URI;
31  import java.util.ArrayList;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  /**
37   * This class represents a collection of {@link java.net.URI}s used
38   * as redirect locations.
39   *
40   * @since 4.0
41   */
42  public final class RedirectLocations {
43  
44      private final Set<URI> unique;
45      private final List<URI> all;
46  
47      public RedirectLocations() {
48          super();
49          this.unique = new HashSet<>();
50          this.all = new ArrayList<>();
51      }
52  
53      /**
54       * Test if the URI is present in the collection.
55       */
56      public boolean contains(final URI uri) {
57          return this.unique.contains(uri);
58      }
59  
60      /**
61       * Adds a new URI to the collection.
62       */
63      public void add(final URI uri) {
64          this.unique.add(uri);
65          this.all.add(uri);
66      }
67  
68      /**
69       * Returns all redirect {@link URI}s in the order they were added to the collection.
70       *
71       * @return list of all URIs
72       *
73       * @since 4.1
74       */
75      public List<URI> getAll() {
76          return new ArrayList<>(this.all);
77      }
78  
79      /**
80       * Returns the URI at the specified position in this list.
81       *
82       * @param index
83       *            index of the location to return
84       * @return the URI at the specified position in this list
85       * @throws IndexOutOfBoundsException
86       *             if the index is out of range (
87       *             {@code index &lt; 0 || index &gt;= size()})
88       * @since 4.3
89       */
90      public URI get(final int index) {
91          return this.all.get(index);
92      }
93  
94      /**
95       * Returns the number of elements in this list. If this list contains more
96       * than {@code Integer.MAX_VALUE} elements, returns
97       * {@code Integer.MAX_VALUE}.
98       *
99       * @return the number of elements in this list
100      * @since 4.3
101      */
102     public int size() {
103         return this.all.size();
104     }
105 
106     public void clear() {
107         unique.clear();
108         all.clear();
109     }
110 
111 }