Source Code Cross Referenced for DateTickUnit.java in  » Chart » jfreechart » org » jfree » chart » axis » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chart » jfreechart » org.jfree.chart.axis 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ===========================================================
002:         * JFreeChart : a free chart library for the Java(tm) platform
003:         * ===========================================================
004:         *
005:         * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006:         *
007:         * Project Info:  http://www.jfree.org/jfreechart/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it 
010:         * under the terms of the GNU Lesser General Public License as published by 
011:         * the Free Software Foundation; either version 2.1 of the License, or 
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but 
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025:         * in the United States and other countries.]
026:         *
027:         * -----------------
028:         * DateTickUnit.java
029:         * -----------------
030:         * (C) Copyright 2000-2007, by Object Refinery Limited.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   Chris Boek;
034:         *
035:         * $Id: DateTickUnit.java,v 1.7.2.3 2007/04/04 10:58:19 mungady Exp $
036:         *
037:         * Changes (from 8-Nov-2002)
038:         * --------------------------
039:         * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
040:         * 27-Nov-2002 : Added IllegalArgumentException to getMillisecondCount() 
041:         *               method (DG);
042:         * 26-Mar-2003 : Implemented Serializable (DG);
043:         * 12-Nov-2003 : Added roll fields that can improve the labelling on segmented 
044:         *               date axes (DG);
045:         * 03-Dec-2003 : DateFormat constructor argument is now filled with an default 
046:         *               if null (TM);
047:         * 07-Dec-2003 : Fixed bug (null pointer exception) in constructor (DG);
048:         * ------------- JFREECHART 1.0.x ---------------------------------------------
049:         * 21-Mar-2007 : Added toString() for debugging (DG);
050:         * 04-Apr-2007 : Added new methods addToDate(Date, TimeZone) and rollDate(Date, 
051:         *               TimeZone) (CB);
052:         *
053:         */
054:
055:        package org.jfree.chart.axis;
056:
057:        import java.io.Serializable;
058:        import java.text.DateFormat;
059:        import java.util.Calendar;
060:        import java.util.Date;
061:        import java.util.TimeZone;
062:
063:        import org.jfree.util.ObjectUtilities;
064:
065:        /**
066:         * A tick unit for use by subclasses of {@link DateAxis}. Instances of this 
067:         * class are immutable.
068:         */
069:        public class DateTickUnit extends TickUnit implements  Serializable {
070:
071:            /** For serialization. */
072:            private static final long serialVersionUID = -7289292157229621901L;
073:
074:            /** A constant for years. */
075:            public static final int YEAR = 0;
076:
077:            /** A constant for months. */
078:            public static final int MONTH = 1;
079:
080:            /** A constant for days. */
081:            public static final int DAY = 2;
082:
083:            /** A constant for hours. */
084:            public static final int HOUR = 3;
085:
086:            /** A constant for minutes. */
087:            public static final int MINUTE = 4;
088:
089:            /** A constant for seconds. */
090:            public static final int SECOND = 5;
091:
092:            /** A constant for milliseconds. */
093:            public static final int MILLISECOND = 6;
094:
095:            /** The unit. */
096:            private int unit;
097:
098:            /** The unit count. */
099:            private int count;
100:
101:            /** The roll unit. */
102:            private int rollUnit;
103:
104:            /** The roll count. */
105:            private int rollCount;
106:
107:            /** The date formatter. */
108:            private DateFormat formatter;
109:
110:            /**
111:             * Creates a new date tick unit.  The dates will be formatted using a 
112:             * SHORT format for the default locale.
113:             *
114:             * @param unit  the unit.
115:             * @param count  the unit count.
116:             */
117:            public DateTickUnit(int unit, int count) {
118:                this (unit, count, null);
119:            }
120:
121:            /**
122:             * Creates a new date tick unit.  You can specify the units using one of 
123:             * the constants YEAR, MONTH, DAY, HOUR, MINUTE, SECOND or MILLISECOND.  
124:             * In addition, you can specify a unit count, and a date format.
125:             *
126:             * @param unit  the unit.
127:             * @param count  the unit count.
128:             * @param formatter  the date formatter (defaults to DateFormat.SHORT).
129:             */
130:            public DateTickUnit(int unit, int count, DateFormat formatter) {
131:
132:                this (unit, count, unit, count, formatter);
133:
134:            }
135:
136:            /**
137:             * Creates a new unit.
138:             *
139:             * @param unit  the unit.
140:             * @param count  the count.
141:             * @param rollUnit  the roll unit.
142:             * @param rollCount  the roll count.
143:             * @param formatter  the date formatter (defaults to DateFormat.SHORT).
144:             */
145:            public DateTickUnit(int unit, int count, int rollUnit,
146:                    int rollCount, DateFormat formatter) {
147:                super (DateTickUnit.getMillisecondCount(unit, count));
148:                this .unit = unit;
149:                this .count = count;
150:                this .rollUnit = rollUnit;
151:                this .rollCount = rollCount;
152:                this .formatter = formatter;
153:                if (formatter == null) {
154:                    this .formatter = DateFormat
155:                            .getDateInstance(DateFormat.SHORT);
156:                }
157:            }
158:
159:            /**
160:             * Returns the date unit.  This will be one of the constants 
161:             * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>, 
162:             * <code>HOUR</code>, <code>MINUTE</code>, <code>SECOND</code> or 
163:             * <code>MILLISECOND</code>, defined by this class.  Note that these 
164:             * constants do NOT correspond to those defined in Java's 
165:             * <code>Calendar</code> class.
166:             *
167:             * @return The date unit.
168:             */
169:            public int getUnit() {
170:                return this .unit;
171:            }
172:
173:            /**
174:             * Returns the unit count.
175:             *
176:             * @return The unit count.
177:             */
178:            public int getCount() {
179:                return this .count;
180:            }
181:
182:            /**
183:             * Returns the roll unit.  This is the amount by which the tick advances if
184:             * it is "hidden" when displayed on a segmented date axis.  Typically the 
185:             * roll will be smaller than the regular tick unit (for example, a 7 day 
186:             * tick unit might use a 1 day roll).
187:             *
188:             * @return The roll unit.
189:             */
190:            public int getRollUnit() {
191:                return this .rollUnit;
192:            }
193:
194:            /**
195:             * Returns the roll count.
196:             *
197:             * @return The roll count.
198:             */
199:            public int getRollCount() {
200:                return this .rollCount;
201:            }
202:
203:            /**
204:             * Formats a value.
205:             *
206:             * @param milliseconds  date in milliseconds since 01-01-1970.
207:             *
208:             * @return The formatted date.
209:             */
210:            public String valueToString(double milliseconds) {
211:                return this .formatter.format(new Date((long) milliseconds));
212:            }
213:
214:            /**
215:             * Formats a date using the tick unit's formatter.
216:             *
217:             * @param date  the date.
218:             *
219:             * @return The formatted date.
220:             */
221:            public String dateToString(Date date) {
222:                return this .formatter.format(date);
223:            }
224:
225:            /**
226:             * Calculates a new date by adding this unit to the base date.
227:             *
228:             * @param base  the base date.
229:             *
230:             * @return A new date one unit after the base date.
231:             * 
232:             * @see #addToDate(Date, TimeZone)
233:             */
234:            public Date addToDate(Date base) {
235:                Calendar calendar = Calendar.getInstance();
236:                calendar.setTime(base);
237:                calendar.add(getCalendarField(this .unit), this .count);
238:                return calendar.getTime();
239:            }
240:
241:            /**
242:             * Calculates a new date by adding this unit to the base date.
243:             *
244:             * @param base  the base date.
245:             * @param zone  the time zone for the date calculation.
246:             *
247:             * @return A new date one unit after the base date.
248:             * 
249:             * @since 1.0.6
250:             * @see #addToDate(Date)
251:             */
252:            public Date addToDate(Date base, TimeZone zone) {
253:                Calendar calendar = Calendar.getInstance(zone);
254:                calendar.setTime(base);
255:                calendar.add(getCalendarField(this .unit), this .count);
256:                return calendar.getTime();
257:            }
258:
259:            /**
260:             * Rolls the date forward by the amount specified by the roll unit and 
261:             * count.
262:             *
263:             * @param base  the base date.
264:
265:             * @return The rolled date.
266:             * 
267:             * @see #rollDate(Date, TimeZone)
268:             */
269:            public Date rollDate(Date base) {
270:                Calendar calendar = Calendar.getInstance();
271:                calendar.setTime(base);
272:                calendar.add(getCalendarField(this .rollUnit), this .rollCount);
273:                return calendar.getTime();
274:            }
275:
276:            /**
277:             * Rolls the date forward by the amount specified by the roll unit and 
278:             * count.
279:             *
280:             * @param base  the base date.
281:             * @param zone  the time zone.
282:             * 
283:             * @return The rolled date.
284:             * 
285:             * @since 1.0.6
286:             * @see #rollDate(Date)
287:             */
288:            public Date rollDate(Date base, TimeZone zone) {
289:                Calendar calendar = Calendar.getInstance(zone);
290:                calendar.setTime(base);
291:                calendar.add(getCalendarField(this .rollUnit), this .rollCount);
292:                return calendar.getTime();
293:            }
294:
295:            /**
296:             * Returns a field code that can be used with the <code>Calendar</code> 
297:             * class.
298:             *
299:             * @return The field code.
300:             */
301:            public int getCalendarField() {
302:                return getCalendarField(this .unit);
303:            }
304:
305:            /**
306:             * Returns a field code (that can be used with the Calendar class) for a 
307:             * given 'unit' code.  The 'unit' is one of:  {@link #YEAR}, {@link #MONTH},
308:             * {@link #DAY}, {@link #HOUR}, {@link #MINUTE}, {@link #SECOND} and 
309:             * {@link #MILLISECOND}.
310:             *
311:             * @param tickUnit  the unit.
312:             *
313:             * @return The field code.
314:             */
315:            private int getCalendarField(int tickUnit) {
316:
317:                switch (tickUnit) {
318:                case (YEAR):
319:                    return Calendar.YEAR;
320:                case (MONTH):
321:                    return Calendar.MONTH;
322:                case (DAY):
323:                    return Calendar.DATE;
324:                case (HOUR):
325:                    return Calendar.HOUR_OF_DAY;
326:                case (MINUTE):
327:                    return Calendar.MINUTE;
328:                case (SECOND):
329:                    return Calendar.SECOND;
330:                case (MILLISECOND):
331:                    return Calendar.MILLISECOND;
332:                default:
333:                    return Calendar.MILLISECOND;
334:                }
335:
336:            }
337:
338:            /**
339:             * Returns the (approximate) number of milliseconds for the given unit and 
340:             * unit count.
341:             * <P>
342:             * This value is an approximation some of the time (e.g. months are 
343:             * assumed to have 31 days) but this shouldn't matter.
344:             *
345:             * @param unit  the unit.
346:             * @param count  the unit count.
347:             *
348:             * @return The number of milliseconds.
349:             */
350:            private static long getMillisecondCount(int unit, int count) {
351:
352:                switch (unit) {
353:                case (YEAR):
354:                    return (365L * 24L * 60L * 60L * 1000L) * count;
355:                case (MONTH):
356:                    return (31L * 24L * 60L * 60L * 1000L) * count;
357:                case (DAY):
358:                    return (24L * 60L * 60L * 1000L) * count;
359:                case (HOUR):
360:                    return (60L * 60L * 1000L) * count;
361:                case (MINUTE):
362:                    return (60L * 1000L) * count;
363:                case (SECOND):
364:                    return 1000L * count;
365:                case (MILLISECOND):
366:                    return count;
367:                default:
368:                    throw new IllegalArgumentException(
369:                            "DateTickUnit.getMillisecondCount() : unit must "
370:                                    + "be one of the constants YEAR, MONTH, DAY, HOUR, MINUTE, "
371:                                    + "SECOND or MILLISECOND defined in the DateTickUnit "
372:                                    + "class. Do *not* use the constants defined in "
373:                                    + "java.util.Calendar.");
374:                }
375:
376:            }
377:
378:            /**
379:             * Tests this unit for equality with another object.
380:             *
381:             * @param obj  the object (<code>null</code> permitted).
382:             *
383:             * @return <code>true</code> or <code>false</code>.
384:             */
385:            public boolean equals(Object obj) {
386:                if (obj == this ) {
387:                    return true;
388:                }
389:                if (!(obj instanceof  DateTickUnit)) {
390:                    return false;
391:                }
392:                if (!super .equals(obj)) {
393:                    return false;
394:                }
395:                DateTickUnit that = (DateTickUnit) obj;
396:                if (this .unit != that.unit) {
397:                    return false;
398:                }
399:                if (this .count != that.count) {
400:                    return false;
401:                }
402:                if (!ObjectUtilities.equal(this .formatter, that.formatter)) {
403:                    return false;
404:                }
405:                return true;
406:            }
407:
408:            /**
409:             * Returns a hash code for this object.
410:             * 
411:             * @return A hash code.
412:             */
413:            public int hashCode() {
414:                int result = 19;
415:                result = 37 * result + this .unit;
416:                result = 37 * result + this .count;
417:                result = 37 * result + this .formatter.hashCode();
418:                return result;
419:            }
420:
421:            /**
422:             * Strings for use by the toString() method.
423:             */
424:            private static final String[] units = { "YEAR", "MONTH", "DAY",
425:                    "HOUR", "MINUTE", "SECOND", "MILLISECOND" };
426:
427:            /**
428:             * Returns a string representation of this instance, primarily used for
429:             * debugging purposes.
430:             *
431:             * @return A string representation of this instance.
432:             */
433:            public String toString() {
434:                return "DateTickUnit[" + DateTickUnit.units[this .unit] + ", "
435:                        + this .count + "]";
436:            }
437:
438:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.