All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.webdav.client.URLTable Maven / Gradle / Ivy

Go to download

We build this plugin because eclipse no longer distributes it and Guvnor tools needs it.

There is a newer version: 7.48.0.Final
Show newest version
package org.eclipse.webdav.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import org.eclipse.webdav.internal.kernel.utils.Assert;

/**
 * A URLTable is a simple hashtable whose keys are
 * URLs. A URL key with a trailing slash is
 * considered by the table to be equal to the same URL
 * without a trailing slash.
 * 

* Note: This class/interface is part of an interim API that is still under * development and expected to change significantly before reaching stability. * It is being made available at this early stage to solicit feedback from pioneering * adopters on the understanding that any code that uses this API will almost * certainly be broken (repeatedly) as the API evolves. *

*/ public class URLTable { private Hashtable table; /** * A URLKey is an equality wrapper class for * URLs. The wrapper treats URLs with and * without a trailing slash as equal. The equals method * works with URLKeys, URLs, and * Strings. *

* Note: This class/interface is part of an interim API that is still under * development and expected to change significantly before reaching stability. * It is being made available at this early stage to solicit feedback from pioneering * adopters on the understanding that any code that uses this API will almost * certainly be broken (repeatedly) as the API evolves. *

*/ class URLKey { URL url; int hashCode = -1; /** * Creates a new URLKey from the given URL. * * @param url the URL to wrap */ public URLKey(URL url) { this.url = url; } /** * Returns true if this URLKey is equal to the * given object. Returns false otherwise. The object may be * a URLKey, String, or URL. * * @param obj the object to compare with this URLKey * @return a boolean indicating whether or not this * URLKey is equal to the given object */ public boolean equals(Object obj) { if (obj == null) return false; if (this == obj) return true; if (obj instanceof URLKey) return equals(((URLKey) obj).getURL()); if (obj instanceof String) { try { return equals(new URL((String) obj)); } catch (MalformedURLException e) { return false; } } if (!(obj instanceof URL)) return false; if (url == (URL) obj) return true; URL url1 = URLTool.removeTrailingSlash(url); URL url2 = URLTool.removeTrailingSlash((URL) obj); return url1.equals(url2); } /** * Returns the URL that this URLKey wraps. * * @return the URL that this URLKey wraps */ public URL getURL() { return url; } /** * Returns an integer suitable for indexing this URLKey in * a hash table. * * @return an integer suitable for hash table indexing */ public int hashCode() { if (hashCode == -1) hashCode = URLTool.removeTrailingSlash(url).hashCode(); return hashCode; } /** * Returns this URLKeys URL as a * String. * * @return a string */ public String toString() { return url.toString(); } } // end-class URLKey /** * Construct an empty URLTable. */ public URLTable() { table = new Hashtable(); } /** * Construct an empty URLTable with the given size. */ public URLTable(int size) { table = new Hashtable(size); } /** * Returns the value to which the given URL is mapped to in the table. If * the given URL not mapped to any value, or is malformed, returns * null. * * @param url a URL as a String * @return the value to which the given URL is mapped to in the table, * or null * @throws MalformedURLException if the given URL is malformed */ public Object get(String url) throws MalformedURLException { Assert.isNotNull(url); return get(new URL(url)); } /** * Returns the value to which the given URL is mapped to in * the table. If the given URL not mapped to any value, * returns null. * * @param url a URL * @return the value to which the given URL is mapped to * in the table, or null */ public Object get(URL url) { Assert.isNotNull(url); return get(new URLKey(url)); } /** * Returns the value to which the specified URL is mapped to in the * table. If the specified URL is not mapped to any value, returns * null. * * @param url a URLKey * @return the value to which the specified URL is mapped to in the * table, or null */ private Object get(URLKey url) { Assert.isNotNull(url); return table.get(url); } /** * Returns an Enumeration over the keys in this * URLTable. * * @return an Enumeration over URLs */ public Enumeration keys() { final Enumeration keys = table.keys(); Enumeration e = new Enumeration() { public boolean hasMoreElements() { return keys.hasMoreElements(); } public Object nextElement() { return ((URLKey) keys.nextElement()).getURL(); } }; return e; } /** * Maps the given URL to the given value in this table. * * @param url a URL as a String * @param value an object * @throws MalformedURLException if the given URL is malformed */ public void put(String url, Object value) throws MalformedURLException { Assert.isNotNull(url); Assert.isNotNull(value); put(new URL(url), value); } /** * Maps the given URL to the given value in this table. * * @param url a URL * @param value an object */ public void put(URL url, Object value) { Assert.isNotNull(url); Assert.isNotNull(value); put(new URLKey(url), value); } /** * Maps the specified URL to the given value in this table. * * @param url a URLKey * @param value an object */ private void put(URLKey url, Object value) { Assert.isNotNull(url); Assert.isNotNull(value); // Remove the old entry so the url key is replaced if (table.get(url) != null) table.remove(url); table.put(url, value); } public void remove(String url) throws MalformedURLException { Assert.isNotNull(url); remove(new URL(url)); } public void remove(URL url) { Assert.isNotNull(url); remove(new URLKey(url)); } private void remove(URLKey url) { Assert.isNotNull(url); table.remove(url); } }