View Javadoc

1   /*
2    *  Copyright 2001-2009 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.convert;
17  
18  import org.joda.time.Chronology;
19  import org.joda.time.DateTimeUtils;
20  import org.joda.time.Period;
21  import org.joda.time.ReadWritableInterval;
22  import org.joda.time.ReadWritablePeriod;
23  
24  /**
25   * NullConverter converts null to an instant, partial, duration, period
26   * or interval. Null means now for instant/partial, zero for duration/period
27   * and from now to now for interval.
28   *
29   * @author Stephen Colebourne
30   * @author Brian S O'Neill
31   * @since 1.0
32   */
33  class NullConverter extends AbstractConverter
34          implements InstantConverter, PartialConverter, DurationConverter, PeriodConverter, IntervalConverter {
35  
36      /**
37       * Singleton instance.
38       */
39      static final NullConverter INSTANCE = new NullConverter();
40  
41      /**
42       * Restricted constructor.
43       */
44      protected NullConverter() {
45          super();
46      }
47  
48      //-----------------------------------------------------------------------
49      /**
50       * Gets the millisecond duration, which is zero.
51       * 
52       * @param object  the object to convert, which is null
53       * @return the millisecond duration
54       */
55      public long getDurationMillis(Object object) {
56          return 0L;
57      }
58  
59      //-----------------------------------------------------------------------
60      /**
61       * Sets the given ReadWritableDuration to zero milliseconds.
62       *
63       * @param duration duration to get modified
64       * @param object  the object to convert, which is null
65       * @param chrono  the chronology to use
66       * @throws NullPointerException if the duration is null
67       */
68      public void setInto(ReadWritablePeriod duration, Object object, Chronology chrono) {
69          duration.setPeriod((Period) null);
70      }
71  
72      //-----------------------------------------------------------------------
73      /**
74       * Extracts interval endpoint values from an object of this converter's
75       * type, and sets them into the given ReadWritableInterval.
76       *
77       * @param writableInterval interval to get modified, not null
78       * @param object  the object to convert, which is null
79       * @param chrono  the chronology to use, may be null
80       * @throws NullPointerException if the interval is null
81       */
82      public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
83          writableInterval.setChronology(chrono);
84          long now = DateTimeUtils.currentTimeMillis();
85          writableInterval.setInterval(now, now);
86      }
87  
88      //-----------------------------------------------------------------------
89      /**
90       * Returns null.
91       * 
92       * @return null
93       */
94      public Class<?> getSupportedType() {
95          return null;
96      }
97  
98  }