View Javadoc

1   /*
2    *  File: MillisecondIterator.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.date.iterator;
12  
13  import java.text.DecimalFormat;
14  import java.text.NumberFormat;
15  
16  import de.jaret.util.date.JaretDate;
17  
18  /**
19   * Implementation of the DateIterator for seconds.
20   * 
21   * @author kliem
22   * @version $Id: MillisecondIterator.java 829 2009-02-08 14:00:40Z kliem $
23   */
24  public class MillisecondIterator extends AbstractDateIterator implements DateIterator {
25      /** default formatter. */
26      protected IIteratorFormatter _defaultFormatter = new IIteratorFormatter() {
27  
28          /**
29           * {@inheritDoc}
30           */
31          public String getLabel(JaretDate date, Format format) {
32              if (format.equals(Format.SHORT)) {
33                  return date.getMillis() + "ms";
34              } else if (format.equals(Format.MEDIUM)) {
35                  return NF.format(date.getSeconds()) + "'' " + NF.format(date.getMillis());
36              } else {
37                  String str = NF.format(date.getHours()) + ":" + NF.format(date.getMinutes()) + ":"
38                          + NF.format(date.getSeconds());
39                  int millis = date.getMillis();
40                  String str2 = NF2.format(millis);
41                  return str + ":" + str2;
42              }
43          }
44      };
45      /** default step if none is set. */
46      private static final int DEFAULT_STEP = 100;
47  
48      /** step for seconds. */
49      protected long _millisecondStep = DEFAULT_STEP;
50  
51      /** Number format for labels (2 digits). */
52      protected static final NumberFormat NF = new DecimalFormat();
53      /** Number format for labels (3 digits). */
54      protected static final NumberFormat NF2 = new DecimalFormat();
55      static {
56          NF.setMaximumFractionDigits(0);
57          NF.setMinimumIntegerDigits(2);
58          NF2.setMaximumFractionDigits(0);
59          NF2.setMinimumIntegerDigits(3);
60      }
61  
62      /**
63       * Contructor supplying the setp.
64       * 
65       * @param millisecondStep number of milli seconds between steps
66       */
67      public MillisecondIterator(long millisecondStep) {
68          _millisecondStep = millisecondStep;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      protected void advanceDate(JaretDate date) {
75          date.advanceMillis(_millisecondStep);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      public long getApproxStepMilliSeconds() {
82          return _millisecondStep;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      protected JaretDate correctStartDate(JaretDate date) {
89          long diff = date.getMillis() % _millisecondStep;
90          date.advanceMillis(-diff);
91  
92          return date;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      protected IIteratorFormatter getDefaultFormatter() {
99          return _defaultFormatter;
100     }
101 
102 }