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 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      /**
48       * Constructs a new instance.
49       */
50      public RedirectLocations() {
51          super();
52          this.unique = new HashSet<>();
53          this.all = new ArrayList<>();
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       * Returns all redirect {@link URI}s in the order they were added to the collection.
73       *
74       * @return list of all URIs
75       *
76       * @since 4.1
77       */
78      public List<URI> getAll() {
79          return new ArrayList<>(this.all);
80      }
81  
82      /**
83       * Returns the URI at the specified position in this list.
84       *
85       * @param index
86       *            index of the location to return
87       * @return the URI at the specified position in this list
88       * @throws IndexOutOfBoundsException
89       *             if the index is out of range (
90       *             {@code index &lt; 0 || index &gt;= size()})
91       * @since 4.3
92       */
93      public URI get(final int index) {
94          return this.all.get(index);
95      }
96  
97      /**
98       * Returns the number of elements in this list. If this list contains more
99       * than {@code Integer.MAX_VALUE} elements, returns
100      * {@code Integer.MAX_VALUE}.
101      *
102      * @return the number of elements in this list
103      * @since 4.3
104      */
105     public int size() {
106         return this.all.size();
107     }
108 
109     public void clear() {
110         unique.clear();
111         all.clear();
112     }
113 
114 }