RLPark 1.0.0
Reinforcement Learning Framework in Java

AbstractCollectionParser.java

Go to the documentation of this file.
00001 package zephyr.plugin.core.api.internal.codeparser.parsers;
00002 
00003 import java.lang.reflect.Field;
00004 import zephyr.plugin.core.api.internal.codeparser.codetree.ClassNode;
00005 import zephyr.plugin.core.api.internal.codeparser.codetree.CodeTrees;
00006 import zephyr.plugin.core.api.internal.codeparser.codetree.ObjectCollectionNode;
00007 import zephyr.plugin.core.api.internal.codeparser.interfaces.CodeNode;
00008 import zephyr.plugin.core.api.internal.codeparser.interfaces.CodeParser;
00009 import zephyr.plugin.core.api.internal.codeparser.interfaces.FieldParser;
00010 import zephyr.plugin.core.api.internal.codeparser.interfaces.MutableParentNode;
00011 import zephyr.plugin.core.api.internal.parsing.CollectionLabelBuilder;
00012 
00013 public abstract class AbstractCollectionParser<T> implements FieldParser {
00014   @Override
00015   public CodeNode parse(CodeParser codeParser, MutableParentNode parentNode, Field instanceField, String instanceLabel,
00016       Object instance) {
00017     @SuppressWarnings("unchecked")
00018     T container = (T) instance;
00019     int nbChildren = nbChildren(container);
00020     String label = instanceField != null ? instanceField.getName() : "";
00021     CollectionLabelBuilder labelBuilder = codeParser.newCollectionLabelBuilder(instanceField, nbChildren);
00022     ObjectCollectionNode collectionNode = new ObjectCollectionNode(label, parentNode, instance, instanceField);
00023     parentNode.addChild(collectionNode);
00024     beginChildrenParse(container);
00025     for (int i = 0; i < nbChildren; i++) {
00026       Object element = getElement(container, i);
00027       if (element == null)
00028         continue;
00029       String elementLabel = labelBuilder.elementLabel(i);
00030       parseElement(codeParser, collectionNode, elementLabel, element, instanceField);
00031     }
00032     endChildrenParse();
00033     return collectionNode;
00034   }
00035 
00036   protected void beginChildrenParse(T fieldValue) {
00037   }
00038 
00039   protected void endChildrenParse() {
00040   }
00041 
00042   private static void parseElement(CodeParser codeParser, MutableParentNode parentNode, String elementLabel,
00043       Object element, Field field) {
00044     ClassNode childNode;
00045     if (element.getClass().isArray()) {
00046       childNode = new ObjectCollectionNode(elementLabel, parentNode, element, field);
00047       codeParser.recursiveParseInstance(childNode, null, null, element);
00048     } else {
00049       childNode = new ClassNode(elementLabel, parentNode, element, field);
00050       codeParser.recursiveParseClass(childNode, element);
00051     }
00052     parentNode.addChild(childNode);
00053     CodeTrees.popupIFN(codeParser, field, childNode);
00054   }
00055 
00056   abstract protected int nbChildren(T container);
00057 
00058   abstract protected Object getElement(T container, int index);
00059 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark