Skip to content

Instantly share code, notes, and snippets.

@jakubjanecek
Created February 10, 2013 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubjanecek/4749536 to your computer and use it in GitHub Desktop.
Save jakubjanecek/4749536 to your computer and use it in GitHub Desktop.
private static int genericSize(Object obj) {
if (obj instanceof String) {
String s = (String) obj;
return s.length();
} else if (obj instanceof int[]) {
int[] a = (int[]) obj;
return a.length;
} else if (obj instanceof List) {
List l = (List) obj;
return l.size();
} else if (obj instanceof ObjectWithSize) {
ObjectWithSize o = (ObjectWithSize) obj;
return o.count();
} else {
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment