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 //System.out.println("addValue "+key+" value:"+value); 051 valueMap.put(key, value); 052 } 053 } 054 055 /** 056 * Add a enum annotation value. 057 */ 058 void addEnum(String prefix, String name, String value){ 059 add(prefix, name, value); 060 } 061 062 private String getKey(String prefix, String name){ 063 if (prefix == null){ 064 return name; 065 } else { 066 return prefix+"."+name; 067 } 068 } 069 070 /** 071 * Return a value out of the map. 072 */ 073 public Object getValue(String key){ 074 Object o = valueMap.get(key); 075 if (o == null && parent != null){ 076 // try getting value from parent 077 o = parent.getValue(key); 078 } 079 return o; 080 } 081}