View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.dataset;
28  
29  import com.rapiddweller.common.Named;
30  
31  import java.util.ArrayList;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  /**
37   * Defines a data set that may be nested.<br/><br/>
38   * Created: 21.03.2008 12:31:13
39   *
40   * @author Volker Bergmann
41   * @since 0.5.0
42   */
43  public class Dataset implements Named {
44  
45    // attributes ------------------------------------------------------------------------------------------------------
46  
47    private final String id;
48    private final String type;
49    private final String name;
50    private final Set<Dataset> parents;
51    private final List<Dataset> subSets;
52  
53    // constructor -----------------------------------------------------------------------------------------------------
54  
55    /**
56     * Instantiates a new Dataset.
57     *
58     * @param type the type
59     * @param name the name
60     */
61    Dataset(String type, String name) {
62      if (type == null) {
63        throw new IllegalArgumentException("type is null");
64      }
65      if (name == null) {
66        throw new IllegalArgumentException("name is null");
67      }
68      this.id = type + ':' + name;
69      this.type = type;
70      this.name = name;
71      this.parents = new HashSet<>();
72      this.subSets = new ArrayList<>();
73    }
74  
75    // interface -------------------------------------------------------------------------------------------------------
76  
77    /**
78     * Gets type.
79     *
80     * @return the type
81     */
82    public String getType() {
83      return type;
84    }
85  
86    @Override
87    public String getName() {
88      return name;
89    }
90  
91    /**
92     * Add parent.
93     *
94     * @param parent the parent
95     */
96    public void addParent(Dataset parent) {
97      this.parents.add(parent);
98    }
99  
100   /**
101    * Gets parents.
102    *
103    * @return the parents
104    */
105   public Set<Dataset> getParents() {
106     return parents;
107   }
108 
109   /**
110    * Add sub set.
111    *
112    * @param subSet the sub set
113    */
114   public void addSubSet(Dataset subSet) {
115     subSets.add(subSet);
116     subSet.addParent(this);
117   }
118 
119   /**
120    * Gets sub sets.
121    *
122    * @return the sub sets
123    */
124   public List<Dataset> getSubSets() {
125     return subSets;
126   }
127 
128   /**
129    * Is atomic boolean.
130    *
131    * @return the boolean
132    */
133   public boolean isAtomic() {
134     return subSets.isEmpty();
135   }
136 
137   /**
138    * All atomic sub sets list.
139    *
140    * @return the list
141    */
142   public List<Dataset> allAtomicSubSets() {
143     List<Dataset> atomicSubSets = new ArrayList<>();
144     for (Dataset subSet : subSets) {
145       if (subSet.getSubSets().size() == 0) {
146         atomicSubSets.add(subSet);
147       } else {
148         atomicSubSets.addAll(subSet.allAtomicSubSets());
149       }
150     }
151     return atomicSubSets;
152   }
153 
154   /**
155    * Contains boolean.
156    *
157    * @param searchedChildName the searched child name
158    * @return the boolean
159    */
160   public boolean contains(String searchedChildName) {
161     for (Dataset subSet : subSets) {
162       if (searchedChildName.equals(subSet.getName())) {
163         return true;
164       }
165       return subSet.contains(searchedChildName);
166     }
167     return false;
168   }
169 
170 
171   // java.lang.Object overrides --------------------------------------------------------------------------------------
172 
173   @Override
174   public String toString() {
175     return name;
176   }
177 
178   @Override
179   public int hashCode() {
180     return id.hashCode();
181   }
182 
183   @Override
184   public boolean equals(Object other) {
185     if (this == other) {
186       return true;
187     }
188     if (other == null || getClass() != other.getClass()) {
189       return false;
190     }
191     final Dataset"../../../../com/rapiddweller/benerator/dataset/Dataset.html#Dataset">Dataset that = (Dataset) other;
192     return this.id.equals(that.id);
193   }
194 
195 }