Skip to content

Commit f670aae

Browse files
committed
106: Shall we also use ParsePosition in UnitFormat?
Task-Url: https://github.com/unitsofmeasurement/unit-api/issues/issues/106
1 parent 4cc18b8 commit f670aae

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/main/java/javax/measure/format/UnitFormat.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package javax.measure.format;
3131

3232
import java.io.IOException;
33+
import java.text.ParsePosition;
3334

3435
import javax.measure.Unit;
3536

@@ -45,7 +46,7 @@
4546
* @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
4647
* @author <a href="mailto:werner@uom.technology">Werner Keil</a>
4748
*
48-
* @version 1.2, June 25, 2018
49+
* @version 1.3, August 8, 2018
4950
* @since 1.0
5051
*
5152
* @see Unit
@@ -111,6 +112,20 @@ public interface UnitFormat {
111112
default boolean isLocaleSensitive() {
112113
return false;
113114
}
115+
116+
/**
117+
* Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a {@link Unit}. If parsing succeeds, then
118+
* the index of the <code>cursor</code> argument is updated to the index after the last character used.
119+
*
120+
* @param csq
121+
* the <code>CharSequence</code> to parse.
122+
* @param cursor
123+
* the cursor holding the current parsing index.
124+
* @return the unit parsed from the specified character sub-sequence.
125+
* @throws IllegalArgumentException
126+
* if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
127+
*/
128+
Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException, MeasurementParseException;
114129

115130
/**
116131
* Parses the text into an instance of {@link Unit}.
@@ -126,6 +141,7 @@ default boolean isLocaleSensitive() {
126141
* if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
127142
* @throws UnsupportedOperationException
128143
* if the {@link UnitFormat} is unable to parse.
144+
* @since 2.0
129145
*/
130146
Unit<?> parse(CharSequence csq) throws MeasurementParseException;
131147
}

src/test/java/javax/measure/test/format/SimpleTestUnitFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* Provides a simple interface for formatting and parsing {@linkplain Unit units}.
4242
*
4343
* @author <a href="mailto:units@catmedia.us">Werner Keil</a>
44-
* @version 0.5
44+
* @version 0.6
4545
*/
4646
public class SimpleTestUnitFormat extends TestUnitFormat {
4747
private final Map<String, String> symbolMap = new HashMap<>();

src/test/java/javax/measure/test/format/TestUnitFormat.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package javax.measure.test.format;
3131

3232
import java.io.IOException;
33+
import java.text.ParsePosition;
3334
import java.util.HashMap;
3435
import java.util.Map;
3536

@@ -119,6 +120,20 @@ public final Unit<?> parse(CharSequence csq) throws MeasurementParseException {
119120
return parse(csq, 0);
120121
}
121122

123+
/**
124+
* Parses the specified character sequence to produce a unit (convenience method). If the specified sequence is empty, the unitary unit
125+
* (dimensionless) is returned.
126+
*
127+
* @param csq
128+
* the <code>CharSequence</code> to parse.
129+
* @return the unit parsed from the specified character sub-sequence.
130+
* @throws ParseException
131+
* if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
132+
*/
133+
public final Unit<?> parse(CharSequence csq, ParsePosition pos) throws MeasurementParseException {
134+
return parse(csq, pos.getIndex());
135+
}
136+
122137
/**
123138
* Formats an object to produce a string. This is equivalent to <blockquote> {@link #format(Unit, Appendable) format}<code>(unit,
124139
* new StringBuilder()).toString();</code> </blockquote>

0 commit comments

Comments
 (0)