View Javadoc

1   /*
2    *  File: SecondIterator.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: SecondIterator.java 828 2009-02-08 13:58:21Z kliem $
23   */
24  public class SecondIterator 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.getSeconds() + "''";
34              } else if (format.equals(Format.MEDIUM)) {
35                  return NF.format(date.getMinutes()) + "' " + NF.format(date.getSeconds());
36              } else {
37                  return NF.format(date.getHours()) + ":" + NF.format(date.getMinutes()) + ":"
38                          + NF.format(date.getSeconds());
39              }
40          }
41      };
42      /** default step if none is set. */
43      private static final int DEFAULT_STEP = 30;
44  
45      /** step for seconds. */
46      protected int _secondStep = DEFAULT_STEP;
47  
48      /** Number format for labels. */
49      protected static final NumberFormat NF = new DecimalFormat();
50      static {
51          NF.setMaximumFractionDigits(0);
52          NF.setMinimumIntegerDigits(2);
53      }
54  
55      /**
56       * Contructor supplying the setp.
57       * 
58       * @param secondStep number of seconds between steps
59       */
60      public SecondIterator(int secondStep) {
61          _secondStep = secondStep;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      protected void advanceDate(JaretDate date) {
68          date.advanceSeconds(_secondStep);
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public long getApproxStepMilliSeconds() {
75          return _secondStep * 1000;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      protected JaretDate correctStartDate(JaretDate date) {
82          date.setSeconds(0);
83          date.setMilliseconds(0);
84  
85          int diff = date.getSeconds() % _secondStep;
86          date.backSeconds(diff);
87  
88          return date;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      protected IIteratorFormatter getDefaultFormatter() {
95          return _defaultFormatter;
96      }
97  }