root/trunk/src/java/org/jcoderz/commons/logging/TimestampFormat.java

Revision 1011, 4.0 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.logging;
34
35import java.text.FieldPosition;
36import java.text.Format;
37import java.text.ParseException;
38import java.text.ParsePosition;
39
40import org.jcoderz.commons.types.Date;
41
42
43
44/**
45 * This format is used for formatting and parsing an object of type
46 * {@link org.jcoderz.commons.types.Date} with default settings.
47 *
48 */
49public class TimestampFormat
50      extends Format
51{
52   private static final long serialVersionUID = 3256719572219212851L;
53
54   // All formatted default Date objects are assumed to be of this length.
55   private static final int DATE_SIZE
56         = Date.now().toString().length();
57
58   /**
59    * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
60    *
61    * Parses the source string for an instance of type
62    * {@link org.jcoderz.commons.types.Date}. Assumes all default Dates to be
63    * of the same fix length.
64    *
65    * @param source The string to parse.
66    * @param pos the positionn within <code>source</code>.
67    *
68    * @return the Date object being parsed from <code>source</code>.
69    */
70   public Object parseObject (String source, ParsePosition pos)
71   {
72      Date rc = null;
73      try
74      {
75         rc = Date.fromString(
76               source.substring(pos.getIndex(), pos.getIndex() + DATE_SIZE));
77         pos.setIndex(pos.getIndex() + DATE_SIZE);
78      }
79      catch (ParseException pex)
80      {
81         rc = null;
82         pos.setErrorIndex(pos.getIndex());
83      }
84      return rc;
85   }
86
87   /**
88    * Formats the supplied Date object by calling its toString() method.
89    *
90    * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
91    *
92    * @param obj the object to format, must be an instance of type
93    * {@link org.jcoderz.commons.types.Date}.
94    * @param toAppendTo the StringBuffer where to append to the formatted
95    * object.
96    * @param pos the field position.
97    *
98    * @return StringBuffer where the formatted Object has been appended to.
99    */
100   public StringBuffer format (Object obj, StringBuffer toAppendTo,
101         FieldPosition pos)
102   {
103      if (! (obj instanceof Date))
104      {
105         throw new IllegalArgumentException("The supplied object must be a "
106               + Date.class.getName() + " but is " + obj.getClass().getName());
107      }
108      pos.setBeginIndex(0);
109      pos.setEndIndex(0);
110      toAppendTo.append(((Date) obj).toString());
111      return toAppendTo;
112   }
113}
Note: See TracBrowser for help on using the browser.