View Javadoc
1   /*
2    * Copyright (C) 2016 Uwe Plonus
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.sw4j.tool.annotation.jpa.generator.model;
18  
19  import java.util.Collections;
20  import java.util.LinkedList;
21  import java.util.List;
22  import javax.annotation.Nonnull;
23  import javax.xml.bind.annotation.XmlAccessType;
24  import javax.xml.bind.annotation.XmlAccessorType;
25  import javax.xml.bind.annotation.XmlAttribute;
26  import javax.xml.bind.annotation.XmlElement;
27  import javax.xml.bind.annotation.XmlType;
28  
29  /**
30   * This class represents an entity. Via the entity you can access the attributes and tables assigned to the entity.
31   *
32   * <p>Each entity must have at least one table and one attribute.</p>
33   *
34   * @author Uwe Plonus
35   */
36  @XmlAccessorType(XmlAccessType.NONE)
37  @XmlType(name = "",
38          propOrder = {
39              "tables",
40              "attributes",
41          })
42  public class Entity {
43  
44      /** The name of the entity. */
45      private final String name;
46  
47      /** The class name of the entity. */
48      private final String className;
49  
50      /** The attributes of this entity. */
51      private List<Attribute> attributes;
52  
53      /** The tables of this entity. */
54      private List<Table> tables;
55  
56      /**
57       * Constructor for an entity.
58       *
59       * @param name the name of the entity.
60       * @param className the class name of the entity.
61       */
62      public Entity(@Nonnull final String name, @Nonnull final String className) {
63          this.name = name;
64          this.className = className;
65      }
66  
67      /**
68       * Returns the name of the entity.
69       *
70       * @return the name.
71       */
72      @Nonnull
73      @XmlAttribute(name = "name", required = true)
74      public String getName() {
75          return this.name;
76      }
77  
78      /**
79       * Returns the class name of the entity.
80       *
81       * @return the class name.
82       */
83      @Nonnull
84      @XmlAttribute(name = "className", required = true)
85      public String getClassName() {
86          return this.className;
87      }
88  
89      /**
90       * Adds an attribute to the entity.
91       *
92       * @param attribute the attribute to add.
93       */
94      public synchronized void addAttribute(@Nonnull final Attribute attribute) {
95          if (this.attributes == null) {
96              this.attributes = new LinkedList<>();
97          }
98          this.attributes.add(attribute);
99      }
100 
101     /**
102      * Returns all attributes of the entity.
103      *
104      * @return a list containing all attributes of the entity.
105      */
106     @Nonnull
107     @XmlElement(name = "attribute")
108     public synchronized List<Attribute> getAttributes() {
109         return this.attributes == null ? Collections.EMPTY_LIST : Collections.unmodifiableList(this.attributes);
110     }
111 
112     /**
113      * Adds a table to the entity.
114      *
115      * @param table the table to add.
116      */
117     public synchronized void addTable(@Nonnull final Table table) {
118         if (this.tables == null) {
119             this.tables = new LinkedList<>();
120         }
121         this.tables.add(table);
122     }
123 
124     /**
125      * Returns all tables of the entity. The primary table of the entity is always the first table in the returned list.
126      *
127      * @return a list containing all tables of the entity.
128      */
129     @Nonnull
130     @XmlElement(name = "table")
131     public synchronized List<Table> getTables() {
132         return this.tables == null ? Collections.EMPTY_LIST : Collections.unmodifiableList(this.tables);
133     }
134 
135 }