experimantal in memory jid caching

This commit is contained in:
iNPUTmice 2015-03-04 19:56:24 +01:00
parent ac577fe4fd
commit e10c4e78f1

View file

@ -1,5 +1,7 @@
package eu.siacs.conversations.xmpp.jid; package eu.siacs.conversations.xmpp.jid;
import android.util.LruCache;
import net.java.otr4j.session.SessionID; import net.java.otr4j.session.SessionID;
import java.net.IDN; import java.net.IDN;
@ -12,6 +14,8 @@ import gnu.inet.encoding.StringprepException;
*/ */
public final class Jid { public final class Jid {
private static LruCache<String,Jid> cache = new LruCache<>(1024);
private final String localpart; private final String localpart;
private final String domainpart; private final String domainpart;
private final String resourcepart; private final String resourcepart;
@ -62,6 +66,15 @@ public final class Jid {
private Jid(final String jid) throws InvalidJidException { private Jid(final String jid) throws InvalidJidException {
if (jid == null) throw new InvalidJidException(InvalidJidException.IS_NULL); if (jid == null) throw new InvalidJidException(InvalidJidException.IS_NULL);
Jid fromCache = Jid.cache.get(jid);
if (fromCache != null) {
displayjid = fromCache.displayjid;
localpart = fromCache.localpart;
domainpart = fromCache.domainpart;
resourcepart = fromCache.resourcepart;
return;
}
// Hackish Android way to count the number of chars in a string... should work everywhere. // Hackish Android way to count the number of chars in a string... should work everywhere.
final int atCount = jid.length() - jid.replace("@", "").length(); final int atCount = jid.length() - jid.replace("@", "").length();
final int slashCount = jid.length() - jid.replace("/", "").length(); final int slashCount = jid.length() - jid.replace("/", "").length();
@ -141,6 +154,8 @@ public final class Jid {
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH); throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
} }
Jid.cache.put(jid,this);
this.displayjid = finaljid; this.displayjid = finaljid;
} }