2009-04-06 16:34:33 -04:00
|
|
|
|
|
|
|
package net.sourceforge.filebot.format;
|
|
|
|
|
|
|
|
|
2009-09-22 17:23:39 -04:00
|
|
|
import groovy.lang.GroovyObject;
|
|
|
|
import groovy.lang.MetaClass;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2009-09-22 17:23:39 -04:00
|
|
|
public class AssociativeScriptObject implements GroovyObject {
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
private final Map<String, Object> properties;
|
2009-04-06 16:34:33 -04:00
|
|
|
|
|
|
|
|
2009-06-29 12:21:56 -04:00
|
|
|
public AssociativeScriptObject(Map<String, ?> properties) {
|
2009-08-02 07:48:45 -04:00
|
|
|
this.properties = new LenientLookup(properties);
|
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) {
|
2009-04-06 16:34:33 -04:00
|
|
|
Object value = properties.get(name);
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
throw new BindingException(name, "undefined");
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-22 17:23:39 -04:00
|
|
|
@Override
|
|
|
|
public Object invokeMethod(String name, Object args) {
|
|
|
|
// ignore, object is merely a structure
|
|
|
|
return null;
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-22 17:23:39 -04:00
|
|
|
@Override
|
|
|
|
public MetaClass getMetaClass() {
|
|
|
|
return null;
|
2009-04-06 16:34:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2009-09-22 17:23:39 -04:00
|
|
|
public void setMetaClass(MetaClass clazz) {
|
|
|
|
// ignore, don't care about MetaClass
|
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
|
|
|
|
return new TreeSet<String>(properties.keySet()).toString();
|
2009-04-06 16:34:33 -04: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.
|
|
|
|
*
|
|
|
|
*/
|
2009-08-02 07:48:45 -04:00
|
|
|
private static class LenientLookup extends AbstractMap<String, Object> {
|
2009-06-29 12:21:56 -04:00
|
|
|
|
2009-08-02 07:48:45 -04:00
|
|
|
private final Map<String, Entry<String, ?>> lookup = new HashMap<String, Entry<String, ?>>();
|
2009-06-29 12:21:56 -04:00
|
|
|
|
|
|
|
|
2009-08-02 07:48:45 -04:00
|
|
|
public LenientLookup(Map<String, ?> source) {
|
|
|
|
// populate lookup map
|
|
|
|
for (Entry<String, ?> entry : source.entrySet()) {
|
|
|
|
lookup.put(definingKey(entry.getKey()), entry);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object get(Object key) {
|
2009-08-02 07:48:45 -04:00
|
|
|
Entry<String, ?> entry = lookup.get(definingKey(key));
|
2009-06-29 12:21:56 -04:00
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
return entry.getValue();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<Entry<String, Object>> entrySet() {
|
|
|
|
return new AbstractSet<Entry<String, Object>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Entry<String, Object>> iterator() {
|
2009-08-02 07:48:45 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Iterator<Entry<String, Object>> iterator = (Iterator) lookup.values().iterator();
|
|
|
|
return iterator;
|
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
|
|
|
}
|