Source Code Cross Referenced for Second.java in  » Chart » jfreechart » org » jfree » data » time » 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.data.time 
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-2006, 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:         * Second.java
029:         * -----------
030:         * (C) Copyright 2001-2006, by Object Refinery Limited.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   -;
034:         *
035:         * $Id: Second.java,v 1.6.2.3 2006/10/06 14:00:15 mungady Exp $
036:         *
037:         * Changes
038:         * -------
039:         * 11-Oct-2001 : Version 1 (DG);
040:         * 18-Dec-2001 : Changed order of parameters in constructor (DG);
041:         * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG);
042:         * 14-Feb-2002 : Fixed bug in Second(Date) constructor, and changed start of 
043:         *               range to zero from one (DG);
044:         * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to 
045:         *               evaluate with reference to a particular time zone (DG);
046:         * 13-Mar-2002 : Added parseSecond() method (DG);
047:         * 10-Sep-2002 : Added getSerialIndex() method (DG);
048:         * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
049:         * 10-Jan-2003 : Changed base class and method names (DG);
050:         * 05-Mar-2003 : Fixed bug in getLastMillisecond() picked up in JUnit 
051:         *               tests (DG);
052:         * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented 
053:         *               Serializable (DG);
054:         * 21-Oct-2003 : Added hashCode() method (DG);
055:         * ------------- JFREECHART 1.0.x ---------------------------------------------
056:         * 05-Oct-2006 : Updated API docs (DG);
057:         * 06-Oct-2006 : Refactored to cache first and last millisecond values (DG);
058:         *
059:         */
060:
061:        package org.jfree.data.time;
062:
063:        import java.io.Serializable;
064:        import java.util.Calendar;
065:        import java.util.Date;
066:        import java.util.TimeZone;
067:
068:        /**
069:         * Represents a second in a particular day.  This class is immutable, which is 
070:         * a requirement for all {@link RegularTimePeriod} subclasses.
071:         */
072:        public class Second extends RegularTimePeriod implements  Serializable {
073:
074:            /** For serialization. */
075:            private static final long serialVersionUID = -6536564190712383466L;
076:
077:            /** Useful constant for the first second in a minute. */
078:            public static final int FIRST_SECOND_IN_MINUTE = 0;
079:
080:            /** Useful constant for the last second in a minute. */
081:            public static final int LAST_SECOND_IN_MINUTE = 59;
082:
083:            /** The day. */
084:            private Day day;
085:
086:            /** The hour of the day. */
087:            private byte hour;
088:
089:            /** The minute. */
090:            private byte minute;
091:
092:            /** The second. */
093:            private byte second;
094:
095:            /** 
096:             * The first millisecond.  We don't store the last millisecond, because it
097:             * is always firstMillisecond + 999L.
098:             */
099:            private long firstMillisecond;
100:
101:            /**
102:             * Constructs a new Second, based on the system date/time.
103:             */
104:            public Second() {
105:                this (new Date());
106:            }
107:
108:            /**
109:             * Constructs a new Second.
110:             *
111:             * @param second  the second (0 to 24*60*60-1).
112:             * @param minute  the minute (<code>null</code> not permitted).
113:             */
114:            public Second(int second, Minute minute) {
115:                if (minute == null) {
116:                    throw new IllegalArgumentException(
117:                            "Null 'minute' argument.");
118:                }
119:                this .day = minute.getDay();
120:                this .hour = (byte) minute.getHourValue();
121:                this .minute = (byte) minute.getMinute();
122:                this .second = (byte) second;
123:                peg(Calendar.getInstance());
124:            }
125:
126:            /**
127:             * Creates a new second.
128:             * 
129:             * @param second  the second (0-59).
130:             * @param minute  the minute (0-59).
131:             * @param hour  the hour (0-23).
132:             * @param day  the day (1-31).
133:             * @param month  the month (1-12).
134:             * @param year  the year (1900-9999).
135:             */
136:            public Second(int second, int minute, int hour, int day, int month,
137:                    int year) {
138:                this (second, new Minute(minute, hour, day, month, year));
139:            }
140:
141:            /**
142:             * Constructs a second.
143:             *
144:             * @param time  the time.
145:             */
146:            public Second(Date time) {
147:                this (time, RegularTimePeriod.DEFAULT_TIME_ZONE);
148:            }
149:
150:            /**
151:             * Creates a new second based on the supplied time and time zone.
152:             *
153:             * @param time  the instant in time.
154:             * @param zone  the time zone.
155:             */
156:            public Second(Date time, final TimeZone zone) {
157:                Calendar calendar = Calendar.getInstance(zone);
158:                calendar.setTime(time);
159:                this .second = (byte) calendar.get(Calendar.SECOND);
160:                this .minute = (byte) calendar.get(Calendar.MINUTE);
161:                this .hour = (byte) calendar.get(Calendar.HOUR_OF_DAY);
162:                this .day = new Day(time, zone);
163:                peg(calendar);
164:            }
165:
166:            /**
167:             * Returns the second within the minute.
168:             *
169:             * @return The second (0 - 59).
170:             */
171:            public int getSecond() {
172:                return this .second;
173:            }
174:
175:            /**
176:             * Returns the minute.
177:             *
178:             * @return The minute (never <code>null</code>).
179:             */
180:            public Minute getMinute() {
181:                return new Minute(this .minute, new Hour(this .hour, this .day));
182:            }
183:
184:            /**
185:             * Returns the first millisecond of the second.  This will be determined 
186:             * relative to the time zone specified in the constructor, or in the 
187:             * calendar instance passed in the most recent call to the 
188:             * {@link #peg(Calendar)} method.
189:             *
190:             * @return The first millisecond of the second.
191:             * 
192:             * @see #getLastMillisecond()
193:             */
194:            public long getFirstMillisecond() {
195:                return this .firstMillisecond;
196:            }
197:
198:            /**
199:             * Returns the last millisecond of the second.  This will be 
200:             * determined relative to the time zone specified in the constructor, or
201:             * in the calendar instance passed in the most recent call to the 
202:             * {@link #peg(Calendar)} method.
203:             *
204:             * @return The last millisecond of the second.
205:             * 
206:             * @see #getFirstMillisecond()
207:             */
208:            public long getLastMillisecond() {
209:                return this .firstMillisecond + 999L;
210:            }
211:
212:            /** 
213:             * Recalculates the start date/time and end date/time for this time period 
214:             * relative to the supplied calendar (which incorporates a time zone).
215:             * 
216:             * @param calendar  the calendar (<code>null</code> not permitted).
217:             * 
218:             * @since 1.0.3
219:             */
220:            public void peg(Calendar calendar) {
221:                this .firstMillisecond = getFirstMillisecond(calendar);
222:            }
223:
224:            /**
225:             * Returns the second preceding this one.
226:             *
227:             * @return The second preceding this one.
228:             */
229:            public RegularTimePeriod previous() {
230:
231:                Second result = null;
232:                if (this .second != FIRST_SECOND_IN_MINUTE) {
233:                    result = new Second(this .second - 1, getMinute());
234:                } else {
235:                    Minute previous = (Minute) getMinute().previous();
236:                    if (previous != null) {
237:                        result = new Second(LAST_SECOND_IN_MINUTE, previous);
238:                    }
239:                }
240:                return result;
241:
242:            }
243:
244:            /**
245:             * Returns the second following this one.
246:             *
247:             * @return The second following this one.
248:             */
249:            public RegularTimePeriod next() {
250:
251:                Second result = null;
252:                if (this .second != LAST_SECOND_IN_MINUTE) {
253:                    result = new Second(this .second + 1, getMinute());
254:                } else {
255:                    Minute next = (Minute) getMinute().next();
256:                    if (next != null) {
257:                        result = new Second(FIRST_SECOND_IN_MINUTE, next);
258:                    }
259:                }
260:                return result;
261:
262:            }
263:
264:            /**
265:             * Returns a serial index number for the minute.
266:             *
267:             * @return The serial index number.
268:             */
269:            public long getSerialIndex() {
270:                long hourIndex = this .day.getSerialIndex() * 24L + this .hour;
271:                long minuteIndex = hourIndex * 60L + this .minute;
272:                return minuteIndex * 60L + this .second;
273:            }
274:
275:            /**
276:             * Returns the first millisecond of the minute.
277:             *
278:             * @param calendar  the calendar/timezone (<code>null</code> not permitted).
279:             *
280:             * @return The first millisecond.
281:             *
282:             * @throws NullPointerException if <code>calendar</code> is 
283:             *     <code>null</code>.
284:             */
285:            public long getFirstMillisecond(Calendar calendar) {
286:                int year = this .day.getYear();
287:                int month = this .day.getMonth() - 1;
288:                int day = this .day.getDayOfMonth();
289:                calendar.clear();
290:                calendar.set(year, month, day, this .hour, this .minute,
291:                        this .second);
292:                calendar.set(Calendar.MILLISECOND, 0);
293:                //return calendar.getTimeInMillis();  // this won't work for JDK 1.3
294:                return calendar.getTime().getTime();
295:            }
296:
297:            /**
298:             * Returns the last millisecond of the second.
299:             *
300:             * @param calendar  the calendar/timezone (<code>null</code> not permitted).
301:             *
302:             * @return The last millisecond.
303:             *
304:             * @throws NullPointerException if <code>calendar</code> is 
305:             *     <code>null</code>.
306:             */
307:            public long getLastMillisecond(Calendar calendar) {
308:                return getFirstMillisecond(calendar) + 999L;
309:            }
310:
311:            /**
312:             * Tests the equality of this object against an arbitrary Object.
313:             * <P>
314:             * This method will return true ONLY if the object is a Second object
315:             * representing the same second as this instance.
316:             *
317:             * @param obj  the object to compare (<code>null</code> permitted).
318:             *
319:             * @return <code>true</code> if second and minute of this and the object 
320:             *         are the same.
321:             */
322:            public boolean equals(Object obj) {
323:                if (obj == this ) {
324:                    return true;
325:                }
326:                if (!(obj instanceof  Second)) {
327:                    return false;
328:                }
329:                Second that = (Second) obj;
330:                if (this .second != that.second) {
331:                    return false;
332:                }
333:                if (this .minute != that.minute) {
334:                    return false;
335:                }
336:                if (this .hour != that.hour) {
337:                    return false;
338:                }
339:                if (!this .day.equals(that.day)) {
340:                    return false;
341:                }
342:                return true;
343:            }
344:
345:            /**
346:             * Returns a hash code for this object instance.  The approach described by
347:             * Joshua Bloch in "Effective Java" has been used here:
348:             * <p>
349:             * <code>http://developer.java.sun.com/developer/Books/effectivejava
350:             * /Chapter3.pdf</code>
351:             * 
352:             * @return A hash code.
353:             */
354:            public int hashCode() {
355:                int result = 17;
356:                result = 37 * result + this .second;
357:                result = 37 * result + this .minute;
358:                result = 37 * result + this .hour;
359:                result = 37 * result + this .day.hashCode();
360:                return result;
361:            }
362:
363:            /**
364:             * Returns an integer indicating the order of this Second object relative
365:             * to the specified
366:             * object: negative == before, zero == same, positive == after.
367:             *
368:             * @param o1  the object to compare.
369:             *
370:             * @return negative == before, zero == same, positive == after.
371:             */
372:            public int compareTo(Object o1) {
373:
374:                int result;
375:
376:                // CASE 1 : Comparing to another Second object
377:                // -------------------------------------------
378:                if (o1 instanceof  Second) {
379:                    Second s = (Second) o1;
380:                    if (this .firstMillisecond < s.firstMillisecond) {
381:                        return -1;
382:                    } else if (this .firstMillisecond > s.firstMillisecond) {
383:                        return 1;
384:                    } else {
385:                        return 0;
386:                    }
387:                }
388:
389:                // CASE 2 : Comparing to another TimePeriod object
390:                // -----------------------------------------------
391:                else if (o1 instanceof  RegularTimePeriod) {
392:                    // more difficult case - evaluate later...
393:                    result = 0;
394:                }
395:
396:                // CASE 3 : Comparing to a non-TimePeriod object
397:                // ---------------------------------------------
398:                else {
399:                    // consider time periods to be ordered after general objects
400:                    result = 1;
401:                }
402:
403:                return result;
404:
405:            }
406:
407:            /**
408:             * Creates a new instance by parsing a string.  The string is assumed to
409:             * be in the format "YYYY-MM-DD HH:MM:SS", perhaps with leading or trailing
410:             * whitespace.
411:             *
412:             * @param s  the string to parse.
413:             *
414:             * @return The second, or <code>null</code> if the string is not parseable.
415:             */
416:            public static Second parseSecond(String s) {
417:
418:                Second result = null;
419:                s = s.trim();
420:
421:                String daystr = s.substring(0, Math.min(10, s.length()));
422:                Day day = Day.parseDay(daystr);
423:                if (day != null) {
424:                    String hmsstr = s.substring(Math.min(daystr.length() + 1, s
425:                            .length()), s.length());
426:                    hmsstr = hmsstr.trim();
427:
428:                    int l = hmsstr.length();
429:                    String hourstr = hmsstr.substring(0, Math.min(2, l));
430:                    String minstr = hmsstr.substring(Math.min(3, l), Math.min(
431:                            5, l));
432:                    String secstr = hmsstr.substring(Math.min(6, l), Math.min(
433:                            8, l));
434:                    int hour = Integer.parseInt(hourstr);
435:
436:                    if ((hour >= 0) && (hour <= 23)) {
437:
438:                        int minute = Integer.parseInt(minstr);
439:                        if ((minute >= 0) && (minute <= 59)) {
440:
441:                            Minute m = new Minute(minute, new Hour(hour, day));
442:                            int second = Integer.parseInt(secstr);
443:                            if ((second >= 0) && (second <= 59)) {
444:                                result = new Second(second, m);
445:                            }
446:                        }
447:                    }
448:                }
449:
450:                return result;
451:
452:            }
453:
454:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.