@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); } });
@Test public void testCustomNumberEditor() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); editor.setAsText("5"); assertEquals(new Integer(5), editor.getValue()); assertEquals("5", editor.getAsText()); editor.setValue(null); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); }
@Test public void testCustomNumberEditorWithHex() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); editor.setAsText("0x" + Integer.toHexString(64)); assertEquals(new Integer(64), editor.getValue()); }
@Test public void testParseShortGreaterThanMaxValueWithoutNumberFormat() { try { CustomNumberEditor editor = new CustomNumberEditor(Short.class, true); editor.setAsText(String.valueOf(Short.MAX_VALUE + 1)); fail(Short.MAX_VALUE + 1 + " is greater than max value"); } catch (NumberFormatException ex) { // expected } }
/** * Parse the Number from the given text, using the specified NumberFormat. */ @Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (this.numberFormat != null) { // Use given NumberFormat for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat)); } else { // Use default valueOf methods for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass)); } }
/** * Format the Number as String, using the specified NumberFormat. */ @Override public String getAsText() { Object value = getValue(); if (value == null) { return ""; } if (this.numberFormat != null) { // Use NumberFormat for rendering value. return this.numberFormat.format(value); } else { // Use toString method for rendering value. return value.toString(); } }
/** * Parse the Number from the given text, using the specified NumberFormat. */ @Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (this.numberFormat != null) { // Use given NumberFormat for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat)); } else { // Use default valueOf methods for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass)); } }
/** * Format the Number as String, using the specified NumberFormat. */ @Override public String getAsText() { Object value = getValue(); if (value == null) { return ""; } if (this.numberFormat != null) { // Use NumberFormat for rendering value. return this.numberFormat.format(value); } else { // Use toString method for rendering value. return value.toString(); } }
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); } });
@Test public void testCustomNumberEditorWithEmptyAsNull() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true); editor.setAsText("5"); assertEquals(new Integer(5), editor.getValue()); assertEquals("5", editor.getAsText()); editor.setAsText(""); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); editor.setValue(null); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); }
/** * Parse the Number from the given text, using the specified NumberFormat. */ @Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (this.numberFormat != null) { // Use given NumberFormat for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat)); } else { // Use default valueOf methods for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass)); } }
/** * Format the Number as String, using the specified NumberFormat. */ @Override public String getAsText() { Object value = getValue(); if (value == null) { return ""; } if (this.numberFormat != null) { // Use NumberFormat for rendering value. return this.numberFormat.format(value); } else { // Use toString method for rendering value. return value.toString(); } }
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } });
/** * Format the Number as String, using the specified NumberFormat. */ public String getAsText() { Object value = getValue(); if (value == null) { return ""; } if (this.numberFormat != null) { // Use NumberFormat for rendering value. return this.numberFormat.format(value); } else { // Use toString method for rendering value. return value.toString(); } }
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } });
/** * Parse the Number from the given text, using the specified NumberFormat. */ public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (this.numberFormat != null) { // Use given NumberFormat for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat)); } else { // Use default valueOf methods for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass)); } }
/** * Format the Number as String, using the specified NumberFormat. */ @Override public String getAsText() { Object value = getValue(); if (value == null) { return ""; } if (this.numberFormat != null) { // Use NumberFormat for rendering value. return this.numberFormat.format(value); } else { // Use toString method for rendering value. return value.toString(); } }
this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false)); this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true)); this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false)); this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true)); this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false)); this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false)); this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true)); this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
/** * Parse the Number from the given text, using the specified NumberFormat. */ @Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (this.numberFormat != null) { // Use given NumberFormat for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat)); } else { // Use default valueOf methods for parsing text. setValue(NumberUtils.parseNumber(text, this.numberClass)); } }