Skip to content

Instantly share code, notes, and snippets.

@kamikat
Last active December 17, 2015 12:09
Show Gist options
  • Save kamikat/5607705 to your computer and use it in GitHub Desktop.
Save kamikat/5607705 to your computer and use it in GitHub Desktop.
Java, "auto" class binding wrapper for Map class
import java.utils.Map;
public class ABMap implements Map {
private final Map src;
public ABMap(Map src) {
this.src = src;
}
public T <T> getAny(Object key) {
Object value = src.get(key);
if(null != value) {
return (T) value;
}else{
return null;
}
}
// Redirect all invoke to Map interface to `src`
}

Usage

Map<String, Object> anymap = new ABMap(new HashMap());

anymap.put("a", 1);
anymap.put("b", "abc");

String b = anymap.getAny("b");

assert b == "abc";

int a = anymap.getAny("a");

assert a == 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment