1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package trees;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;
public class MyTree {
// Key of Hashmap is Class, the values are the Class that inherit directly from Key
private HashMap <Class, HashSet<Class>> inheritance = new HashMap<Class, HashSet<Class>>();
/**
* This function adds a given Class type to inheritance HashMap
* It create an entry with it's superclass if none exists, or is added to convenient HashSet
* If type is already taken into account, nothing happens
* @param type : Class type to be added to inheritance attribute
*/
@SuppressWarnings("unchecked")
public void setInherits(Class type){
HashSet<Class> v;
// if key does not exist, create a new empty HashSet, to be filled below
if (getInheritance(type.getSuperclass()) == null)
inheritance.put(type.getSuperclass(), new HashSet<Class>());
// get the HashSet corresponding to the given Key
v=inheritance.get(type.getSuperclass());
// adds type to HashMap, before adding modified (or not) HashSet to HashMap
v.add(type);
inheritance.put(type.getSuperclass(), v);
}
/**
* Get all Class types that directly inherit from given type
* @param type : Class whose children are wanted
* @return : A HashSet that contains all children from parameter Class type, null if there are no children
*/
@SuppressWarnings("unchecked")
public HashSet<Class> getInheritance(Class type){
HashSet<Class> hs = null;
if (inheritance.containsKey(type))
hs = inheritance.get(type);
return hs;
}
} |