Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  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 org.joda.time.chrono;
 
 
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(s):
Brian S O'Neill
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.

Parameters:
base base chronology to wrap
lowerLimit inclusive lower limit, or null if none
upperLimit exclusive upper limit, or null if none
Throws:
java.lang.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.

Parameters:
lowerLimit inclusive lower limit, or null if none
upperLimit exclusive upper limit, or null if none
 
     private LimitChronology(Chronology base,
                             DateTime lowerLimitDateTime upperLimit) {
         super(basenull);
        // These can be set after assembly.
         = lowerLimit;
         = upperLimit;
    }

    
Returns the inclusive lower limit instant.

Returns:
lower limit
    public DateTime getLowerLimit() {
        return ;
    }

    
Returns the inclusive upper limit instant.

Returns:
upper limit
    public DateTime getUpperLimit() {
        return ;
    }

    
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(.);
    }

    
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 == . &&  != null) {
            return ;
        }
        DateTime lowerLimit = ;
        if (lowerLimit != null) {
            MutableDateTime mdt = lowerLimit.toMutableDateTime();
            mdt.setZoneRetainFields(zone);
            lowerLimit = mdt.toDateTime();
        }
        DateTime upperLimit = ;
        if (upperLimit != null) {
            MutableDateTime mdt = upperLimit.toMutableDateTime();
            mdt.setZoneRetainFields(zone);
            upperLimit = mdt.toDateTime();
        }
        
        LimitChronology chrono = getInstance
            (getBase().withZone(zone), lowerLimitupperLimit);
        if (zone == .) {
             = chrono;
        }
        return chrono;
    }
    public long getDateTimeMillis(int yearint monthOfYearint dayOfMonth,
                                  int millisOfDay)
        throws IllegalArgumentException
    {
        long instant = getBase().getDateTimeMillis(yearmonthOfYeardayOfMonthmillisOfDay);
        checkLimits(instant"resulting");
        return instant;
    }
    public long getDateTimeMillis(int yearint monthOfYearint dayOfMonth,
                                  int hourOfDayint minuteOfHour,
                                  int secondOfMinuteint millisOfSecond)
        throws IllegalArgumentException
    {
        long instant = getBase().getDateTimeMillis
            (yearmonthOfYeardayOfMonth,
             hourOfDayminuteOfHoursecondOfMinutemillisOfSecond);
        checkLimits(instant"resulting");
        return instant;
    }
    public long getDateTimeMillis(long instant,
                                  int hourOfDayint minuteOfHour,
                                  int secondOfMinuteint millisOfSecond)
        throws IllegalArgumentException
    {
        checkLimits(instantnull);
        instant = getBase().getDateTimeMillis
            (instanthourOfDayminuteOfHoursecondOfMinutemillisOfSecond);
        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<ObjectObjectconverted = new HashMap<ObjectObject>();
        // Convert duration fields...
        fields.eras = convertField(fields.erasconverted);
        fields.centuries = convertField(fields.centuriesconverted);
        fields.years = convertField(fields.yearsconverted);
        fields.months = convertField(fields.monthsconverted);
        fields.weekyears = convertField(fields.weekyearsconverted);
        fields.weeks = convertField(fields.weeksconverted);
        fields.days = convertField(fields.daysconverted);
        fields.halfdays = convertField(fields.halfdaysconverted);
        fields.hours = convertField(fields.hoursconverted);
        fields.minutes = convertField(fields.minutesconverted);
        fields.seconds = convertField(fields.secondsconverted);
        fields.millis = convertField(fields.millisconverted);
        // Convert datetime fields...
        fields.year = convertField(fields.yearconverted);
        fields.yearOfEra = convertField(fields.yearOfEraconverted);
        fields.yearOfCentury = convertField(fields.yearOfCenturyconverted);
        fields.centuryOfEra = convertField(fields.centuryOfEraconverted);
        fields.era = convertField(fields.eraconverted);
        fields.dayOfWeek = convertField(fields.dayOfWeekconverted);
        fields.dayOfMonth = convertField(fields.dayOfMonthconverted);
        fields.dayOfYear = convertField(fields.dayOfYearconverted);
        fields.monthOfYear = convertField(fields.monthOfYearconverted);
        fields.weekOfWeekyear = convertField(fields.weekOfWeekyearconverted);
        fields.weekyear = convertField(fields.weekyearconverted);
        fields.weekyearOfCentury = convertField(fields.weekyearOfCenturyconverted);
        fields.millisOfSecond = convertField(fields.millisOfSecondconverted);
        fields.millisOfDay = convertField(fields.millisOfDayconverted);
        fields.secondOfMinute = convertField(fields.secondOfMinuteconverted);
        fields.secondOfDay = convertField(fields.secondOfDayconverted);
        fields.minuteOfHour = convertField(fields.minuteOfHourconverted);
        fields.minuteOfDay = convertField(fields.minuteOfDayconverted);
        fields.hourOfDay = convertField(fields.hourOfDayconverted);
        fields.hourOfHalfday = convertField(fields.hourOfHalfdayconverted);
        fields.clockhourOfDay = convertField(fields.clockhourOfDayconverted);
        fields.clockhourOfHalfday = convertField(fields.clockhourOfHalfdayconverted);
        fields.halfdayOfDay = convertField(fields.halfdayOfDayconverted);
    }
    private DurationField convertField(DurationField fieldHashMap<ObjectObjectconverted) {
        if (field == null || !field.isSupported()) {
            return field;
        }
        if (converted.containsKey(field)) {
            return (DurationField)converted.get(field);
        }
        LimitDurationField limitField = new LimitDurationField(field);
        converted.put(fieldlimitField);
        return limitField;
    }
    private DateTimeField convertField(DateTimeField fieldHashMap<ObjectObjectconverted) {
        if (field == null || !field.isSupported()) {
            return field;
        }
        if (converted.containsKey(field)) {
            return (DateTimeField)converted.get(field);
        }
        LimitDateTimeField limitField =
            new LimitDateTimeField(field,
                                   convertField(field.getDurationField(), converted),
                                   convertField(field.getRangeDurationField(), converted),
                                   convertField(field.getLeapDurationField(), converted));
        converted.put(fieldlimitField);
        return limitField;
    }
    void checkLimits(long instantString desc) {
        DateTime limit;
        if ((limit = ) != null && instant < limit.getMillis()) {
            throw new LimitException(desctrue);
        }
        if ((limit = ) != null && instant >= limit.getMillis()) {
            throw new LimitException(descfalse);
        }
    }
    //-----------------------------------------------------------------------
    
