Source Code Cross Referenced for Minutes.java in  » Development » Joda-Time » org » joda » 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 » Development » Joda Time » org.joda.time 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2006 Stephen Colebourne
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.joda.time;
017:
018:        import org.joda.time.base.BaseSingleFieldPeriod;
019:        import org.joda.time.field.FieldUtils;
020:        import org.joda.time.format.ISOPeriodFormat;
021:        import org.joda.time.format.PeriodFormatter;
022:
023:        /**
024:         * An immutable time period representing a number of minutes.
025:         * <p>
026:         * <code>Minutes</code> is an immutable period that can only store minutes.
027:         * It does not store years, months or hours for example. As such it is a
028:         * type-safe way of representing a number of minutes in an application.
029:         * <p>
030:         * The number of minutes is set in the constructor, and may be queried using
031:         * <code>getMinutes()</code>. Basic mathematical operations are provided -
032:         * <code>plus()</code>, <code>minus()</code>, <code>multipliedBy()</code> and
033:         * <code>dividedBy()</code>.
034:         * <p>
035:         * <code>Minutes</code> is thread-safe and immutable.
036:         *
037:         * @author Stephen Colebourne
038:         * @since 1.4
039:         */
040:        public final class Minutes extends BaseSingleFieldPeriod {
041:
042:            /** Constant representing zero minutes. */
043:            public static final Minutes ZERO = new Minutes(0);
044:            /** Constant representing one minute. */
045:            public static final Minutes ONE = new Minutes(1);
046:            /** Constant representing two minutes. */
047:            public static final Minutes TWO = new Minutes(2);
048:            /** Constant representing three minutes. */
049:            public static final Minutes THREE = new Minutes(3);
050:            /** Constant representing the maximum number of minutes that can be stored in this object. */
051:            public static final Minutes MAX_VALUE = new Minutes(
052:                    Integer.MAX_VALUE);
053:            /** Constant representing the minimum number of minutes that can be stored in this object. */
054:            public static final Minutes MIN_VALUE = new Minutes(
055:                    Integer.MIN_VALUE);
056:
057:            /** The paser to use for this class. */
058:            private static final PeriodFormatter PARSER = ISOPeriodFormat
059:                    .standard().withParseType(PeriodType.minutes());
060:            /** Serialization version. */
061:            private static final long serialVersionUID = 87525275727380863L;
062:
063:            //-----------------------------------------------------------------------
064:            /**
065:             * Obtains an instance of <code>Minutes</code> that may be cached.
066:             * <code>Minutes</code> is immutable, so instances can be cached and shared.
067:             * This factory method provides access to shared instances.
068:             *
069:             * @param minutes  the number of minutes to obtain an instance for
070:             * @return the instance of Minutes
071:             */
072:            public static Minutes minutes(int minutes) {
073:                switch (minutes) {
074:                case 0:
075:                    return ZERO;
076:                case 1:
077:                    return ONE;
078:                case 2:
079:                    return TWO;
080:                case 3:
081:                    return THREE;
082:                case Integer.MAX_VALUE:
083:                    return MAX_VALUE;
084:                case Integer.MIN_VALUE:
085:                    return MIN_VALUE;
086:                default:
087:                    return new Minutes(minutes);
088:                }
089:            }
090:
091:            //-----------------------------------------------------------------------
092:            /**
093:             * Creates a <code>Minutes</code> representing the number of whole minutes
094:             * between the two specified datetimes.
095:             *
096:             * @param start  the start instant, must not be null
097:             * @param end  the end instant, must not be null
098:             * @return the period in minutes
099:             * @throws IllegalArgumentException if the instants are null or invalid
100:             */
101:            public static Minutes minutesBetween(ReadableInstant start,
102:                    ReadableInstant end) {
103:                int amount = BaseSingleFieldPeriod.between(start, end,
104:                        DurationFieldType.minutes());
105:                return Minutes.minutes(amount);
106:            }
107:
108:            /**
109:             * Creates a <code>Minutes</code> representing the number of whole minutes
110:             * between the two specified partial datetimes.
111:             * <p>
112:             * The two partials must contain the same fields, for example you can specify
113:             * two <code>LocalTime</code> objects.
114:             *
115:             * @param start  the start partial date, must not be null
116:             * @param end  the end partial date, must not be null
117:             * @return the period in minutes
118:             * @throws IllegalArgumentException if the partials are null or invalid
119:             */
120:            public static Minutes minutesBetween(ReadablePartial start,
121:                    ReadablePartial end) {
122:                if (start instanceof  LocalTime && end instanceof  LocalTime) {
123:                    Chronology chrono = DateTimeUtils.getChronology(start
124:                            .getChronology());
125:                    int minutes = chrono.minutes().getDifference(
126:                            ((LocalTime) end).getLocalMillis(),
127:                            ((LocalTime) start).getLocalMillis());
128:                    return Minutes.minutes(minutes);
129:                }
130:                int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
131:                return Minutes.minutes(amount);
132:            }
133:
134:            /**
135:             * Creates a <code>Minutes</code> representing the number of whole minutes
136:             * in the specified interval.
137:             *
138:             * @param interval  the interval to extract minutes from, null returns zero
139:             * @return the period in minutes
140:             * @throws IllegalArgumentException if the partials are null or invalid
141:             */
142:            public static Minutes minutesIn(ReadableInterval interval) {
143:                if (interval == null) {
144:                    return Minutes.ZERO;
145:                }
146:                int amount = BaseSingleFieldPeriod.between(interval.getStart(),
147:                        interval.getEnd(), DurationFieldType.minutes());
148:                return Minutes.minutes(amount);
149:            }
150:
151:            /**
152:             * Creates a new <code>Minutes</code> representing the number of complete
153:             * standard length minutes in the specified period.
154:             * <p>
155:             * This factory method converts all fields from the period to minutes using standardised
156:             * durations for each field. Only those fields which have a precise duration in
157:             * the ISO UTC chronology can be converted.
158:             * <ul>
159:             * <li>One week consists of 7 days.
160:             * <li>One day consists of 24 hours.
161:             * <li>One hour consists of 60 minutes.
162:             * <li>One minute consists of 60 seconds.
163:             * <li>One second consists of 1000 milliseconds.
164:             * </ul>
165:             * Months and Years are imprecise and periods containing these values cannot be converted.
166:             *
167:             * @param period  the period to get the number of minutes from, null returns zero
168:             * @return the period in minutes
169:             * @throws IllegalArgumentException if the period contains imprecise duration values
170:             */
171:            public static Minutes standardMinutesIn(ReadablePeriod period) {
172:                int amount = BaseSingleFieldPeriod.standardPeriodIn(period,
173:                        DateTimeConstants.MILLIS_PER_MINUTE);
174:                return Minutes.minutes(amount);
175:            }
176:
177:            /**
178:             * Creates a new <code>Minutes</code> by parsing a string in the ISO8601 format 'PTnM'.
179:             * <p>
180:             * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
181:             * minutes component may be non-zero. If any other component is non-zero, an exception
182:             * will be thrown.
183:             *
184:             * @param periodStr  the period string, null returns zero
185:             * @return the period in minutes
186:             * @throws IllegalArgumentException if the string format is invalid
187:             */
188:            public static Minutes parseMinutes(String periodStr) {
189:                if (periodStr == null) {
190:                    return Minutes.ZERO;
191:                }
192:                Period p = PARSER.parsePeriod(periodStr);
193:                return Minutes.minutes(p.getMinutes());
194:            }
195:
196:            //-----------------------------------------------------------------------
197:            /**
198:             * Creates a new instance representing a number of minutes.
199:             * You should consider using the factory method {@link #minutes(int)}
200:             * instead of the constructor.
201:             *
202:             * @param minutes  the number of minutes to represent
203:             */
204:            private Minutes(int minutes) {
205:                super (minutes);
206:            }
207:
208:            /**
209:             * Resolves singletons.
210:             * 
211:             * @return the singleton instance
212:             */
213:            private Object readResolve() {
214:                return Minutes.minutes(getValue());
215:            }
216:
217:            //-----------------------------------------------------------------------
218:            /**
219:             * Gets the duration field type, which is <code>minutes</code>.
220:             *
221:             * @return the period type
222:             */
223:            public DurationFieldType getFieldType() {
224:                return DurationFieldType.minutes();
225:            }
226:
227:            /**
228:             * Gets the period type, which is <code>minutes</code>.
229:             *
230:             * @return the period type
231:             */
232:            public PeriodType getPeriodType() {
233:                return PeriodType.minutes();
234:            }
235:
236:            //-----------------------------------------------------------------------
237:            /**
238:             * Converts this period in minutes to a period in weeks assuming a
239:             * 7 days week, 24 hour day and 60 minute hour.
240:             * <p>
241:             * This method allows you to convert between different types of period.
242:             * However to achieve this it makes the assumption that all weeks are
243:             * 7 days long, all days are 24 hours long and all hours are 60 minutes long.
244:             * This is not true when daylight savings is considered and may also not
245:             * be true for some unusual chronologies. However, it is included
246:             * as it is a useful operation for many applications and business rules.
247:             * 
248:             * @return a period representing the number of whole weeks for this number of minutes
249:             */
250:            public Weeks toStandardWeeks() {
251:                return Weeks.weeks(getValue()
252:                        / DateTimeConstants.MINUTES_PER_WEEK);
253:            }
254:
255:            /**
256:             * Converts this period in minutes to a period in days assuming a
257:             * 24 hour day and 60 minute hour.
258:             * <p>
259:             * This method allows you to convert between different types of period.
260:             * However to achieve this it makes the assumption that all days are
261:             * 24 hours long and all hours are 60 minutes long.
262:             * This is not true when daylight savings is considered and may also not
263:             * be true for some unusual chronologies. However, it is included
264:             * as it is a useful operation for many applications and business rules.
265:             * 
266:             * @return a period representing the number of whole days for this number of minutes
267:             */
268:            public Days toStandardDays() {
269:                return Days
270:                        .days(getValue() / DateTimeConstants.MINUTES_PER_DAY);
271:            }
272:
273:            /**
274:             * Converts this period in minutes to a period in hours assuming a
275:             * 60 minute hour.
276:             * <p>
277:             * This method allows you to convert between different types of period.
278:             * However to achieve this it makes the assumption that all hours are
279:             * 60 minutes long.
280:             * This may not be true for some unusual chronologies. However, it is included
281:             * as it is a useful operation for many applications and business rules.
282:             * 
283:             * @return a period representing the number of hours for this number of minutes
284:             */
285:            public Hours toStandardHours() {
286:                return Hours.hours(getValue()
287:                        / DateTimeConstants.MINUTES_PER_HOUR);
288:            }
289:
290:            /**
291:             * Converts this period in minutes to a period in seconds assuming a
292:             * 60 second minute.
293:             * <p>
294:             * This method allows you to convert between different types of period.
295:             * However to achieve this it makes the assumption that all minutes are
296:             * 60 seconds long.
297:             * This may not be true for some unusual chronologies. However, it is included
298:             * as it is a useful operation for many applications and business rules.
299:             * 
300:             * @return a period representing the number of seconds for this number of minutes
301:             * @throws ArithmeticException if the number of seconds is too large to be represented
302:             */
303:            public Seconds toStandardSeconds() {
304:                return Seconds.seconds(FieldUtils.safeMultiply(getValue(),
305:                        DateTimeConstants.SECONDS_PER_MINUTE));
306:            }
307:
308:            //-----------------------------------------------------------------------
309:            /**
310:             * Converts this period in minutes to a duration in milliseconds assuming a
311:             * 60 second minute.
312:             * <p>
313:             * This method allows you to convert from a period to a duration.
314:             * However to achieve this it makes the assumption that all minutes are
315:             * 60 seconds long. This might not be true for an unusual chronology,
316:             * for example one that takes leap seconds into account.
317:             * However, the method is included as it is a useful operation for many
318:             * applications and business rules.
319:             *
320:             * @return a duration equivalent to this number of minutes
321:             */
322:            public Duration toStandardDuration() {
323:                long minutes = getValue(); // assign to a long
324:                return new Duration(minutes
325:                        * DateTimeConstants.MILLIS_PER_MINUTE);
326:            }
327:
328:            //-----------------------------------------------------------------------
329:            /**
330:             * Gets the number of minutes that this period represents.
331:             *
332:             * @return the number of minutes in the period
333:             */
334:            public int getMinutes() {
335:                return getValue();
336:            }
337:
338:            //-----------------------------------------------------------------------
339:            /**
340:             * Returns a new instance with the specified number of minutes added.
341:             * <p>
342:             * This instance is immutable and unaffected by this method call.
343:             *
344:             * @param minutes  the amount of minutes to add, may be negative
345:             * @return the new period plus the specified number of minutes
346:             * @throws ArithmeticException if the result overflows an int
347:             */
348:            public Minutes plus(int minutes) {
349:                if (minutes == 0) {
350:                    return this ;
351:                }
352:                return Minutes.minutes(FieldUtils.safeAdd(getValue(), minutes));
353:            }
354:
355:            /**
356:             * Returns a new instance with the specified number of minutes added.
357:             * <p>
358:             * This instance is immutable and unaffected by this method call.
359:             *
360:             * @param minutes  the amount of minutes to add, may be negative, null means zero
361:             * @return the new period plus the specified number of minutes
362:             * @throws ArithmeticException if the result overflows an int
363:             */
364:            public Minutes plus(Minutes minutes) {
365:                if (minutes == null) {
366:                    return this ;
367:                }
368:                return plus(minutes.getValue());
369:            }
370:
371:            //-----------------------------------------------------------------------
372:            /**
373:             * Returns a new instance with the specified number of minutes taken away.
374:             * <p>
375:             * This instance is immutable and unaffected by this method call.
376:             *
377:             * @param minutes  the amount of minutes to take away, may be negative
378:             * @return the new period minus the specified number of minutes
379:             * @throws ArithmeticException if the result overflows an int
380:             */
381:            public Minutes minus(int minutes) {
382:                return plus(FieldUtils.safeNegate(minutes));
383:            }
384:
385:            /**
386:             * Returns a new instance with the specified number of minutes taken away.
387:             * <p>
388:             * This instance is immutable and unaffected by this method call.
389:             *
390:             * @param minutes  the amount of minutes to take away, may be negative, null means zero
391:             * @return the new period minus the specified number of minutes
392:             * @throws ArithmeticException if the result overflows an int
393:             */
394:            public Minutes minus(Minutes minutes) {
395:                if (minutes == null) {
396:                    return this ;
397:                }
398:                return minus(minutes.getValue());
399:            }
400:
401:            //-----------------------------------------------------------------------
402:            /**
403:             * Returns a new instance with the minutes multiplied by the specified scalar.
404:             * <p>
405:             * This instance is immutable and unaffected by this method call.
406:             *
407:             * @param scalar  the amount to multiply by, may be negative
408:             * @return the new period multiplied by the specified scalar
409:             * @throws ArithmeticException if the result overflows an int
410:             */
411:            public Minutes multipliedBy(int scalar) {
412:                return Minutes.minutes(FieldUtils.safeMultiply(getValue(),
413:                        scalar));
414:            }
415:
416:            /**
417:             * Returns a new instance with the minutes divided by the specified divisor.
418:             * The calculation uses integer division, thus 3 divided by 2 is 1.
419:             * <p>
420:             * This instance is immutable and unaffected by this method call.
421:             *
422:             * @param divisor  the amount to divide by, may be negative
423:             * @return the new period divided by the specified divisor
424:             * @throws ArithmeticException if the divisor is zero
425:             */
426:            public Minutes dividedBy(int divisor) {
427:                if (divisor == 1) {
428:                    return this ;
429:                }
430:                return Minutes.minutes(getValue() / divisor);
431:            }
432:
433:            //-----------------------------------------------------------------------
434:            /**
435:             * Returns a new instance with the minutes value negated.
436:             *
437:             * @return the new period with a negated value
438:             * @throws ArithmeticException if the result overflows an int
439:             */
440:            public Minutes negated() {
441:                return Minutes.minutes(FieldUtils.safeNegate(getValue()));
442:            }
443:
444:            //-----------------------------------------------------------------------
445:            /**
446:             * Is this minutes instance greater than the specified number of minutes.
447:             *
448:             * @param other  the other period, null means zero
449:             * @return true if this minutes instance is greater than the specified one
450:             */
451:            public boolean isGreaterThan(Minutes other) {
452:                if (other == null) {
453:                    return getValue() > 0;
454:                }
455:                return getValue() > other.getValue();
456:            }
457:
458:            /**
459:             * Is this minutes instance less than the specified number of minutes.
460:             *
461:             * @param other  the other period, null means zero
462:             * @return true if this minutes instance is less than the specified one
463:             */
464:            public boolean isLessThan(Minutes other) {
465:                if (other == null) {
466:                    return getValue() < 0;
467:                }
468:                return getValue() < other.getValue();
469:            }
470:
471:            //-----------------------------------------------------------------------
472:            /**
473:             * Gets this instance as a String in the ISO8601 duration format.
474:             * <p>
475:             * For example, "PT4M" represents 4 minutes.
476:             *
477:             * @return the value as an ISO8601 string
478:             */
479:            public String toString() {
480:                return "PT" + String.valueOf(getValue()) + "M";
481:            }
482:
483:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.