下面列出了java.text.NumberFormat#setParseIntegerOnly ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testNumeratorFormat() {
NumberFormat old = properFormat.getNumeratorFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
properFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, properFormat.getNumeratorFormat());
properFormat.setNumeratorFormat(old);
old = improperFormat.getNumeratorFormat();
nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
improperFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
improperFormat.setNumeratorFormat(old);
}
/**
* Sets integer number format to JFormattedTextField instance,
* sets value of JFormattedTextField instance to object's field value,
* synchronizes object's field value with the value of JFormattedTextField instance.
*
* @param textField JFormattedTextField instance
* @param owner an object whose field is synchronized with {@code textField}
* @param property object's field name for synchronization
*/
public static void setupIntegerFieldTrackingValue(final JFormattedTextField textField,
final InspectionProfileEntry owner,
final String property) {
NumberFormat formatter = NumberFormat.getIntegerInstance();
formatter.setParseIntegerOnly(true);
textField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(formatter)));
textField.setValue(getPropertyValue(owner, property));
final Document document = textField.getDocument();
document.addDocumentListener(new DocumentAdapter() {
@Override
public void textChanged(DocumentEvent e) {
try {
textField.commitEdit();
setPropertyValue(owner, property,
((Number) textField.getValue()).intValue());
} catch (ParseException e1) {
// No luck this time
}
}
});
}
@Test
public void testNumeratorFormat() {
NumberFormat old = properFormat.getNumeratorFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
properFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, properFormat.getNumeratorFormat());
properFormat.setNumeratorFormat(old);
old = improperFormat.getNumeratorFormat();
nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
improperFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
improperFormat.setNumeratorFormat(old);
}
@Test
public void testNumeratorFormat() {
NumberFormat old = properFormat.getNumeratorFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
properFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, properFormat.getNumeratorFormat());
properFormat.setNumeratorFormat(old);
old = improperFormat.getNumeratorFormat();
nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
improperFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
improperFormat.setNumeratorFormat(old);
}
public void testWholeFormat() {
ProperFractionFormat format = (ProperFractionFormat)properFormat;
NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
assertEquals(nf, format.getWholeFormat());
format.setWholeFormat(old);
}
public void testWholeFormat() {
ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;
NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
assertEquals(nf, format.getWholeFormat());
format.setWholeFormat(old);
}
public void testWholeFormat() {
ProperFractionFormat format = (ProperFractionFormat)properFormat;
NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
assertEquals(nf, format.getWholeFormat());
format.setWholeFormat(old);
}
public void testWholeFormat() {
ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;
NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
assertEquals(nf, format.getWholeFormat());
format.setWholeFormat(old);
}
@Override
protected Number parse(String value, NumberFormat format) throws ParseException {
format.setParseIntegerOnly(true);
Number result = super.parse(value, format);
if (!hasValidLongRange(result)) {
throw new ParseException(String.format("Integer range exceeded: \"%s\"", value), 0);
}
return result;
}
public void testDenominatorFormat() {
NumberFormat old = properFormat.getDenominatorFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
properFormat.setDenominatorFormat(nf);
assertEquals(nf, properFormat.getDenominatorFormat());
properFormat.setDenominatorFormat(old);
old = improperFormat.getDenominatorFormat();
nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
improperFormat.setDenominatorFormat(nf);
assertEquals(nf, improperFormat.getDenominatorFormat());
improperFormat.setDenominatorFormat(old);
}
public void testWholeFormat() {
ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;
NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
assertEquals(nf, format.getWholeFormat());
format.setWholeFormat(old);
}
public void testNumeratorFormat() {
NumberFormat old = properFormat.getNumeratorFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
properFormat.setNumeratorFormat(nf);
assertEquals(nf, properFormat.getNumeratorFormat());
properFormat.setNumeratorFormat(old);
old = improperFormat.getNumeratorFormat();
nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
improperFormat.setNumeratorFormat(nf);
assertEquals(nf, improperFormat.getNumeratorFormat());
improperFormat.setNumeratorFormat(old);
}
@Test
public void testWholeFormat() {
ProperFractionFormat format = (ProperFractionFormat)properFormat;
NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
Assert.assertEquals(nf, format.getWholeFormat());
format.setWholeFormat(old);
}
/**
* Restricted constructor.
*/
private NumberFormatter(NumberFormat format) {
ArgChecker.notNull(format, "format");
format.setParseIntegerOnly(false);
this.underlying = ThreadLocal.withInitial(() -> (NumberFormat) format.clone());
}
/**
* Restriction to named sequence, accepts restrictions of
* the following forms:
* <code>name</code> (corresponds to the whole chromosome)
* <code>name:</code> (corresponds to the whole chromosome)
* <code>name:start</code> (corresponds to a point on the chromosome)
* <code>name:start-end</code> (a range from start to end)
* <code>name:start+length</code> (a range from start to start+length)
* <code>name:start~length</code> (a range from start-length to start+length)
* @param restriction restriction string
*/
public RegionRestriction(String restriction) {
if (restriction.length() == 0) {
throw malformedRange(restriction);
}
final int rangepos = restriction.lastIndexOf(':');
if (rangepos != -1 && rangepos < restriction.length() - 1) {
mSequence = restriction.substring(0, rangepos);
final String range = restriction.substring(rangepos + 1);
final NumberFormat fmt = NumberFormat.getInstance(Locale.getDefault());
fmt.setParseIntegerOnly(true);
final boolean hasPlus = range.contains("+");
final boolean hasMinus = range.contains("-");
final boolean hasTilde = range.contains("~");
final String[] parts = range.split("[-~+]");
if (parts.length != 2 && parts.length != 1) {
throw malformedRange(restriction);
}
final ParsePosition pos = new ParsePosition(0);
final Number start = fmt.parse(parts[0], pos);
if (parts[0].length() != pos.getIndex() || pos.getIndex() == 0) {
throw malformedRange(restriction);
}
if (parts.length == 1) {
mStart = start.intValue() - 1;
mEnd = MISSING;
} else {
pos.setIndex(0);
final Number last = fmt.parse(parts[1], pos);
if (parts[1].length() != pos.getIndex() || pos.getIndex() == 0) {
throw malformedRange(restriction);
}
if (hasPlus) {
mStart = start.intValue() - 1;
mEnd = start.intValue() - 1 + last.intValue();
} else if (hasTilde) {
mStart = Math.max(start.intValue() - 1 - last.intValue(), 0); // Allow truncation at 0
mEnd = start.intValue() - 1 + last.intValue();
} else if (hasMinus) {
mStart = start.intValue() - 1;
mEnd = last.intValue();
} else {
throw malformedRange(restriction);
}
}
if ((mStart < 0) || (mEnd != MISSING && mEnd < (mStart + 1))) {
throw malformedRange(restriction);
}
} else {
mSequence = rangepos == restriction.length() - 1 ? restriction.substring(0, rangepos) : restriction;
mStart = MISSING;
mEnd = MISSING;
}
}
@Override
public void validate(Model m) {
Object value = m.get(attribute);
if(!present(value, m)){
return;
}
// validators should not also do conversion
if(convertNullIfEmpty && "".equals(value)){
m.set(attribute, null);
value = null;
}
if(value == null && allowNull){
return;
}
//this is to check just numericality
if(!(value instanceof Number)) {
if (value != null) {
ParsePosition pp = new ParsePosition(0);
String input = value.toString();
// toString() is not Locale dependant...
// ... but NumberFormat is. For Polish locale where decimal separator is "," instead of ".". Might fail some tests...
NumberFormat nf = NumberFormat.getInstance();
nf.setParseIntegerOnly(onlyInteger);
nf.parse(input, pp);
if (pp.getIndex() != (input.length())) {
m.addValidator(this, attribute);
}
} else {
m.addValidator(this, attribute);
}
}
if(min != null){
validateMin(Convert.toDouble(value), m);
}
if(max != null){
validateMax(Convert.toDouble(value), m);
}
if(onlyInteger){
validateIntegerOnly(value, m);
}
}
/**
* Create a default number format. The default number format is based on
* {@link NumberFormat#getNumberInstance(java.util.Locale)} with the only
* customizing is the maximum number of BigFraction digits, which is set to 0.
* @param locale the specific locale used by the format.
* @return the default number format specific to the given locale.
*/
protected static NumberFormat getDefaultNumberFormat(final Locale locale) {
final NumberFormat nf = NumberFormat.getNumberInstance(locale);
nf.setMaximumFractionDigits(0);
nf.setParseIntegerOnly(true);
return nf;
}
/**
* Create a default number format. The default number format is based on
* {@link NumberFormat#getNumberInstance(java.util.Locale)}. The only
* customization is the maximum number of BigFraction digits, which is set to 0.
* @param locale the specific locale used by the format.
* @return the default number format specific to the given locale.
*/
protected static NumberFormat getDefaultNumberFormat(final Locale locale) {
final NumberFormat nf = NumberFormat.getNumberInstance(locale);
nf.setMaximumFractionDigits(0);
nf.setParseIntegerOnly(true);
return nf;
}
/**
* Create a default number format. The default number format is based on
* {@link NumberFormat#getNumberInstance(java.util.Locale)}. The only
* customization is the maximum number of BigFraction digits, which is set to 0.
* @param locale the specific locale used by the format.
* @return the default number format specific to the given locale.
*/
protected static NumberFormat getDefaultNumberFormat(final Locale locale) {
final NumberFormat nf = NumberFormat.getNumberInstance(locale);
nf.setMaximumFractionDigits(0);
nf.setParseIntegerOnly(true);
return nf;
}
/**
* Create a default number format. The default number format is based on
* {@link NumberFormat#getNumberInstance(java.util.Locale)} with the only
* customizing is the maximum number of BigFraction digits, which is set to 0.
* @param locale the specific locale used by the format.
* @return the default number format specific to the given locale.
*/
protected static NumberFormat getDefaultNumberFormat(final Locale locale) {
final NumberFormat nf = NumberFormat.getNumberInstance(locale);
nf.setMaximumFractionDigits(0);
nf.setParseIntegerOnly(true);
return nf;
}