Java Code Examples for java.text.NumberFormat#setParseIntegerOnly()

The following examples show how to use java.text.NumberFormat#setParseIntegerOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: FractionFormatTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: SingleIntegerFieldOptionsPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * 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
            }
        }
    });
}
 
Example 4
Source File: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source File: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: FractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: FractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: FractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: LongDatatype.java    From cuba with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: FractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: FractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: NumericValidator.java    From javalite with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 15
Source File: RegionRestriction.java    From rtg-tools with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * 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;
  }
}
 
Example 16
Source File: NumberFormatter.java    From Strata with Apache License 2.0 4 votes vote down vote up
/**
 * Restricted constructor.
 */
private NumberFormatter(NumberFormat format) {
  ArgChecker.notNull(format, "format");
  format.setParseIntegerOnly(false);
  this.underlying = ThreadLocal.withInitial(() -> (NumberFormat) format.clone());
}
 
Example 17
Source File: AbstractFormat.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: AbstractFormat.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: AbstractFormat.java    From hipparchus with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: AbstractFormat.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}