001package io.ebeanservice.docstore.api.mapping; 002 003import io.ebean.annotation.DocMapping; 004 005import java.util.ArrayList; 006import java.util.List; 007 008/** 009 * Property mapping in a doc store document structure. 010 */ 011public class DocPropertyMapping { 012 013 private String name; 014 015 private DocPropertyType type; 016 017 private DocPropertyOptions options; 018 019 private List<DocPropertyMapping> children = new ArrayList<>(); 020 021 /** 022 * Construct ROOT. 023 */ 024 public DocPropertyMapping() { 025 this.type = DocPropertyType.ROOT; 026 } 027 028 /** 029 * Construct property mapping. 030 */ 031 public DocPropertyMapping(String name, DocPropertyType type) { 032 this.type = type; 033 this.name = name; 034 this.options = new DocPropertyOptions(); 035 } 036 037 /** 038 * Construct property mapping with options. 039 */ 040 public DocPropertyMapping(String name, DocPropertyType type, DocPropertyOptions options) { 041 this.name = name; 042 this.type = type; 043 this.options = options; 044 } 045 046 /** 047 * Visit this property and any nested children. 048 */ 049 public void visit(DocPropertyVisitor visitor) { 050 switch (type) { 051 case ROOT: 052 visitor.visitBegin(); 053 visitChildren(visitor); 054 visitor.visitEnd(); 055 break; 056 case OBJECT: 057 visitor.visitBeginObject(this); 058 visitChildren(visitor); 059 visitor.visitEndObject(this); 060 break; 061 case LIST: 062 visitor.visitBeginList(this); 063 visitChildren(visitor); 064 visitor.visitEndList(this); 065 break; 066 default: 067 visitor.visitProperty(this); 068 } 069 } 070 071 private void visitChildren(DocPropertyVisitor visitor) { 072 073 for (DocPropertyMapping property : children) { 074 property.visit(visitor); 075 } 076 } 077 078 @Override 079 public String toString() { 080 return "name:" + name + " type:" + type + " options(" + options + ")"; 081 } 082 083 /** 084 * Return the type of the property. 085 */ 086 public DocPropertyType getType() { 087 return type; 088 } 089 090 /** 091 * Set the type of the property. 092 */ 093 public void setType(DocPropertyType type) { 094 this.type = type; 095 } 096 097 /** 098 * Return the property name. 099 */ 100 public String getName() { 101 return name; 102 } 103 104 /** 105 * Return the property options. 106 */ 107 public DocPropertyOptions getOptions() { 108 return options; 109 } 110 111 /** 112 * Return the child nested properties. 113 */ 114 public List<DocPropertyMapping> getChildren() { 115 return children; 116 } 117 118 /** 119 * Add a child property. 120 */ 121 public void addChild(DocPropertyMapping docMapping) { 122 children.add(docMapping); 123 } 124 125 /** 126 * Apply mapping options to this property. 127 */ 128 public void apply(DocMapping docMapping) { 129 options.apply(docMapping); 130 } 131}