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 net.sourceforge.jarduino.message.ArduinoParser.ArduinoParserException;
20  
21  /**
22   * Arduino Signal Factor.
23   */
24  public final class ArduinoSignalFactor {
25      /**
26       * The start character.
27       */
28      private static final char START = '(';
29  
30      /**
31       * The end character.
32       */
33      private static final char END = ')';
34  
35      /**
36       * the factor of the signal.
37       */
38      private final Number theFactor;
39  
40      /**
41       * the offset of the signal.
42       */
43      private final Number theOffset;
44  
45      /**
46       * the numberf of decimals.
47       */
48      private final int theNumDecimals;
49  
50      /**
51       * Constructor.
52       * @param pFactor the factor
53       * @param pOffset the offset
54       * @param pNumDecimals the number of decimals
55       */
56      private ArduinoSignalFactor(final Number pFactor,
57                                  final Number pOffset,
58                                  final int pNumDecimals) {
59          /* Store values */
60          theFactor = pFactor;
61          theOffset = pOffset;
62          theNumDecimals = pNumDecimals;
63      }
64  
65      /**
66       * Obtain the factor.
67       * @return the factor
68       */
69      public Number getFactor() {
70          return theFactor;
71      }
72  
73      /**
74       * Obtain the offset.
75       * @return the offset
76       */
77      public Number getOffset() {
78          return theOffset;
79      }
80  
81      /**
82       * Obtain the number of decimals.
83       * @return the number of decimals
84       */
85      public int getNumDecimals() {
86          return theNumDecimals;
87      }
88  
89      /**
90       * Does this factor use floats?
91       * @return true/false
92       */
93      public boolean isFloat() {
94          return theFactor instanceof Double;
95      }
96  
97      /**
98       * Parse factors.
99       * @param pFactorDef the factor representation
100      * @return the factors
101      * @throws ArduinoParserException on error
102      */
103     static ArduinoSignalFactor parseFactors(final String pFactorDef) throws ArduinoParserException {
104         /* Remove surrounding parentheses */
105         if (pFactorDef.charAt(0) != START
106                 || pFactorDef.charAt(pFactorDef.length() - 1) != END) {
107             throw new ArduinoParserException("Missing surrounding ()s", pFactorDef);
108         }
109         final String myFactors = pFactorDef.substring(1, pFactorDef.length() - 1);
110 
111         /* Split out factors */
112         final int myIndex = myFactors.indexOf(ArduinoChar.COMMA);
113         if (myIndex == -1) {
114             throw new ArduinoParserException("Missing " + ArduinoChar.COMMA + " separator", pFactorDef);
115         }
116         final String myFact = myFactors.substring(0, myIndex);
117         Number myFactor = ArduinoParser.parseNumber(myFact);
118         final String myOff = myFactors.substring(myIndex + 1);
119         Number myOffset = ArduinoParser.parseNumber(myOff);
120 
121         /* Check number of decimals */
122         final int myNumDecimals = ArduinoParser.determineNumDecimals(myFact);
123 
124         /* Make sure that if either value is double, both are */
125         if (myFactor.getClass() != myOffset.getClass()) {
126             /* Convert longs to doubles */
127             if (myFactor instanceof Long) {
128                 myFactor = myFactor.doubleValue();
129             }
130             if (myOffset instanceof Long) {
131                 myOffset = myOffset.doubleValue();
132             }
133         }
134 
135         /* return the parsed Factor */
136         return new ArduinoSignalFactor(myFactor, myOffset, myNumDecimals);
137     }
138 
139     @Override
140     public String toString() {
141         return "" + START + theFactor + ArduinoChar.COMMA + theOffset + END;
142     }
143 }