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-2013 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 com.facebook.presto.jdbc.internal.joda.time.chrono;
import java.util.HashMap;
import java.util.Locale;
import com.facebook.presto.jdbc.internal.joda.time.Chronology;
import com.facebook.presto.jdbc.internal.joda.time.DateTime;
import com.facebook.presto.jdbc.internal.joda.time.DateTimeField;
import com.facebook.presto.jdbc.internal.joda.time.DateTimeZone;
import com.facebook.presto.jdbc.internal.joda.time.DurationField;
import com.facebook.presto.jdbc.internal.joda.time.MutableDateTime;
import com.facebook.presto.jdbc.internal.joda.time.ReadableDateTime;
import com.facebook.presto.jdbc.internal.joda.time.field.DecoratedDateTimeField;
import com.facebook.presto.jdbc.internal.joda.time.field.DecoratedDurationField;
import com.facebook.presto.jdbc.internal.joda.time.field.FieldUtils;
import com.facebook.presto.jdbc.internal.joda.time.format.DateTimeFormatter;
import com.facebook.presto.jdbc.internal.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
* @return the chronology, not null
* @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 && !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.
*/
@Override
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.
*/
@Override
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;
}
@Override
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;
}
@Override
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;
}
@Override
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;
}
@Override
protected void assemble(Fields fields) {
// Keep a local cache of converted fields so as not to create redundant
// objects.
HashMap