2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.intellij
.util
.xml
.converters
.values
;
19 import com
.intellij
.util
.xml
.ConvertContext
;
20 import com
.intellij
.util
.xml
.Converter
;
21 import com
.intellij
.util
.xml
.DomBundle
;
22 import org
.jetbrains
.annotations
.NonNls
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
26 import java
.math
.BigDecimal
;
27 import java
.math
.BigInteger
;
29 public class NumberValueConverter
extends Converter
<String
> {
31 private final Class myNumberClass
;
32 private final boolean myAllowEmpty
;
35 public NumberValueConverter(@NotNull final Class numberClass
, final boolean allowEmpty
) {
36 myNumberClass
= numberClass
;
37 myAllowEmpty
= allowEmpty
;
40 public String
fromString(@Nullable @NonNls final String s
, final ConvertContext context
) {
41 if (s
== null) return null;
43 if (myAllowEmpty
&& s
.trim().length() == 0) return s
;
45 return parseNumber(s
, myNumberClass
) == null ?
null : s
;
48 public String
toString(@Nullable final String s
, final ConvertContext context
) {
52 public String
getErrorMessage(@Nullable final String s
, final ConvertContext context
) {
53 if (s
== null) return super.getErrorMessage(s
, context
);
55 return s
.trim().length() == 0 ?
56 DomBundle
.message("value.converter.format.exception.empty.string", myNumberClass
.getName()) :
57 DomBundle
.message("value.converter.format.exception", s
, myNumberClass
.getName());
61 public static Number
parseNumber(@NotNull String text
, @NotNull Class targetClass
) {
63 String trimmed
= text
.trim();
65 if (targetClass
.equals(Byte
.class) || targetClass
.equals(byte.class)) {
66 return Byte
.decode(trimmed
);
68 else if (targetClass
.equals(Short
.class) || targetClass
.equals(short.class)) {
69 return Short
.decode(trimmed
);
71 else if (targetClass
.equals(Integer
.class) || targetClass
.equals(int.class)) {
72 return Integer
.decode(trimmed
);
74 else if (targetClass
.equals(Long
.class) || targetClass
.equals(long.class)) {
75 return Long
.decode(trimmed
);
77 else if (targetClass
.equals(BigInteger
.class)) {
78 return decodeBigInteger(trimmed
);
80 else if (targetClass
.equals(Float
.class) || targetClass
.equals(float.class)) {
81 return Float
.valueOf(trimmed
);
83 else if (targetClass
.equals(Double
.class) || targetClass
.equals(double.class)) {
84 return Double
.valueOf(trimmed
);
86 else if (targetClass
.equals(BigDecimal
.class) || targetClass
.equals(Number
.class)) {
87 return new BigDecimal(trimmed
);
89 } catch (NumberFormatException ex
) {
95 private static BigInteger
decodeBigInteger(String value
) {
98 boolean negative
= false;
100 // Handle minus sign, if present.
101 if (value
.startsWith("-")) {
106 // Handle radix specifier, if present.
107 if (value
.startsWith("0x", index
) || value
.startsWith("0X", index
)) {
111 else if (value
.startsWith("#", index
)) {
115 else if (value
.startsWith("0", index
) && value
.length() > 1 + index
) {
120 BigInteger result
= new BigInteger(value
.substring(index
), radix
);
121 return (negative ? result
.negate() : result
);