View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ******************************************************************************/
17  package net.sourceforge.jarduino.message;
18  
19  import java.util.Objects;
20  
21  import net.sourceforge.jarduino.ArduinoException;
22  
23  /**
24   * Attribute.
25   */
26  public class ArduinoAttribute {
27      /**
28       * Interface for object that has attributes.
29       */
30      public interface ArduinoAttrObject {
31          /**
32           * Obtain value for attribute.
33           * @param pAttr the attribute
34           * @return the value
35           */
36          Object getAttrValue(ArduinoAttribute pAttr);
37      }
38  
39      /**
40       * The Relationship Marker.
41       */
42      static final String REL_MARKER = "REL_";
43  
44      /**
45       * The NODE2MSG Marker.
46       */
47      static final String NODE2MSG_MARKER = ArduinoNode.MARKER + ArduinoMessage.MARKER + REL_MARKER;
48  
49      /**
50       * The NODE2SIG Marker.
51       */
52      static final String NODE2SIG_MARKER = ArduinoNode.MARKER + ArduinoSignal.MARKER + REL_MARKER;
53  
54      /**
55       * The name of the attribute.
56       */
57      private final String theName;
58  
59      /**
60       * The class of the attribute.
61       */
62      private final ArduinoAttrClass theClass;
63  
64      /**
65       * The type of the attribute.
66       */
67      private final ArduinoAttrType theType;
68  
69      /**
70       * The constraints.
71       */
72      private ArduinoAttrConstraints theConstraints;
73  
74      /**
75       * The default value.
76       */
77      private Object theDefault;
78  
79      /**
80       * Constructor.
81       * @param pName the name
82       * @param pClass the class
83       * @param pType the type
84       */
85      ArduinoAttribute(final String pName,
86                       final ArduinoAttrClass pClass,
87                       final ArduinoAttrType pType) {
88          theName = pName;
89          theClass = pClass;
90          theType = pType;
91      }
92  
93      /**
94       * Obtain the name.
95       * @return the name
96       */
97      public String getName() {
98          return theName;
99      }
100 
101     /**
102      * Obtain the class.
103      * @return the class
104      */
105     public ArduinoAttrClass getAttrClass() {
106         return theClass;
107     }
108 
109     /**
110      * Obtain the type.
111      * @return the type
112      */
113     public ArduinoAttrType getAttrType() {
114         return theType;
115     }
116 
117     /**
118      * Obtain the constraints.
119      * @return the constraints
120      */
121     public ArduinoAttrConstraints getConstraints() {
122         return theConstraints;
123     }
124 
125     /**
126      * Set the constraints.
127      * @param pConstraints the constraints
128      */
129     void setConstraints(final ArduinoAttrConstraints pConstraints) {
130         theConstraints = pConstraints;
131     }
132 
133     /**
134      * Obtain the default value.
135      * @return the default
136      */
137     public Object getDefault() {
138         return theDefault;
139     }
140 
141     /**
142      * Set the default value.
143      * @param pDefault the default
144      */
145     void setDefault(final Object pDefault) {
146         theDefault = pDefault;
147     }
148 
149     @Override
150     public boolean equals(final Object pThat) {
151         /* Handle trivial cases */
152         if (pThat == this) {
153             return true;
154         } else if (!(pThat instanceof ArduinoAttribute)) {
155             return false;
156         }
157 
158         /* Access correctly */
159         final ArduinoAttribute./net/sourceforge/jarduino/message/ArduinoAttribute.html#ArduinoAttribute">ArduinoAttribute myThat = (ArduinoAttribute) pThat;
160 
161         /* Check name and owner */
162         return theName.equals(myThat.getName());
163     }
164 
165     @Override
166     public int hashCode() {
167         return Objects.hashCode(theName);
168     }
169 
170     @Override
171     public String toString() {
172         String myName = theName + ":" + theClass + ":" + theType;
173         if (theDefault != null) {
174             myName += "=" + theDefault;
175         }
176         return myName;
177     }
178 
179     /**
180      * AttributeClass.
181      */
182     public enum ArduinoAttrClass {
183         /**
184          * System.
185          */
186         SYSTEM,
187 
188         /**
189          * Node.
190          */
191         NODE,
192 
193         /**
194          * Message.
195          */
196         MESSAGE,
197 
198         /**
199          * Signal.
200          */
201         SIGNAL,
202 
203         /**
204          * NodeToMessage.
205          */
206         NODE2MSG,
207 
208         /**
209          * NodeToSignal.
210          */
211         NODE2SIGNAL;
212 
213         /**
214          * Is this a relation attribute?
215          * @return true/false
216          */
217         public boolean isRelation() {
218             switch (this) {
219                 case NODE2SIGNAL:
220                 case NODE2MSG:
221                     return true;
222                 default:
223                     return false;
224             }
225         }
226 
227         /**
228          * Parse attributeClass.
229          * @param pAttrClass the attributeClass representation
230          * @return the attrClass
231          * @throws ArduinoException on error
232          */
233         static ArduinoAttrClass parseAttrClass(final String pAttrClass) throws ArduinoException {
234             switch (pAttrClass) {
235                 case ArduinoNode.MARKER:
236                     return NODE;
237                 case ArduinoMessage.MARKER:
238                     return MESSAGE;
239                 case ArduinoSignal.MARKER:
240                     return SIGNAL;
241                 case NODE2MSG_MARKER:
242                     return NODE2MSG;
243                 case NODE2SIG_MARKER:
244                     return NODE2SIGNAL;
245                 default:
246                     if (pAttrClass.length() == 0
247                             || pAttrClass.charAt(0) != ArduinoChar.QUOTE) {
248                         throw new ArduinoException("Invalid Attribute class", pAttrClass);
249                     }
250                     return SYSTEM;
251             }
252         }
253     }
254 
255     /**
256      * AttributeType.
257      */
258     public enum ArduinoAttrType {
259         /**
260          * Integer.
261          */
262         INT,
263 
264         /**
265          * Float.
266          */
267         FLOAT,
268 
269         /**
270          * Hex.
271          */
272         HEX,
273 
274         /**
275          * String.
276          */
277         STRING,
278 
279         /**
280          * ENUM.
281          */
282         ENUM;
283 
284         /**
285          * Parse attrType.
286          * @param pAttrType the attrType representation
287          * @return the attrType
288          * @throws ArduinoException on error
289          */
290         static ArduinoAttrType parseAttrType(final String pAttrType) throws ArduinoException {
291             /* Loop through the values */
292             for (ArduinoAttrType myType : values()) {
293                 if (pAttrType.equals(myType.name())) {
294                     return myType;
295                 }
296             }
297             throw new ArduinoException("Invalid AttrType", pAttrType);
298         }
299     }
300 }