A limit chronology is only equal to a limit chronology with the same base chronology and limits.

Parameters:
obj the object to compare to
Returns:
true if equal
Since:
1.4
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof LimitChronology == false) {
            return false;
        }
        LimitChronology chrono = (LimitChronologyobj;
        return
            getBase().equals(chrono.getBase()) &&
            FieldUtils.equals(getLowerLimit(), chrono.getLowerLimit()) &&
            FieldUtils.equals(getUpperLimit(), chrono.getUpperLimit());
    }

    
A suitable hashcode for the chronology.

Returns:
the hashcode
Since:
1.4
    public int hashCode() {
        int hash = 317351877;
        hash += (getLowerLimit() != null ? getLowerLimit().hashCode() : 0);
        hash += (getUpperLimit() != null ? getUpperLimit().hashCode() : 0);
        hash += getBase().hashCode() * 7;
        return hash;
    }

    
A debugging string for the chronology.

Returns:
the debugging string
    public String toString() {
        return "LimitChronology[" + getBase().toString() + ", " +
            (getLowerLimit() == null ? "NoLimit" : getLowerLimit().toString()) + ", " +
            (getUpperLimit() == null ? "NoLimit" : getUpperLimit().toString()) + ']';
    }
    //-----------------------------------------------------------------------
    
