← joda-money / src/main/java/org/joda/money/format/MoneyParseContext.java
| 1 | /* |
| 2 | * Copyright 2009-present, Stephen Colebourne |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.joda.money.format; |
| 17 | |
| 18 | import java.math.BigDecimal; |
| 19 | import java.text.ParsePosition; |
| 20 | import java.util.Locale; |
| 21 | |
| 22 | import org.joda.money.BigMoney; |
| 23 | import org.joda.money.CurrencyUnit; |
| 24 | |
| 25 | /** |
| 26 | * Context used when parsing money. |
| 27 | * <p> |
| 28 | * This class is mutable and intended for use by a single thread. |
| 29 | * A new instance is created for each parse. |
| 30 | */ |
| 31 | public final class MoneyParseContext { |
| 32 | |
| 33 | /** |
| 34 | * The locale to parse using. |
| 35 | */ |
| 36 | private Locale locale; |
| 37 | /** |
| 38 | * The text to parse. |
| 39 | */ |
| 40 | private CharSequence text; |
| 41 | /** |
| 42 | * The text index. |
| 43 | */ |
| 44 | private int textIndex; |
| 45 | /** |
| 46 | * The text error index. |
| 47 | */ |
| 48 | private int textErrorIndex = -1; |
| 49 | /** |
| 50 | * The parsed currency. |
| 51 | */ |
| 52 | private CurrencyUnit currency; |
| 53 | /** |
| 54 | * The parsed amount. |
| 55 | */ |
| 56 | private BigDecimal amount; |
| 57 | |
| 58 | /** |
| 59 | * Constructor. |
| 60 | * |
| 61 | * @param locale the locale, not null |
| 62 | * @param text the text to parse, not null |
| 63 | * @param index the current text index |
| 64 | */ |
| 65 | MoneyParseContext(Locale locale, CharSequence text, int index) { |
| 66 | this.locale = locale; |
| 67 | this.text = text; |
| 68 | this.textIndex = index; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Constructor. |
| 73 | * |
| 74 | * @param locale the locale, not null |
| 75 | * @param text the text to parse, not null |
| 76 | * @param index the current text index |
| 77 | * @param errorIndex the error index |
| 78 | * @param currency the currency |
| 79 | * @param amount the parsed amount |
| 80 | */ |
| 81 | MoneyParseContext(Locale locale, CharSequence text, int index, int errorIndex, CurrencyUnit currency, BigDecimal amount) { |
| 82 | this.locale = locale; |
| 83 | this.text = text; |
| 84 | this.textIndex = index; |
| 85 | this.textErrorIndex = errorIndex; |
| 86 | this.currency = currency; |
| 87 | this.amount = amount; |
| 88 | } |
| 89 | |
| 90 | //----------------------------------------------------------------------- |
| 91 | /** |
| 92 | * Gets the locale. |
| 93 | * |
| 94 | * @return the locale, not null |
| 95 | */ |
| 96 | public Locale getLocale() { |
| 97 | return locale; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the locale. |
| 102 | * |
| 103 | * @param locale the locale, not null |
| 104 | */ |
| 105 | public void setLocale(Locale locale) { |
| 106 | MoneyFormatter.checkNotNull(locale, "Locale must not be null"); |
| 107 | this.locale = locale; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets the text being parsed. |
| 112 | * |
| 113 | * @return the text being parsed, never null |
| 114 | */ |
| 115 | public CharSequence getText() { |
| 116 | return text; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets the text. |
| 121 | * |
| 122 | * @param text the text being parsed, not null |
| 123 | */ |
| 124 | public void setText(CharSequence text) { |
| 125 | MoneyFormatter.checkNotNull(text, "Text must not be null"); |
| 126 | this.text = text; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Gets the length of the text being parsed. |
| 131 | * |
| 132 | * @return the length of the text being parsed |
| 133 | */ |
| 134 | public int getTextLength() { |
| 135 | return text.length(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Gets a substring of the text being parsed. |
| 140 | * |
| 141 | * @param start the start index |
| 142 | * @param end the end index |
| 143 | * @return the substring, not null |
| 144 | */ |
| 145 | public String getTextSubstring(int start, int end) { |
| 146 | return text.subSequence(start, end).toString(); |
| 147 | } |
| 148 | |
| 149 | //----------------------------------------------------------------------- |
| 150 | /** |
| 151 | * Gets the current parse position index. |
| 152 | * |
| 153 | * @return the current parse position index |
| 154 | */ |
| 155 | public int getIndex() { |
| 156 | return textIndex; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the current parse position index. |
| 161 | * |
| 162 | * @param index the current parse position index |
| 163 | */ |
| 164 | public void setIndex(int index) { |
| 165 | this.textIndex = index; |
| 166 | } |
| 167 | |
| 168 | //----------------------------------------------------------------------- |
| 169 | /** |
| 170 | * Gets the error index. |
| 171 | * |
| 172 | * @return the error index, negative if no error |
| 173 | */ |
| 174 | public int getErrorIndex() { |
| 175 | return textErrorIndex; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Sets the error index. |
| 180 | * |
| 181 | * @param index the error index |
| 182 | */ |
| 183 | public void setErrorIndex(int index) { |
| 184 | this.textErrorIndex = index; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets the error index from the current index. |
| 189 | */ |
| 190 | public void setError() { |
| 191 | this.textErrorIndex = textIndex; |
| 192 | } |
| 193 | |
| 194 | //----------------------------------------------------------------------- |
| 195 | /** |
| 196 | * Gets the parsed currency. |
| 197 | * |
| 198 | * @return the parsed currency, null if not parsed yet |
| 199 | */ |
| 200 | public CurrencyUnit getCurrency() { |
| 201 | return currency; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the parsed currency. |
| 206 | * |
| 207 | * @param currency the parsed currency, may be null |
| 208 | */ |
| 209 | public void setCurrency(CurrencyUnit currency) { |
| 210 | this.currency = currency; |
| 211 | } |
| 212 | |
| 213 | //----------------------------------------------------------------------- |
| 214 | /** |
| 215 | * Gets the parsed amount. |
| 216 | * |
| 217 | * @return the parsed amount, null if not parsed yet |
| 218 | */ |
| 219 | public BigDecimal getAmount() { |
| 220 | return amount; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Sets the parsed currency. |
| 225 | * |
| 226 | * @param amount the parsed amount, may be null |
| 227 | */ |
| 228 | public void setAmount(BigDecimal amount) { |
| 229 | this.amount = amount; |
| 230 | } |
| 231 | |
| 232 | //----------------------------------------------------------------------- |
| 233 | /** |
| 234 | * Checks if the parse has found an error. |
| 235 | * |
| 236 | * @return whether a parse error has occurred |
| 237 | */ |
| 238 | public boolean isError() { |
| 239 | return textErrorIndex >= 0; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Checks if the text has been fully parsed such that there is no more text to parse. |
| 244 | * |
| 245 | * @return true if fully parsed |
| 246 | */ |
| 247 | public boolean isFullyParsed() { |
| 248 | return textIndex == getTextLength(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Checks if the context contains a currency and amount suitable for creating |
| 253 | * a monetary value. |
| 254 | * |
| 255 | * @return true if able to create a monetary value |
| 256 | */ |
| 257 | public boolean isComplete() { |
| 258 | return currency != null && amount != null; |
| 259 | } |
| 260 | |
| 261 | //----------------------------------------------------------------------- |
| 262 | /** |
| 263 | * Creates a child context. |
| 264 | * |
| 265 | * @return the child context, never null |
| 266 | */ |
| 267 | MoneyParseContext createChild() { |
| 268 | return new MoneyParseContext(locale, text, textIndex, textErrorIndex, currency, amount); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Merges the child context back into this instance. |
| 273 | * |
| 274 | * @param child the child context, not null |
| 275 | */ |
| 276 | void mergeChild(MoneyParseContext child) { |
| 277 | setLocale(child.getLocale()); |
| 278 | setText(child.getText()); |
| 279 | setIndex(child.getIndex()); |
| 280 | setErrorIndex(child.getErrorIndex()); |
| 281 | setCurrency(child.getCurrency()); |
| 282 | setAmount(child.getAmount()); |
| 283 | } |
| 284 | |
| 285 | //----------------------------------------------------------------------- |
| 286 | /** |
| 287 | * Converts the indexes to a parse position. |
| 288 | * |
| 289 | * @return the parse position, never null |
| 290 | */ |
| 291 | public ParsePosition toParsePosition() { |
| 292 | var pp = new ParsePosition(textIndex); |
| 293 | pp.setErrorIndex(textErrorIndex); |
| 294 | return pp; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Converts the context to a {@code BigMoney}. |
| 299 | * |
| 300 | * @return the monetary value, never null |
| 301 | * @throws MoneyFormatException if either the currency or amount is missing |
| 302 | */ |
| 303 | public BigMoney toBigMoney() { |
| 304 | if (currency == null) { |
| 305 | throw new MoneyFormatException("Cannot convert to BigMoney as no currency found"); |
| 306 | } |
| 307 | if (amount == null) { |
| 308 | throw new MoneyFormatException("Cannot convert to BigMoney as no amount found"); |
| 309 | } |
| 310 | return BigMoney.of(currency, amount); |
| 311 | } |
| 312 | |
| 313 | } |
| 314 |