001package io.ebean.enhance.common;
002
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006
007/**
008 * Collects the annotation information.
009 */
010public class AnnotationInfo {
011
012  private final HashMap<String,Object> valueMap = new HashMap<>();
013
014  private AnnotationInfo parent;
015
016  /**
017  * The parent is typically the class level annotation information
018  * which could be considered to hold default values.
019  */
020  public AnnotationInfo(AnnotationInfo parent){
021    this.parent = parent;
022  }
023
024  @Override
025  public String toString() {
026    return valueMap.toString();
027  }
028
029  public void setParent(AnnotationInfo parent) {
030    this.parent = parent;
031  }
032
033  /**
034   * Gets or creates a list for the given prefix, it will hold the array values.
035   */
036  @SuppressWarnings("unchecked")
037  public List<Object> getArrayEntry(String prefix) {
038    return (List<Object>) valueMap.computeIfAbsent(prefix, k -> new ArrayList<>());
039  }
040
041  /**
042  * Add a annotation value.
043  */
044  public void add(String prefix, String name, Object value){
045    if (name == null){
046      // this is an array value...
047      getArrayEntry(prefix).add(value);
048    } else {
049      String key = getKey(prefix, name);
050      valueMap.put(key, value);
051    }
052  }
053
054  /**
055  * Add a enum annotation value.
056  */
057  void addEnum(String prefix, String name, String value){
058    add(prefix, name, value);
059  }
060
061  private String getKey(String prefix, String name){
062    if (prefix == null){
063      return name;
064    } else {
065      return prefix+"."+name;
066    }
067  }
068
069  /**
070  * Return a value out of the map.
071  */
072  public Object getValue(String key){
073    Object o = valueMap.get(key);
074    if (o == null && parent != null){
075      // try getting value from parent
076      o = parent.getValue(key);
077    }
078    return o;
079  }
080}