Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2001-2009 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.time.chrono;
import java.util.HashMap;
import java.util.Locale;
import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeZone;
import org.joda.time.DurationField;
import org.joda.time.MutableDateTime;
import org.joda.time.ReadableDateTime;
import org.joda.time.field.DecoratedDateTimeField;
import org.joda.time.field.DecoratedDurationField;
import org.joda.time.field.FieldUtils;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* Wraps another Chronology to impose limits on the range of instants that
* the fields within a Chronology may support. The limits are applied to both
* DateTimeFields and DurationFields.
*
* Methods in DateTimeField and DurationField throw an IllegalArgumentException
* whenever given an input instant that is outside the limits or when an
* attempt is made to move an instant outside the limits.
*
* LimitChronology is thread-safe and immutable.
*
* @author Brian S O'Neill
* @author Stephen Colebourne
* @since 1.0
*/
public final class LimitChronology extends AssembledChronology {
/** Serialization lock */
private static final long serialVersionUID = 7670866536893052522L;
/**
* Wraps another chronology, with datetime limits. When withUTC or
* withZone is called, the returned LimitChronology instance has
* the same limits, except they are time zone adjusted.
*
* @param base base chronology to wrap
* @param lowerLimit inclusive lower limit, or null if none
* @param upperLimit exclusive upper limit, or null if none
* @throws IllegalArgumentException if chronology is null or limits are invalid
*/
public static LimitChronology getInstance(Chronology base,
ReadableDateTime lowerLimit,
ReadableDateTime upperLimit) {
if (base == null) {
throw new IllegalArgumentException("Must supply a chronology");
}
lowerLimit = lowerLimit == null ? null : lowerLimit.toDateTime();
upperLimit = upperLimit == null ? null : upperLimit.toDateTime();
if (lowerLimit != null && upperLimit != null) {
if (!lowerLimit.isBefore(upperLimit)) {
throw new IllegalArgumentException
("The lower limit must be come before than the upper limit");
}
}
return new LimitChronology(base, (DateTime)lowerLimit, (DateTime)upperLimit);
}
final DateTime iLowerLimit;
final DateTime iUpperLimit;
private transient LimitChronology iWithUTC;
/**
* Wraps another chronology, with datetime limits. When withUTC or
* withZone is called, the returned LimitChronology instance has
* the same limits, except they are time zone adjusted.
*
* @param lowerLimit inclusive lower limit, or null if none
* @param upperLimit exclusive upper limit, or null if none
*/
private LimitChronology(Chronology base,
DateTime lowerLimit, DateTime upperLimit) {
super(base, null);
// These can be set after assembly.
iLowerLimit = lowerLimit;
iUpperLimit = upperLimit;
}
/**
* Returns the inclusive lower limit instant.
*
* @return lower limit
*/
public DateTime getLowerLimit() {
return iLowerLimit;
}
/**
* Returns the inclusive upper limit instant.
*
* @return upper limit
*/
public DateTime getUpperLimit() {
return iUpperLimit;
}
/**
* If this LimitChronology is already UTC, then this is
* returned. Otherwise, a new instance is returned, with the limits
* adjusted to the new time zone.
*/
public Chronology withUTC() {
return withZone(DateTimeZone.UTC);
}
/**
* If this LimitChronology has the same time zone as the one given, then
* this is returned. Otherwise, a new instance is returned, with the limits
* adjusted to the new time zone.
*/
public Chronology withZone(DateTimeZone zone) {
if (zone == null) {
zone = DateTimeZone.getDefault();
}
if (zone == getZone()) {
return this;
}
if (zone == DateTimeZone.UTC && iWithUTC != null) {
return iWithUTC;
}
DateTime lowerLimit = iLowerLimit;
if (lowerLimit != null) {
MutableDateTime mdt = lowerLimit.toMutableDateTime();
mdt.setZoneRetainFields(zone);
lowerLimit = mdt.toDateTime();
}
DateTime upperLimit = iUpperLimit;
if (upperLimit != null) {
MutableDateTime mdt = upperLimit.toMutableDateTime();
mdt.setZoneRetainFields(zone);
upperLimit = mdt.toDateTime();
}
LimitChronology chrono = getInstance
(getBase().withZone(zone), lowerLimit, upperLimit);
if (zone == DateTimeZone.UTC) {
iWithUTC = chrono;
}
return chrono;
}
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
int millisOfDay)
throws IllegalArgumentException
{
long instant = getBase().getDateTimeMillis(year, monthOfYear, dayOfMonth, millisOfDay);
checkLimits(instant, "resulting");
return instant;
}
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour,
int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException
{
long instant = getBase().getDateTimeMillis
(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
checkLimits(instant, "resulting");
return instant;
}
public long getDateTimeMillis(long instant,
int hourOfDay, int minuteOfHour,
int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException
{
checkLimits(instant, null);
instant = getBase().getDateTimeMillis
(instant, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
checkLimits(instant, "resulting");
return instant;
}
protected void assemble(Fields fields) {
// Keep a local cache of converted fields so as not to create redundant
// objects.
HashMap