Alignment with type hierarchy

Suppose we have a hierarchy of a fixed type, for example. as shown below. This is a well-defined tree, where each node has one parent (except the root).

Human Taxonomy

Each type has an action associated with it that must be performed upon successful matching. This means that it does not mean that the action matches the method of the specified type. This is just an arbitrary association.

What are smart ways to map objects to a type hierarchy? Each object must be mapped to the most specific type. Objects already created.

+4
source share
2 answers

.

, , .

:

    class MatchContext {
         public int level;
         public Node result;
    }

    public boolean match(Node object, int level, MatchContext ctx) {
        if (no match)
            return false;
        boolean found = false;
        for (all children in object) {
            if (match(child, level + 1, ctx))
                found = true;
        }
        if (!found && level > ctx.level) {
            ctx.level = level;
            ctx.result = this;
        }
        return found;
    }

:

    MatchContext ctx;
    if (match(root, 0, ctx))
        myAction(ctx.result);
+1

. getSuperclass() , . ( ) -.

, , enum

public enum ClassType{
    HOMINOIDEA(HOMINOIDEA.class),
    HOMINIDAE(HOMINIDAE.class),
    HOMININAE(HOMININAE.class),
    //and so on
    UNKNOWN(null);      

    private static final Map<Class<?>, ClassType> typesMap = new HashMap<>();
    public final Class<?> type;

    static{
        for (ClassType classType : EnumSet.allOf(ClassType.class)){             
            if(classType.type != null){
                typesMap.put(classType.type, classType);
            }
        }
    }

    private ClassType(Class<?> type){
        this.type = type;           
    }

    public static ClassType getClassTypeOf(Class<?> type){
        for(Class<?> lookupType = type; lookupType != null; lookupType = lookupType.getSuperclass()){
            ClassType classType = typesMap.get(lookupType);
            if(classType != null){
                return classType;
            }
        }       
        return UNKNOWN;
    }
}

:

public static void main(String[] args){
    EnumMap<ClassType, Action> actionMap = new EnumMap<>(ClassType.class);
    actionMap.put(ClassType.HOMININAE, new HomininaeAction());
    Homininae h = new Homininae();
    actionMap.get(ClassType.getClassTypeOf(h)); //action associated with homininaes     
}

public class ActionDispatcher {
    private final Map<Class<?>, Consumer<?>> actionMap = new HashMap<>();

    public <T> void registerAction(Class<T> type, Consumer<? super T> action){
        actionMap.put(type, action);
    }    

    @SuppressWarnings("unchecked")
    public void dispatchActionFor(Object object){
        Consumer<Object> action = ((Consumer<Object>)getActionFor(object.getClass()));
        if(action != null){
            action.accept(object);
        }
    }

    private Consumer<?> getActionFor(Class<?> type){        
        for(Class<?> lookupType = type; lookupType != null; lookupType = lookupType.getSuperclass()){
            Consumer<?> action = actionMap.get(lookupType);
            if(action != null){
                return action;
            }
        }       
        return null;
    }

    //demo
    public static void main(String[] args){
        ActionDispatcher dispatcher = new ActionDispatcher();
        dispatcher.registerAction(Number.class, n -> System.out.println("number: " + n));
        dispatcher.registerAction(Double.class, d -> System.out.println("double: " + d));
        dispatcher.registerAction(String.class, s -> System.out.println("first char: " + s.charAt(0)));
        dispatcher.registerAction(Object.class, o -> System.out.println("object: " + o));

        dispatcher.dispatchActionFor(new Integer(3)); 
        dispatcher.dispatchActionFor(new Double(3.0));
        dispatcher.dispatchActionFor("string");
        dispatcher.dispatchActionFor(new Thread());
    }

}

:

number: 3
double: 3.0
first char: s
object: Thread[Thread-0,5,main]
0

Source: https://habr.com/ru/post/1668628/


All Articles