From 5f073f91241e58214e87db7c044ef015fd8e99ba Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 29 Jun 2009 16:21:56 +0000 Subject: [PATCH] * access mapped values on demand only --- .../format/AssociativeScriptObject.java | 100 +++++++++++++----- 1 file changed, 74 insertions(+), 26 deletions(-) diff --git a/source/net/sourceforge/filebot/format/AssociativeScriptObject.java b/source/net/sourceforge/filebot/format/AssociativeScriptObject.java index d8e53ac2..a4bd2a8b 100644 --- a/source/net/sourceforge/filebot/format/AssociativeScriptObject.java +++ b/source/net/sourceforge/filebot/format/AssociativeScriptObject.java @@ -2,40 +2,25 @@ package net.sourceforge.filebot.format; -import java.util.Comparator; +import java.util.AbstractMap; +import java.util.AbstractSet; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; -import java.util.TreeMap; +import java.util.Set; +import java.util.TreeSet; import org.mozilla.javascript.Scriptable; public class AssociativeScriptObject implements Scriptable { - /** - * Map allowing look-up of values by a fault-tolerant key as specified by the defining key. - * - * @see {@link #definingKey(String)} - */ - protected final TreeMap properties = new TreeMap(new Comparator() { - - @Override - public int compare(String s1, String s2) { - return definingKey(s1).compareTo(definingKey(s2)); - } - }); - - - /** - * The Java constructor - */ - public AssociativeScriptObject(Map properties) { - this.properties.putAll(properties); - } + private final Map properties; - protected String definingKey(String s) { - // letters and digits are defining, everything else will be ignored - return s.replaceAll("\\p{Punct}", "").toLowerCase(); + @SuppressWarnings("unchecked") + public AssociativeScriptObject(Map properties) { + this.properties = new LenientLookup((Map) properties); } @@ -117,7 +102,8 @@ public class AssociativeScriptObject implements Scriptable { @Override public String toString() { - return getClassName() + properties.entrySet().toString(); + // all the properties in alphabetic order + return new TreeSet(properties.keySet()).toString(); } @@ -165,4 +151,66 @@ public class AssociativeScriptObject implements Scriptable { return false; } + + /** + * Map allowing look-up of values by a fault-tolerant key as specified by the defining key. + * + */ + protected class LenientLookup extends AbstractMap { + + private final Map> source; + + + public LenientLookup(Map source) { + // initialize source map + this.source = new HashMap>(source.size()); + + // populate source map + for (Entry entry : source.entrySet()) { + this.source.put(definingKey(entry.getKey()), entry); + } + } + + + protected String definingKey(Object key) { + // letters and digits are defining, everything else will be ignored + return key.toString().replaceAll("[^\\p{Alnum}]", "").toLowerCase(); + } + + + @Override + public boolean containsKey(Object key) { + return source.containsKey(definingKey(key)); + } + + + @Override + public Object get(Object key) { + Entry entry = source.get(definingKey(key)); + + if (entry != null) + return entry.getValue(); + + return null; + } + + + @Override + public Set> entrySet() { + return new AbstractSet>() { + + @Override + public Iterator> iterator() { + return source.values().iterator(); + } + + + @Override + public int size() { + return source.size(); + } + }; + } + } + }