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.field;
017
018 import org.joda.time.DateTimeField;
019
020 /**
021 * Converts a lenient DateTimeField into a strict one. By being strict, the set
022 * throws an IllegalArgumentException if the value is out of bounds.
023 * <p>
024 * StrictDateTimeField is thread-safe and immutable.
025 *
026 * @author Brian S O'Neill
027 * @see org.joda.time.chrono.StrictChronology
028 * @see LenientDateTimeField
029 * @since 1.0
030 */
031 public class StrictDateTimeField extends DelegatedDateTimeField {
032
033 private static final long serialVersionUID = 3154803964207950910L;
034
035 /**
036 * Returns a strict version of the given field. If it is already strict,
037 * then it is returned as-is. Otherwise, a new StrictDateTimeField is
038 * returned.
039 */
040 public static DateTimeField getInstance(DateTimeField field) {
041 if (field == null) {
042 return null;
043 }
044 if (field instanceof LenientDateTimeField) {
045 field = ((LenientDateTimeField)field).getWrappedField();
046 }
047 if (!field.isLenient()) {
048 return field;
049 }
050 return new StrictDateTimeField(field);
051 }
052
053 protected StrictDateTimeField(DateTimeField field) {
054 super(field);
055 }
056
057 public final boolean isLenient() {
058 return false;
059 }
060
061 /**
062 * Does a bounds check before setting the value.
063 *
064 * @throws IllegalArgumentException if the value is invalid
065 */
066 public long set(long instant, int value) {
067 FieldUtils.verifyValueBounds
068 (this, value, getMinimumValue(instant), getMaximumValue(instant));
069 return super.set(instant, value);
070 }
071 }