RLPark 1.0.0
Reinforcement Learning Framework in Java

CollectionLabelBuilder.java

Go to the documentation of this file.
00001 package zephyr.plugin.core.api.internal.parsing;
00002 
00003 import zephyr.plugin.core.api.monitoring.abstracts.LabeledCollection;
00004 
00005 
00006 public class CollectionLabelBuilder {
00007   private final String collectionPattern;
00008   private final LabeledCollection labeledElement;
00009   private final String elementLabelSeparator;
00010   private final boolean arrayDecoration;
00011   private String[] labels;
00012 
00013   public CollectionLabelBuilder(LabelBuilder labelBuilder, int size, String label, String id, boolean arrayDecoration) {
00014     collectionPattern = CollectionLabelBuilder.collectionPattern(label, size, arrayDecoration);
00015     labeledElement = labelBuilder.getLabeledElement(id);
00016     elementLabelSeparator = labelBuilder.elementLabelSeparator();
00017     this.arrayDecoration = arrayDecoration;
00018   }
00019 
00020   public CollectionLabelBuilder(LabeledCollection labeledElement, String elementLabelSeparator, int size,
00021       boolean arrayDecoration) {
00022     collectionPattern = CollectionLabelBuilder.collectionPattern("", size, arrayDecoration);
00023     this.labeledElement = labeledElement;
00024     this.elementLabelSeparator = elementLabelSeparator;
00025     this.arrayDecoration = arrayDecoration;
00026   }
00027 
00028   public String elementLabel(int index) {
00029     String suffix = labeledElement == null ? null : labeledElement.label(index);
00030     if (arrayDecoration && suffix != null)
00031       suffix = elementLabelSeparator + suffix;
00032     return CollectionLabelBuilder.collectionLabel(collectionPattern, index, suffix, arrayDecoration);
00033   }
00034 
00035   public int indexOf(String label, int size) {
00036     createLabels(size);
00037     for (int i = 0; i < labels.length; i++)
00038       if (labels[i].equals(label))
00039         return i;
00040     return -1;
00041   }
00042 
00043   public String[] createLabels(int size) {
00044     if (labels != null)
00045       return labels;
00046     labels = new String[size];
00047     for (int i = 0; i < size; i++)
00048       labels[i] = elementLabel(i);
00049     return labels;
00050   }
00051 
00052   public static String collectionLabel(String collectionPattern, int index, String suffix, boolean arrayDecoration) {
00053     StringBuilder result = arrayDecoration ?
00054         new StringBuilder(String.format(collectionPattern, index)) :
00055         new StringBuilder(collectionPattern);
00056     if (suffix != null)
00057       result.append(suffix);
00058     if (arrayDecoration)
00059       result.append("]");
00060     return result.toString();
00061   }
00062 
00063   public static String collectionPattern(String collectionLabel, int size, boolean arrayDecoration) {
00064     if (!arrayDecoration)
00065       return collectionLabel;
00066     if (size < 10)
00067       return String.format("%s[%%d", collectionLabel);
00068     return String.format("%s[%%0%dd", collectionLabel, (int) Math.ceil(Math.log10(size)));
00069   }
00070 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark