001 /* 002 * Copyright 2001-2011 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 /** 019 * Defines a partial time that does not support every datetime field, and is 020 * thus a local time. 021 * <p> 022 * A {@code ReadablePartial} supports a subset of those fields on the chronology. 023 * It cannot be compared to a {@code ReadableInstant}, as it does not fully 024 * specify an instant in time. The time it does specify is a local time, and does 025 * not include a time zone. 026 * <p> 027 * A {@code ReadablePartial} can be converted to a {@code ReadableInstant} 028 * using the {@code toDateTime} method. This works by providing a full base 029 * instant that can be used to 'fill in the gaps' and specify a time zone. 030 * <p> 031 * {@code ReadablePartial} is {@code Comparable} from v2.0. 032 * The comparison is based on the fields, compared in order, from largest to smallest. 033 * The first field that is non-equal is used to determine the result. 034 * 035 * @author Stephen Colebourne 036 * @since 1.0 037 */ 038 public interface ReadablePartial extends Comparable<ReadablePartial> { 039 040 /** 041 * Gets the number of fields that this partial supports. 042 * 043 * @return the number of fields supported 044 */ 045 int size(); 046 047 /** 048 * Gets the field type at the specified index. 049 * 050 * @param index the index to retrieve 051 * @return the field at the specified index 052 * @throws IndexOutOfBoundsException if the index is invalid 053 */ 054 DateTimeFieldType getFieldType(int index); 055 056 /** 057 * Gets the field at the specified index. 058 * 059 * @param index the index to retrieve 060 * @return the field at the specified index 061 * @throws IndexOutOfBoundsException if the index is invalid 062 */ 063 DateTimeField getField(int index); 064 065 /** 066 * Gets the value at the specified index. 067 * 068 * @param index the index to retrieve 069 * @return the value of the field at the specified index 070 * @throws IndexOutOfBoundsException if the index is invalid 071 */ 072 int getValue(int index); 073 074 /** 075 * Gets the chronology of the partial which is never null. 076 * <p> 077 * The {@link Chronology} is the calculation engine behind the partial and 078 * provides conversion and validation of the fields in a particular calendar system. 079 * 080 * @return the chronology, never null 081 */ 082 Chronology getChronology(); 083 084 /** 085 * Gets the value of one of the fields. 086 * <p> 087 * The field type specified must be one of those that is supported by the partial. 088 * 089 * @param field a DateTimeFieldType instance that is supported by this partial 090 * @return the value of that field 091 * @throws IllegalArgumentException if the field is null or not supported 092 */ 093 int get(DateTimeFieldType field); 094 095 /** 096 * Checks whether the field type specified is supported by this partial. 097 * 098 * @param field the field to check, may be null which returns false 099 * @return true if the field is supported 100 */ 101 boolean isSupported(DateTimeFieldType field); 102 103 /** 104 * Converts this partial to a full datetime by resolving it against another 105 * datetime. 106 * <p> 107 * This method takes the specified datetime and sets the fields from this 108 * instant on top. The chronology from the base instant is used. 109 * <p> 110 * For example, if this partial represents a time, then the result of this 111 * method will be the datetime from the specified base instant plus the 112 * time from this partial. 113 * 114 * @param baseInstant the instant that provides the missing fields, null means now 115 * @return the combined datetime 116 */ 117 DateTime toDateTime(ReadableInstant baseInstant); 118 119 //----------------------------------------------------------------------- 120 /** 121 * Compares this partial with the specified object for equality based 122 * on the supported fields, chronology and values. 123 * <p> 124 * Two instances of ReadablePartial are equal if they have the same 125 * chronology, same field types (in same order) and same values. 126 * 127 * @param partial the object to compare to 128 * @return true if equal 129 */ 130 boolean equals(Object partial); 131 132 /** 133 * Gets a hash code for the partial that is compatible with the 134 * equals method. 135 * <p> 136 * The formula used must be: 137 * <pre> 138 * int total = 157; 139 * for (int i = 0; i < fields.length; i++) { 140 * total = 23 * total + values[i]; 141 * total = 23 * total + fieldTypes[i].hashCode(); 142 * } 143 * total += chronology.hashCode(); 144 * return total; 145 * </pre> 146 * 147 * @return a suitable hash code 148 */ 149 int hashCode(); 150 151 //----------------------------------------------------------------------- 152 // This is commented out to improve backwards compatibility 153 // /** 154 // * Compares this partial with another returning an integer 155 // * indicating the order. 156 // * <p> 157 // * The fields are compared in order, from largest to smallest. 158 // * The first field that is non-equal is used to determine the result. 159 // * Thus a year-hour partial will first be compared on the year, and then 160 // * on the hour. 161 // * <p> 162 // * The specified object must be a partial instance whose field types 163 // * match those of this partial. If the partial instance has different 164 // * fields then a {@code ClassCastException} is thrown. 165 // * 166 // * @param partial an object to check against 167 // * @return negative if this is less, zero if equal, positive if greater 168 // * @throws ClassCastException if the partial is the wrong class 169 // * or if it has field types that don't match 170 // * @throws NullPointerException if the partial is null 171 // * @since 2.0, previously on {@code AbstractPartial} 172 // */ 173 // int compareTo(ReadablePartial partial); 174 175 //----------------------------------------------------------------------- 176 /** 177 * Get the value as a String in a recognisable ISO8601 format, only 178 * displaying supported fields. 179 * <p> 180 * The string output is in ISO8601 format to enable the String 181 * constructor to correctly parse it. 182 * 183 * @return the value as an ISO8601 string 184 */ 185 String toString(); 186 187 }