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.model.data;
28  
29  import java.util.Collection;
30  import java.util.Map;
31  import java.util.TreeMap;
32  
33  /**
34   * Describes an array.<br/><br/>
35   * Created: 29.04.2010 07:32:52
36   *
37   * @author Volker Bergmann
38   * @since 0.6.1
39   */
40  public class ArrayTypeDescriptor extends TypeDescriptor {
41  
42    private final Map<Integer, ArrayElementDescriptor> elements;
43  
44    /**
45     * Instantiates a new Array type descriptor.
46     *
47     * @param name     the name
48     * @param provider the provider
49     */
50    public ArrayTypeDescriptor(String name, DescriptorProvider provider) {
51      this(name, provider, null);
52    }
53  
54    /**
55     * Instantiates a new Array type descriptor.
56     *
57     * @param name     the name
58     * @param provider the provider
59     * @param parent   the parent
60     */
61    public ArrayTypeDescriptor(String name, DescriptorProvider provider,
62                               ArrayTypeDescriptor parent) {
63      super(name, provider, parent);
64      this.elements = new TreeMap<>();
65    }
66  
67    // element handling ------------------------------------------------------------------------------------------------
68  
69    @Override
70    public ArrayTypeDescriptor getParent() {
71      return (ArrayTypeDescriptor) super.getParent();
72    }
73  
74    /**
75     * Add element.
76     *
77     * @param descriptor the descriptor
78     */
79    public void addElement(ArrayElementDescriptor descriptor) {
80      elements.put(descriptor.getIndex(), descriptor);
81    }
82  
83    /**
84     * Gets element.
85     *
86     * @param index the index
87     * @return the element
88     */
89    public ArrayElementDescriptor getElement(int index) {
90      return elements.get(index);
91    }
92  
93    /**
94     * Gets element.
95     *
96     * @param index   the index
97     * @param inherit the inherit
98     * @return the element
99     */
100   public ArrayElementDescriptor getElement(int index, boolean inherit) {
101     ArrayElementDescriptor element = getElement(index);
102     if (element != null) {
103       return element;
104     }
105     ArrayTypeDescriptor tmp = getParent();
106     while (tmp != null && inherit) {
107       ArrayElementDescriptor candidate = tmp.getElement(index);
108       if (candidate != null) {
109         return candidate;
110       }
111       tmp = tmp.getParent();
112     }
113     return null;
114   }
115 
116   /**
117    * Gets elements.
118    *
119    * @return the elements
120    */
121   public Collection<ArrayElementDescriptor> getElements() {
122     return elements.values();
123   }
124 
125   /**
126    * Gets element count.
127    *
128    * @return the element count
129    */
130   public int getElementCount() {
131     return (parent != null ?
132         ((ArrayTypeDescriptor) parent).getElementCount() :
133         elements.size());
134   }
135 
136 
137   // java.lang.Object overrides --------------------------------------------------------------------------------------
138 
139   @Override
140   public String toString() {
141     if (elements.size() == 0) {
142       return super.toString();
143     }
144     //return new CompositeFormatter(false, false).render(super.toString() + '{', new CompositeAdapter(), "}");
145     return getClass().getSimpleName() + "[name=" + getName() +
146         ", elements=" + elements + ']';
147   }
148 
149 }