1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

add Double.tryParse using new RefSupport class

This commit is contained in:
Kevin Glynn 2011-05-02 15:34:21 +02:00
parent 0a7af29e58
commit 4e9f105c54
2 changed files with 90 additions and 18 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is
@ -43,6 +43,50 @@
<Name>Parse</Name>
<Return>System.Double</Return>
</Method>
<Method>
<Imports>
<Import>CS2JNet.System.DoubleSupport</Import>
</Imports>
<Java>DoubleSupport.tryParse(${dStr}, ${result})</Java>
<Params>
<Param>
<Type>System.String</Type>
<Name>dStr</Name>
</Param>
<Param>
<Type>System.Double</Type>
<Name>result</Name>
</Param>
</Params>
<Name>TryParse</Name>
<Return>System.Boolean</Return>
</Method>
<Method>
<Imports>
<Import>CS2JNet.System.DoubleSupport</Import>
</Imports>
<Java>DoubleSupport.tryParse(${dStr}, ${style}, ${provider}, ${result})</Java>
<Params>
<Param>
<Type>System.String</Type>
<Name>dStr</Name>
</Param>
<Param>
<Type>System.Globalization.NumberStyles</Type>
<Name>style</Name>
</Param>
<Param>
<Type>System.IFormatProvider</Type>
<Name>provider</Name>
</Param>
<Param>
<Type>System.Double</Type>
<Name>result</Name>
</Param>
</Params>
<Name>TryParse</Name>
<Return>System.Boolean</Return>
</Method>
<Method>
<Imports>
<Import>java.text.NumberFormat</Import>

View File

@ -25,6 +25,7 @@ import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import CS2JNet.JavaSupport.language.RefSupport;
import CS2JNet.JavaSupport.util.LocaleSupport;
import CS2JNet.System.Globalization.*;
@ -89,21 +90,48 @@ public class DoubleSupport {
}
}
public static double parse(String s, int style, Locale loc) throws ParseException
{
String toParse = s;
if ((style & NumberStyles.getAllowLeadingWhite()) > 0)
toParse = StringSupport.TrimStart(toParse, null);
if ((style & NumberStyles.getAllowTrailingWhite()) > 0)
toParse = StringSupport.TrimEnd(toParse, null);
if ((style & NumberStyles.getAllowLeadingSign()) == 0)
if (toParse.charAt(0) == '+' || toParse.charAt(0) == '-')
throw new ParseException("Signs not allowed: " + s, 0);
return NumberFormat.getInstance(loc == null ? LocaleSupport.INVARIANT : loc).parse(toParse).doubleValue();
}
public static double parse(String s, int style, Locale loc) throws ParseException
{
String toParse = s;
if ((style & NumberStyles.getAllowLeadingWhite()) > 0)
toParse = StringSupport.TrimStart(toParse, null);
if ((style & NumberStyles.getAllowTrailingWhite()) > 0)
toParse = StringSupport.TrimEnd(toParse, null);
if ((style & NumberStyles.getAllowLeadingSign()) == 0)
if (toParse.charAt(0) == '+' || toParse.charAt(0) == '-')
throw new ParseException("Signs not allowed: " + s, 0);
return NumberFormat.getInstance(loc == null ? LocaleSupport.INVARIANT : loc).parse(toParse).doubleValue();
}
public static boolean tryParse(String s, int style, Locale loc, RefSupport<Double> result)
{
String toParse = s;
if ((style & NumberStyles.getAllowLeadingWhite()) > 0)
toParse = StringSupport.TrimStart(toParse, null);
if ((style & NumberStyles.getAllowTrailingWhite()) > 0)
toParse = StringSupport.TrimEnd(toParse, null);
if ((style & NumberStyles.getAllowLeadingSign()) == 0)
if (toParse.charAt(0) == '+' || toParse.charAt(0) == '-')
return false;
try {
result.setValue(NumberFormat.getInstance(loc == null ? LocaleSupport.INVARIANT : loc).parse(toParse).doubleValue());
return true;
} catch (ParseException e) {
return false;
}
}
public static boolean tryParse(String s, RefSupport<Double> result)
{
// TODO: what is default style?
return tryParse(s, NumberStyles.getAny(), null, result);
}
}