Extends IllegalArgumentException such that the exception message is not generated unless it is actually requested.
    private class LimitException extends IllegalArgumentException {
        private static final long serialVersionUID = -5924689995607498581L;
        private final boolean iIsLow;
        LimitException(String descboolean isLow) {
            super(desc);
             = isLow;
        }
        public String getMessage() {
            StringBuffer buf = new StringBuffer(85);
            buf.append("The");
            String desc = super.getMessage();
            if (desc != null) {
                buf.append(' ');
                buf.append(desc);
            }
            buf.append(" instant is ");
            DateTimeFormatter p = ISODateTimeFormat.dateTime();
            p = p.withChronology(getBase());
            if () {
                buf.append("below the supported minimum of ");
                p.printTo(bufgetLowerLimit().getMillis());
            } else {
                buf.append("above the supported maximum of ");
                p.printTo(bufgetUpperLimit().getMillis());
            }
            
            buf.append(" (");
            buf.append(getBase());
            buf.append(')');
            return buf.toString();
        }
        public String toString() {
            return "IllegalArgumentException: " + getMessage();
        }
    }
    private class LimitDurationField extends DecoratedDurationField {
        private static final long serialVersionUID = 8049297699408782284L;
        LimitDurationField(DurationField field) {
            super(fieldfield.getType());
        }
        public int getValue(long durationlong instant) {
            checkLimits(instantnull);
            return getWrappedField().getValue(durationinstant);
        }
        public long getValueAsLong(long durationlong instant) {
            checkLimits(instantnull);
            return getWrappedField().getValueAsLong(durationinstant);
        }
        public long getMillis(int valuelong instant) {
            checkLimits(instantnull);
            return getWrappedField().getMillis(valueinstant);
        }
        public long getMillis(long valuelong instant) {
            checkLimits(instantnull);
            return getWrappedField().getMillis(valueinstant);
        }
        public long add(long instantint amount) {
            checkLimits(instantnull);
            long result = getWrappedField().add(instantamount);
            checkLimits(result"resulting");
            return result;
        }
        public long add(long instantlong amount) {
            checkLimits(instantnull);
            long result = getWrappedField().add(instantamount);
            checkLimits(result"resulting");
            return result;
        }
        public int getDifference(long minuendInstantlong subtrahendInstant) {
            checkLimits(minuendInstant"minuend");
            checkLimits(subtrahendInstant"subtrahend");
            return getWrappedField().getDifference(minuendInstantsubtrahendInstant);
        }
        public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
            checkLimits(minuendInstant"minuend");
            checkLimits(subtrahendInstant"subtrahend");
            return getWrappedField().getDifferenceAsLong(minuendInstantsubtrahendInstant);
        }
    }
    private class LimitDateTimeField extends DecoratedDateTimeField {
        @SuppressWarnings("unused")
        private static final long serialVersionUID = -2435306746995699312L;
        private final DurationField iDurationField;
        private final DurationField iRangeDurationField;
        private final DurationField iLeapDurationField;
        LimitDateTimeField(DateTimeField field,
                           DurationField durationField,
                           DurationField rangeDurationField,
                           DurationField leapDurationField) {
            super(fieldfield.getType());
             = durationField;
             = rangeDurationField;
             = leapDurationField;
        }
        public int get(long instant) {
            checkLimits(instantnull);
            return getWrappedField().get(instant);
        }
        
        public String getAsText(long instantLocale locale) {
            checkLimits(instantnull);
            return getWrappedField().getAsText(instantlocale);
        }
        
        public String getAsShortText(long instantLocale locale) {
            checkLimits(instantnull);
            return getWrappedField().getAsShortText(instantlocale);
        }
        
        public long add(long instantint amount) {
            checkLimits(instantnull);
            long result = getWrappedField().add(instantamount);
            checkLimits(result"resulting");
            return result;
        }
        public long add(long instantlong amount) {
            checkLimits(instantnull);
            long result = getWrappedField().add(instantamount);
            checkLimits(result"resulting");
            return result;
        }
        public long addWrapField(long instantint amount) {
            checkLimits(instantnull);
            long result = getWrappedField().addWrapField(instantamount);
            checkLimits(result"resulting");
            return result;
        }
        
        public int getDifference(long minuendInstantlong subtrahendInstant) {
            checkLimits(minuendInstant"minuend");
            checkLimits(subtrahendInstant"subtrahend");
            return getWrappedField().getDifference(minuendInstantsubtrahendInstant);
        }
        
        public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
            checkLimits(minuendInstant"minuend");
            checkLimits(subtrahendInstant"subtrahend");
            return getWrappedField().getDifferenceAsLong(minuendInstantsubtrahendInstant);
        }
        
        public long set(long instantint value) {
            checkLimits(instantnull);
            long result = getWrappedField().set(instantvalue);
            checkLimits(result"resulting");
            return result;
        }
        
        public long set(long instantString textLocale locale) {
            checkLimits(instantnull);
            long result = getWrappedField().set(instanttextlocale);
            checkLimits(result"resulting");
            return result;
        }
        
        public final DurationField getDurationField() {
            return ;
        }
        public final DurationField getRangeDurationField() {
            return ;
        }
        public boolean isLeap(long instant) {
            checkLimits(instantnull);
            return getWrappedField().isLeap(instant);
        }
        
        public int getLeapAmount(long instant) {
            checkLimits(instantnull);
            return getWrappedField().getLeapAmount(instant);
        }
        
        public final DurationField getLeapDurationField() {
            return ;
        }
        
        public long roundFloor(long instant) {
            checkLimits(instantnull);
            long result = getWrappedField().roundFloor(instant);
            checkLimits(result"resulting");
            return result;
        }
        
        public long roundCeiling(long instant) {
            checkLimits(instantnull);
            long result = getWrappedField().roundCeiling(instant);
            checkLimits(result"resulting");
            return result;
        }
        
        public long roundHalfFloor(long instant) {
            checkLimits(instantnull);
            long result = getWrappedField().roundHalfFloor(instant);
            checkLimits(result"resulting");
            return result;
        }
        
        public long roundHalfCeiling(long instant) {
            checkLimits(instantnull);
            long result = getWrappedField().roundHalfCeiling(instant);
            checkLimits(result"resulting");
            return result;
        }
        
        public long roundHalfEven(long instant) {
            checkLimits(instantnull);
            long result = getWrappedField().roundHalfEven(instant);
            checkLimits(result"resulting");
            return result;
        }
        
        public long remainder(long instant) {
            checkLimits(instantnull);
            long result = getWrappedField().remainder(instant);
            checkLimits(result"resulting");
            return result;
        }
        public int getMinimumValue(long instant) {
            checkLimits(instantnull);
            return getWrappedField().getMinimumValue(instant);
        }
        public int getMaximumValue(long instant) {
            checkLimits(instantnull);
            return getWrappedField().getMaximumValue(instant);
        }
        public int getMaximumTextLength(Locale locale) {
            return getWrappedField().getMaximumTextLength(locale);
        }
        public int getMaximumShortTextLength(Locale locale) {
            return getWrappedField().getMaximumShortTextLength(locale);
        }
    }
New to GrepCode? Check out our FAQ X