summaryrefslogtreecommitdiffstats
path: root/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/RedirectLocations.java
blob: 872dc167ff8e2e416b3a3efc98fe101ee5cf6315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package ch.boye.httpclientandroidlib.impl.client;

import java.net.URI;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;

/**
 * This class represents a collection of {@link java.net.URI}s used
 * as redirect locations.
 *
 * @since 4.0
 */
@NotThreadSafe // HashSet/ArrayList are not synch.
public class RedirectLocations extends AbstractList<Object> {

    private final Set<URI> unique;
    private final List<URI> all;

    public RedirectLocations() {
        super();
        this.unique = new HashSet<URI>();
        this.all = new ArrayList<URI>();
    }

    /**
     * Test if the URI is present in the collection.
     */
    public boolean contains(final URI uri) {
        return this.unique.contains(uri);
    }

    /**
     * Adds a new URI to the collection.
     */
    public void add(final URI uri) {
        this.unique.add(uri);
        this.all.add(uri);
    }

    /**
     * Removes a URI from the collection.
     */
    public boolean remove(final URI uri) {
        final boolean removed = this.unique.remove(uri);
        if (removed) {
            final Iterator<URI> it = this.all.iterator();
            while (it.hasNext()) {
                final URI current = it.next();
                if (current.equals(uri)) {
                    it.remove();
                }
            }
        }
        return removed;
    }

    /**
     * Returns all redirect {@link URI}s in the order they were added to the collection.
     *
     * @return list of all URIs
     *
     * @since 4.1
     */
    public List<URI> getAll() {
        return new ArrayList<URI>(this.all);
    }

    /**
     * Returns the URI at the specified position in this list.
     *
     * @param index
     *            index of the location to return
     * @return the URI at the specified position in this list
     * @throws IndexOutOfBoundsException
     *             if the index is out of range (
     *             <tt>index &lt; 0 || index &gt;= size()</tt>)
     * @since 4.3
     */
    @Override
    public URI get(final int index) {
        return this.all.get(index);
    }

    /**
     * Returns the number of elements in this list. If this list contains more
     * than <tt>Integer.MAX_VALUE</tt> elements, returns
     * <tt>Integer.MAX_VALUE</tt>.
     *
     * @return the number of elements in this list
     * @since 4.3
     */
    @Override
    public int size() {
        return this.all.size();
    }

    /**
     * Replaces the URI at the specified position in this list with the
     * specified element (must be a URI).
     *
     * @param index
     *            index of the element to replace
     * @param element
     *            URI to be stored at the specified position
     * @return the URI previously at the specified position
     * @throws UnsupportedOperationException
     *             if the <tt>set</tt> operation is not supported by this list
     * @throws ClassCastException
     *             if the element is not a {@link URI}
     * @throws NullPointerException
     *             if the specified element is null and this list does not
     *             permit null elements
     * @throws IndexOutOfBoundsException
     *             if the index is out of range (
     *             <tt>index &lt; 0 || index &gt;= size()</tt>)
     * @since 4.3
     */
    @Override
    public Object set(final int index, final Object element) {
        final URI removed = this.all.set(index, (URI) element);
        this.unique.remove(removed);
        this.unique.add((URI) element);
        if (this.all.size() != this.unique.size()) {
            this.unique.addAll(this.all);
        }
        return removed;
    }

    /**
     * Inserts the specified element at the specified position in this list
     * (must be a URI). Shifts the URI currently at that position (if any) and
     * any subsequent URIs to the right (adds one to their indices).
     *
     * @param index
     *            index at which the specified element is to be inserted
     * @param element
     *            URI to be inserted
     * @throws UnsupportedOperationException
     *             if the <tt>add</tt> operation is not supported by this list
     * @throws ClassCastException
     *             if the element is not a {@link URI}
     * @throws NullPointerException
     *             if the specified element is null and this list does not
     *             permit null elements
     * @throws IndexOutOfBoundsException
     *             if the index is out of range (
     *             <tt>index &lt; 0 || index &gt; size()</tt>)
     * @since 4.3
     */
    @Override
    public void add(final int index, final Object element) {
        this.all.add(index, (URI) element);
        this.unique.add((URI) element);
    }

    /**
     * Removes the URI at the specified position in this list. Shifts any
     * subsequent URIs to the left (subtracts one from their indices). Returns
     * the URI that was removed from the list.
     *
     * @param index
     *            the index of the URI to be removed
     * @return the URI previously at the specified position
     * @throws IndexOutOfBoundsException
     *             if the index is out of range (
     *             <tt>index &lt; 0 || index &gt;= size()</tt>)
     * @since 4.3
     */
    @Override
    public URI remove(final int index) {
        final URI removed = this.all.remove(index);
        this.unique.remove(removed);
        if (this.all.size() != this.unique.size()) {
            this.unique.addAll(this.all);
        }
        return removed;
    }

    /**
     * Returns <tt>true</tt> if this collection contains the specified element.
     * More formally, returns <tt>true</tt> if and only if this collection
     * contains at least one element <tt>e</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
     *
     * @param o element whose presence in this collection is to be tested
     * @return <tt>true</tt> if this collection contains the specified
     *         element
     */
    @Override
    public boolean contains(final Object o) {
        return this.unique.contains(o);
    }

}