xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
- From: norwoods <norwoods AT gbronline.com>
- Cc: xom-interest AT lists.ibiblio.org
- Subject: Re: [XOM-interest] QName flyweights
- Date: Mon, 13 Jun 2005 08:29:39 -0700
here is another attempt at a cache. this uses an arraylist with binary search for the matching entry. it can return a QName object with the methods qName=QName3.newQName(name, uri); or qName=QName3.newQName(QName3.qualifyName(prefix, localName), uri);
norwod sisson
****************************
package nu.xom;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
final class QName2 {
static ArrayList cache = new ArrayList();
private static final int trimDelayCount = 3;
private static int trimDelayCounter = 0;
private String localName;
private String name;
private String prefix;
private String uri;
private WeakReference reference;
private QName2(String name, String uri) {
this.name = name.intern();
this.uri = uri.intern();
}
public static synchronized QName2 newQName(String name, String uri) {
if (name == null) {
throw new NullPointerException();
}
if (uri == null)
uri = "";
int high = cache.size(), low = -1, probe;
Reference reference;
QName2 value;
while (high - low > 1) {
probe = (high + low) / 2;
reference = (WeakReference) cache.get(probe);
value = (QName2) reference.get();
if (value == null) {
cache.remove(probe);
high--;
break;
}
if (value.getQualifiedName().compareTo(name) > 0
&& (value.getNamespaceURI().compareTo(uri)) > 0)
high = probe;
else
low = probe;
}
if (low > -1) {
reference = (WeakReference) cache.get(low);
value = (QName2) reference.get();
if ((value.getQualifiedName().compareTo(name) == 0)
&& (value.getNamespaceURI().compareTo(uri) == 0))
return value;
}
value = new QName2(name, uri);
value.reference = new WeakReference(value);
cache.add(high, value.reference);
return value;
}
private void splitQualifiedName() {
int i = name.indexOf(':');
if (i < 0) {
prefix = "".intern();
localName = name.intern();
} else {
prefix = name.substring(0, i).intern();
localName = name.substring(i + 1).intern();
}
}
public boolean equals(QName2 qName) {
if (this == qName)
return true;
return (name.equals(qName.getQualifiedName()) && (uri.equals(qName
.getNamespaceURI())));
}
protected synchronized void finalize() {
cache.remove(reference);
reference.clear();
reference = null;
if (++trimDelayCounter >= trimDelayCount) {
cache.trimToSize();
trimDelayCounter = 0;
}
}
public String getLocalName() {
if (localName == null)
splitQualifiedName();
return localName;
}
public String getNamespacePrefix() {
if (prefix == null)
splitQualifiedName();
return prefix;
}
public String getNamespaceURI() {
return uri;
}
public String getQualifiedName() {
return name;
}
public int hashCode() {
return uri.hashCode() ^ name.hashCode();
}
public static String qualifyName(String prefix, String localName) {
if (prefix == null || prefix == "")
return localName;
return prefix + ":" + localName;
}
}
-
[XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/09/2005
- Re: [XOM-interest] QName flyweights, Elliotte Harold, 06/09/2005
-
Re: [XOM-interest] QName flyweights,
norwoods, 06/09/2005
-
Re: [XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/09/2005
- Re: [XOM-interest] QName flyweights, Wolfgang Hoschek, 06/09/2005
-
Re: [XOM-interest] QName flyweights,
Metalab, 06/09/2005
-
Re: [XOM-interest] QName flyweights,
Mik Lernout, 06/10/2005
-
Re: [XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/10/2005
-
Re: [XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/10/2005
- Re: [XOM-interest] QName flyweights, norwoods, 06/13/2005
- Re: [XOM-interest] QName flyweights, norwoods, 06/13/2005
- Re: [XOM-interest] QName flyweights, Wolfgang Hoschek, 06/13/2005
- Message not available
- Re: [XOM-interest] QName flyweights, Wolfgang Hoschek, 06/13/2005
-
Re: [XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/10/2005
-
Re: [XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/10/2005
-
Re: [XOM-interest] QName flyweights,
Mik Lernout, 06/10/2005
-
Re: [XOM-interest] QName flyweights,
Wolfgang Hoschek, 06/09/2005
- Re: [XOM-interest] QName flyweights, norwoods, 06/10/2005
Archive powered by MHonArc 2.6.24.