Example usage for java.text Format Format

List of usage examples for java.text Format Format

Introduction

In this page you can find the example usage for java.text Format Format.

Prototype

protected Format() 

Source Link

Document

Sole constructor.

Usage

From source file:Main.java

private static Format createFormat() {
    NumberFormat format = NumberFormat.getInstance();
    format.setParseIntegerOnly(true);//  w w w .java 2  s .c  o m
    return new Format() {
        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            return format.format(obj, toAppendTo, pos);
        }

        @Override
        public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
            return format.formatToCharacterIterator(obj);
        }

        @Override
        public Object parseObject(String source, ParsePosition pos) {
            int initialIndex = pos.getIndex();
            Object result = format.parseObject(source, pos);
            if (result != null && pos.getIndex() > initialIndex + 1) {
                int errorIndex = initialIndex + 1;
                pos.setIndex(initialIndex);
                pos.setErrorIndex(errorIndex);
                return null;
            }
            return result;
        }
    };
}

From source file:org.sipfoundry.sipxconfig.cdr.CdrManagerImplTest.java

public void testCdrsJsonWriter() throws Exception {
    TimeZone tz = DateUtils.UTC_TIME_ZONE;
    Calendar calendar = Calendar.getInstance(tz);

    Timestamp timestamp = new Timestamp(0);

    ResultSet rs = createMock(ResultSet.class);
    for (int i = 0; i < ColumnInfo.FIELDS.length; i++) {
        rs.findColumn((String) anyObject());
        expectLastCall().andReturn(i);/*from w  w  w. j a v  a2s . c o m*/
    }

    rs.getString(0);
    expectLastCall().andReturn("\"Brian Ferry\"<sip:1111@example.org>");
    rs.getString(1);
    expectLastCall().andReturn("sip:callee@example.com");

    rs.getTimestamp(eq(2), eqTimeZone(calendar));
    expectLastCall().andReturn(timestamp);
    rs.getTimestamp(eq(3), eqTimeZone(calendar));
    expectLastCall().andReturn(timestamp);
    rs.getTimestamp(eq(4), eqTimeZone(calendar));
    expectLastCall().andReturn(timestamp);

    rs.getString(5);
    expectLastCall().andReturn("404");

    rs.getString(6);
    expectLastCall().andReturn("I");

    rs.getString(7);
    expectLastCall().andReturn("0000-0000");

    replay(rs);

    StringWriter writer = new StringWriter();

    Format testDateFormat = new Format() {
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            return toAppendTo.append("2008-05-02T06:29:08-04:00");
        }

        public Object parseObject(String source, ParsePosition pos) {
            return null;
        }
    };

    DefaultColumnInfoFactory ciFactory = new DefaultColumnInfoFactory(tz);
    ciFactory.setDateFormat(testDateFormat);
    ciFactory.setAorFormat(CdrsJsonWriter.AOR_FORMAT);
    CdrsWriter handler = new CdrsJsonWriter(writer, ciFactory);
    handler.writeHeader();
    handler.processRow(rs);
    handler.writeFooter();

    InputStream expectedJson = getClass().getResourceAsStream("cdrs.test.json");
    assertNotNull(expectedJson);

    assertEquals(IOUtils.toString(expectedJson), writer.toString());

    verify(rs);
}

From source file:com.gmail.walles.johan.batterylogger.BatteryPlotFragment.java

private void setUpPlotLayout(final XYPlot plot) {
    final float labelHeightPixels = spToPixels(15);

    // Note that we have to set text size before label text, otherwise the label gets clipped,
    // with AndroidPlot 0.6.2-SNAPSHOT on 2014sep12 /Johan
    plot.getRangeLabelWidget().getLabelPaint().setTextSize(labelHeightPixels);
    plot.setRangeLabel("Battery Drain (%/h)");

    plot.getTitleWidget().setVisible(false);
    plot.getDomainLabelWidget().setVisible(false);
    plot.getLegendWidget().setVisible(false);

    plot.getGraphWidget().getRangeTickLabelPaint().setTextSize(labelHeightPixels);
    plot.getGraphWidget().getDomainTickLabelPaint().setTextSize(labelHeightPixels);

    // Tell the widget about how much space we should reserve for the range label widgets
    final float maxRangeLabelWidth = plot.getGraphWidget().getRangeTickLabelPaint().measureText("25.0");
    plot.getGraphWidget().setRangeTickLabelWidth(maxRangeLabelWidth);

    // Need room for top scale label
    plot.getGraphWidget().setMarginTop(labelHeightPixels);

    // Need room for domain labels
    plot.getGraphWidget().setMarginBottom(labelHeightPixels);

    // Need room for the range label
    //noinspection SuspiciousNameCombination
    plot.getGraphWidget().setMarginLeft(labelHeightPixels);

    // Prevent the leftmost part of the range labels from being clipped
    // FIXME: I don't know where the clipping comes from, fixing it properly would be better
    plot.getGraphWidget().setClippingEnabled(false);

    // Symmetry with upper and bottom
    //noinspection SuspiciousNameCombination
    plot.getGraphWidget().setMarginRight(labelHeightPixels);

    plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);
    plot.setTicksPerRangeLabel(5);//from  w w  w. j a  v a  2  s  . c  om

    plot.setTicksPerDomainLabel(1);
    plot.setDomainStep(XYStepMode.SUBDIVIDE, 4);
    plot.setDomainValueFormat(new Format() {
        @Override
        public StringBuffer format(Object o, @NotNull StringBuffer toAppendTo,
                @NotNull FieldPosition position) {
            Date timestamp = History.toDate((Number) o);
            long domainWidthSeconds = History.doubleToDeltaMs(maxX - minX) / 1000;
            SimpleDateFormat format;
            if (domainWidthSeconds < 5 * 60) {
                format = new SimpleDateFormat("HH:mm:ss");
            } else if (domainWidthSeconds < 86400) {
                format = new SimpleDateFormat("HH:mm");
            } else if (domainWidthSeconds < 86400 * 7) {
                format = new SimpleDateFormat("EEE HH:mm");
            } else {
                format = new SimpleDateFormat("MMM d");
            }
            return format.format(timestamp, toAppendTo, position);
        }

        @Override
        @Nullable
        public Object parseObject(String s, @NotNull ParsePosition parsePosition) {
            return null;
        }
    });

    plot.calculateMinMaxVals();
    minX = plot.getCalculatedMinX().doubleValue();
    maxX = plot.getCalculatedMaxX().doubleValue();
    Date now = new Date();
    if (maxX < History.toDouble(now)) {
        maxX = History.toDouble(now);
    }
    Date fiveMinutesAgo = new Date(now.getTime() - History.FIVE_MINUTES_MS);
    if (minX > History.toDouble(fiveMinutesAgo)) {
        minX = History.toDouble(fiveMinutesAgo);
    }

    originalMinX = minX;
    originalMaxX = maxX;

    plot.setDomainBoundaries(minX, maxX, BoundaryMode.FIXED);

    double maxY = plot.getCalculatedMaxY().doubleValue();
    if (maxY < 5) {
        maxY = 5;
    }
    if (maxY > 25) {
        // We sometimes get unreasonable outliers, clamp them so they don't make the graph unreadable
        maxY = 25;
    }

    plot.setRangeBoundaries(0, maxY, BoundaryMode.FIXED);
}