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