2009-04-06 16:34:33 -04:00
|
|
|
|
|
|
|
package net.sourceforge.filebot.format;
|
|
|
|
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
import groovy.lang.GroovyObjectSupport;
|
2009-09-22 17:23:39 -04:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
import java.util.AbstractMap;
|
|
|
|
import java.util.AbstractSet;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2009-04-06 16:34:33 -04:00
|
|
|
import java.util.Map;
|
2009-06-29 12:21:56 -04:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.TreeSet;
|
2009-04-06 16:34:33 -04:00
|
|
|
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
public class AssociativeScriptObject extends GroovyObjectSupport {
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
private final Map<?, ?> properties;
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
|
|
|
public AssociativeScriptObject(Map<?, ?> properties) {
|
2009-08-02 07:48:45 -04:00
|
|
|
this.properties = new LenientLookup(properties);
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-04-06 16:34:33 -04:00
|
|
|
/**
|
|
|
|
* Get the property with the given name.
|
|
|
|
*
|
|
|
|
* @param name the property name
|
|
|
|
* @param start the object where the lookup began
|
|
|
|
*/
|
2009-09-22 17:23:39 -04:00
|
|
|
@Override
|
|
|
|
public Object getProperty(String name) {
|
2012-07-07 23:09:42 -04:00
|
|
|
return properties.get(name);
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-09-22 17:23:39 -04:00
|
|
|
@Override
|
|
|
|
public void setProperty(String name, Object value) {
|
|
|
|
// ignore, object is immutable
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2009-06-29 12:21:56 -04:00
|
|
|
// all the properties in alphabetic order
|
2011-12-22 14:36:31 -05:00
|
|
|
return new TreeSet<Object>(properties.keySet()).toString();
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
/**
|
|
|
|
* Map allowing look-up of values by a fault-tolerant key as specified by the defining key.
|
|
|
|
*
|
|
|
|
*/
|
2011-12-22 14:36:31 -05:00
|
|
|
private static class LenientLookup extends AbstractMap<Object, Object> {
|
2009-06-29 12:21:56 -04:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
private final Map<String, Entry<?, ?>> lookup = new HashMap<String, Entry<?, ?>>();
|
2009-06-29 12:21:56 -04:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
|
|
|
public LenientLookup(Map<?, ?> source) {
|
2009-08-02 07:48:45 -04:00
|
|
|
// populate lookup map
|
2011-12-22 14:36:31 -05:00
|
|
|
for (Entry<?, ?> entry : source.entrySet()) {
|
2009-08-02 07:48:45 -04:00
|
|
|
lookup.put(definingKey(entry.getKey()), entry);
|
2009-06-29 12:21:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
protected String definingKey(Object key) {
|
|
|
|
// letters and digits are defining, everything else will be ignored
|
|
|
|
return key.toString().replaceAll("[^\\p{Alnum}]", "").toLowerCase();
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
@Override
|
|
|
|
public boolean containsKey(Object key) {
|
2009-08-02 07:48:45 -04:00
|
|
|
return lookup.containsKey(definingKey(key));
|
2009-06-29 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
@Override
|
|
|
|
public Object get(Object key) {
|
2011-12-22 14:36:31 -05:00
|
|
|
Entry<?, ?> entry = lookup.get(definingKey(key));
|
2009-06-29 12:21:56 -04:00
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
return entry.getValue();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
@Override
|
2011-12-22 14:36:31 -05:00
|
|
|
public Set<Entry<Object, Object>> entrySet() {
|
|
|
|
return new AbstractSet<Entry<Object, Object>>() {
|
2009-06-29 12:21:56 -04:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
@SuppressWarnings("unchecked")
|
2009-06-29 12:21:56 -04:00
|
|
|
@Override
|
2011-12-22 14:36:31 -05:00
|
|
|
public Iterator<Entry<Object, Object>> iterator() {
|
|
|
|
return (Iterator) lookup.values().iterator();
|
2009-06-29 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
@Override
|
|
|
|
public int size() {
|
2009-08-02 07:48:45 -04:00
|
|
|
return lookup.size();
|
2009-06-29 12:21:56 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|