1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.message;
18
19 import net.sourceforge.jarduino.message.ArduinoParser.ArduinoParserException;
20
21
22
23
24 public final class ArduinoSignalFactor {
25
26
27
28 private static final char START = '(';
29
30
31
32
33 private static final char END = ')';
34
35
36
37
38 private final Number theFactor;
39
40
41
42
43 private final Number theOffset;
44
45
46
47
48 private final int theNumDecimals;
49
50
51
52
53
54
55
56 private ArduinoSignalFactor(final Number pFactor,
57 final Number pOffset,
58 final int pNumDecimals) {
59
60 theFactor = pFactor;
61 theOffset = pOffset;
62 theNumDecimals = pNumDecimals;
63 }
64
65
66
67
68
69 public Number getFactor() {
70 return theFactor;
71 }
72
73
74
75
76
77 public Number getOffset() {
78 return theOffset;
79 }
80
81
82
83
84
85 public int getNumDecimals() {
86 return theNumDecimals;
87 }
88
89
90
91
92
93 public boolean isFloat() {
94 return theFactor instanceof Double;
95 }
96
97
98
99
100
101
102
103 static ArduinoSignalFactor parseFactors(final String pFactorDef) throws ArduinoParserException {
104
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
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
122 final int myNumDecimals = ArduinoParser.determineNumDecimals(myFact);
123
124
125 if (myFactor.getClass() != myOffset.getClass()) {
126
127 if (myFactor instanceof Long) {
128 myFactor = myFactor.doubleValue();
129 }
130 if (myOffset instanceof Long) {
131 myOffset = myOffset.doubleValue();
132 }
133 }
134
135
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 }