Author: michiel Date: 2009-09-29 23:16:50 +0200 (Tue, 29 Sep 2009) New Revision: 38929
Modified: mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatFileSize.java mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatQuantity.java mmbase/trunk/core/src/test/java/org/mmbase/datatypes/processors/FormatQuantityTest.java Log: made file size processor a bit more sophisticated. We know that filesizes are integer, so negative power prefixes are not used. Modified: mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatFileSize.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatFileSize.java 2009-09-29 21:15:30 UTC (rev 38928) +++ mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatFileSize.java 2009-09-29 21:16:50 UTC (rev 38929) @@ -31,6 +31,7 @@ public FormatFileSize() { setClassical(false); + setInteger(true); } /** * It was commonplace to mix SI prefixes with 'binary' factors. Modified: mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatQuantity.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatQuantity.java 2009-09-29 21:15:30 UTC (rev 38928) +++ mmbase/trunk/core/src/main/java/org/mmbase/datatypes/processors/FormatQuantity.java 2009-09-29 21:16:50 UTC (rev 38929) @@ -46,11 +46,13 @@ protected BigDecimal k = KILO; protected String[] prefixes = SI; protected String unit = ""; - protected String lowFormat = "0.0 "; - protected String highFormat = "0 "; + protected String lowFormat = "0.0"; + protected String highFormat = "0"; protected BigDecimal lowLimit = new BigDecimal(15); protected BigDecimal limit = k.multiply(new BigDecimal(2)); + protected boolean integer = false; + /** * If set, will use binary prefixes as recommended by <a href="http://en.wikipedia.org/wiki/IEEE_1541-2002">IEEE 1541</a>. So, Ki, Mi, etc. which * are multiples of 1024. Otherwise normal SI prefixes are applied (k, M, G etc), which are multiples of @@ -124,11 +126,23 @@ } + /** + * If a quantity is 'integer' then it can not have fractional + * values. For example a number of bytes. + * @since MMBase-1.9.2 + */ + public void setInteger(boolean i) { + integer = i; + } + + public Object process(Node node, Field field, Object value) { - if (value == null) return null; + if (value == null) return ""; BigDecimal v = org.mmbase.util.Casting.toDecimal(value); - log.debug("Formatting " + value + " -> " + v); + if (log.isDebugEnabled()) { + log.debug("Formatting " + value + " -> " + v); + } BigDecimal av = v.abs(); BigDecimal factor = BigDecimal.ONE; @@ -140,7 +154,7 @@ factor = factor.multiply(k); power++; } - } else { + } else if (! integer) { BigDecimal inverse = BigDecimal.ONE.divide(av, RoundingMode.HALF_UP); while (inverse.compareTo(factor.multiply(limit)) > 0 && -power < SI_NEGATIVE.length) { @@ -169,11 +183,20 @@ ((DecimalFormat) nf).applyPattern(av.compareTo(lowLimit) > 0 ? highFormat : lowFormat); } - StringBuffer buf = nf.format(v.doubleValue(), new StringBuffer(), new FieldPosition(0)); + StringBuffer buf; + if (integer && power == 0) { + buf = new StringBuffer("" + v.intValue()); + } else { + buf = nf.format(v.doubleValue(), new StringBuffer(), new FieldPosition(0)); + } if (power > 0) { + buf.append(' '); buf.append(prefixes[power - 1]); } else if (power < 0) { + buf.append(' '); buf.append(SI_NEGATIVE[-1 - power]); + } else if (unit.length() > 0) { + buf.append(' '); } buf.append(unit); return buf.toString(); Modified: mmbase/trunk/core/src/test/java/org/mmbase/datatypes/processors/FormatQuantityTest.java =================================================================== --- mmbase/trunk/core/src/test/java/org/mmbase/datatypes/processors/FormatQuantityTest.java 2009-09-29 21:15:30 UTC (rev 38928) +++ mmbase/trunk/core/src/test/java/org/mmbase/datatypes/processors/FormatQuantityTest.java 2009-09-29 21:16:50 UTC (rev 38929) @@ -36,9 +36,23 @@ FormatQuantity fileSize = new FormatFileSize(); assertEquals("123 B", "" + fileSize.process(null, null, "123")); assertEquals("123 B", "" + fileSize.process(null, null, 123)); - assertEquals("3.0 KiB", "" + fileSize.process(null, null, "3061")); + assertEquals("0 B", "" + fileSize.process(null, null, 0)); + assertEquals("", "" + fileSize.process(null, null, null)); + assertEquals("-1 B", "" + fileSize.process(null, null, -1)); + assertEquals("0 B", "" + fileSize.process(null, null, 0.1)); } + + @Test + public void unitLessInteger() { + FormatQuantity def = new FormatQuantity(); + def.setInteger(true); + assertEquals("123", "" + def.process(null, null, "123")); + assertEquals("123", "" + def.process(null, null, 123)); + assertEquals("3.0 k", "" + def.process(null, null, 3061)); + assertEquals("", "" + def.process(null, null, null)); + assertEquals("-1", "" + def.process(null, null, -1)); + } _______________________________________________ Cvs mailing list Cvs@lists.mmbase.org http://lists.mmbase.org/mailman/listinfo/cvs