001: /*
002: * Copyright 2001-2005 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.convert;
017:
018: import org.joda.time.Chronology;
019: import org.joda.time.DateTimeUtils;
020: import org.joda.time.ReadWritableInterval;
021: import org.joda.time.ReadWritablePeriod;
022: import org.joda.time.ReadableInterval;
023:
024: /**
025: * Converts intervals into durations of any requested period type.
026: *
027: * @author Brian S O'Neill
028: * @since 1.0
029: */
030: class ReadableIntervalConverter extends AbstractConverter implements
031: IntervalConverter, DurationConverter, PeriodConverter {
032:
033: /**
034: * Singleton instance.
035: */
036: static final ReadableIntervalConverter INSTANCE = new ReadableIntervalConverter();
037:
038: /**
039: * Restricted constructor.
040: */
041: protected ReadableIntervalConverter() {
042: super ();
043: }
044:
045: //-----------------------------------------------------------------------
046: /**
047: * Gets the millisecond length of the interval.
048: *
049: * @param object the interval
050: */
051: public long getDurationMillis(Object object) {
052: return (((ReadableInterval) object)).toDurationMillis();
053: }
054:
055: //-----------------------------------------------------------------------
056: /**
057: * Sets the values of the mutable duration from the specified interval.
058: *
059: * @param writablePeriod the period to modify
060: * @param object the interval to set from
061: * @param chrono the chronology to use
062: */
063: public void setInto(ReadWritablePeriod writablePeriod,
064: Object object, Chronology chrono) {
065: ReadableInterval interval = (ReadableInterval) object;
066: chrono = (chrono != null ? chrono : DateTimeUtils
067: .getIntervalChronology(interval));
068: long start = interval.getStartMillis();
069: long end = interval.getEndMillis();
070: int[] values = chrono.get(writablePeriod, start, end);
071: for (int i = 0; i < values.length; i++) {
072: writablePeriod.setValue(i, values[i]);
073: }
074: }
075:
076: //-----------------------------------------------------------------------
077: /**
078: * Checks if the input is a ReadableInterval.
079: * <p>
080: * If it is, then the calling code should cast and copy the fields directly.
081: *
082: * @param object the object to convert, must not be null
083: * @param chrono the chronology to use, may be null
084: * @return true if the input is a ReadableInterval
085: * @throws ClassCastException if the object is invalid
086: */
087: public boolean isReadableInterval(Object object, Chronology chrono) {
088: return true;
089: }
090:
091: /**
092: * Extracts interval endpoint values from an object of this converter's
093: * type, and sets them into the given ReadWritableInterval.
094: *
095: * @param writableInterval interval to get modified, not null
096: * @param object the object to convert, must not be null
097: * @param chrono the chronology to use, may be null
098: * @throws ClassCastException if the object is invalid
099: */
100: public void setInto(ReadWritableInterval writableInterval,
101: Object object, Chronology chrono) {
102: ReadableInterval input = (ReadableInterval) object;
103: writableInterval.setInterval(input);
104: if (chrono != null) {
105: writableInterval.setChronology(chrono);
106: } else {
107: writableInterval.setChronology(input.getChronology());
108: }
109: }
110:
111: //-----------------------------------------------------------------------
112: /**
113: * Returns ReadableInterval.class.
114: */
115: public Class getSupportedType() {
116: return ReadableInterval.class;
117: }
118:
119: }
|