HTTPCLIENT-1351: Made URICollection a concrete class; added #clear() method
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1487448 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d917eec043
commit
2bd70ff44c
|
@ -28,24 +28,86 @@
|
|||
package org.apache.http.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
|
||||
/**
|
||||
* This class represents an iterable collection of {@link java.net.URI} locations.
|
||||
* This class represents an iterable collection of {@link URI}s.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
public interface URICollection extends Iterable<URI> {
|
||||
@NotThreadSafe // HashSet is not synch.
|
||||
public class URICollection implements Iterable<URI> {
|
||||
|
||||
int getCount();
|
||||
private final Set<URI> unique;
|
||||
private final List<URI> all;
|
||||
|
||||
boolean isEmpty();
|
||||
public URICollection() {
|
||||
super();
|
||||
this.unique = new HashSet<URI>();
|
||||
this.all = new ArrayList<URI>();
|
||||
}
|
||||
|
||||
boolean contains(URI uri);
|
||||
/**
|
||||
* Test if the URI is present in the collection.
|
||||
*/
|
||||
public boolean contains(final URI uri) {
|
||||
return this.unique.contains(uri);
|
||||
}
|
||||
|
||||
Set<URI> getUnique();
|
||||
/**
|
||||
* Adds a new URI to the collection.
|
||||
*/
|
||||
public void add(final URI uri) {
|
||||
this.unique.add(uri);
|
||||
this.all.add(uri);
|
||||
}
|
||||
|
||||
List<URI> getAll();
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
public List<URI> getAll() {
|
||||
return new ArrayList<URI>(this.all);
|
||||
}
|
||||
|
||||
public Set<URI> getUnique() {
|
||||
return new HashSet<URI>(this.unique);
|
||||
}
|
||||
|
||||
public Iterator<URI> iterator() {
|
||||
return getAll().iterator();
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return this.all.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.all.isEmpty();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.all.clear();
|
||||
this.unique.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,102 +27,16 @@
|
|||
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
import org.apache.http.client.URICollection;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* This class represents a collection of {@link URI}s used as redirect locations.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
@NotThreadSafe // HashSet is not synch.
|
||||
public class RedirectLocations implements URICollection {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public Set<URI> getUnique() {
|
||||
return new HashSet<URI>(this.unique);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public Iterator<URI> iterator() {
|
||||
return getAll().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public int getCount() {
|
||||
return this.all.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return this.all.isEmpty();
|
||||
}
|
||||
|
||||
@NotThreadSafe
|
||||
public class RedirectLocations extends URICollection {
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.apache.http.HttpResponse;
|
|||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.client.URICollection;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
|
@ -282,8 +283,7 @@ public class TestDefaultRedirectStrategy {
|
|||
Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
|
||||
Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
|
||||
|
||||
final RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
|
||||
DefaultRedirectStrategy.REDIRECT_LOCATIONS);
|
||||
final URICollection redirectLocations = context.getRedirectLocations();
|
||||
Assert.assertNotNull(redirectLocations);
|
||||
Assert.assertTrue(redirectLocations.contains(uri));
|
||||
final List<URI> uris = redirectLocations.getAll();
|
||||
|
|
Loading…
Reference in New Issue