Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 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.goda.time;
 
 
MutableInterval is the standard implementation of a mutable time interval.

A time interval represents a period of time between two instants. Intervals are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.

Intervals have a fixed millisecond duration. This is the difference between the start and end instants. The duration is represented separately by ReadableDuration. As a result, intervals are not comparable. To compare the length of two intervals, you should compare their durations.

An interval can also be converted to a ReadablePeriod. This represents the difference between the start and end points in terms of fields such as years and days.

If performing significant calculations on an interval, it may be faster to convert an Interval object to a MutableInterval one.

MutableInterval is mutable and not thread-safe, unless concurrent threads are not invoking mutator methods.

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public class MutableInterval
         extends BaseInterval
         implements ReadWritableIntervalCloneableSerializable {

    
Serialization version
 
     private static final long serialVersionUID = -5982824024992428470L;
 
     //-----------------------------------------------------------------------
     
Constructs a zero length time interval from 1970-01-01 to 1970-01-01.
 
     public MutableInterval() {
         super(0L, 0L, null);
     }

    
Constructs an interval from a start and end instant with the ISO default chronology.

Parameters:
startInstant start of this interval, as milliseconds from 1970-01-01T00:00:00Z.
endInstant end of this interval, as milliseconds from 1970-01-01T00:00:00Z.
Throws:
java.lang.IllegalArgumentException if the end is before the start
 
     public MutableInterval(long startInstantlong endInstant) {
         super(startInstantendInstantnull);
     }

    
Constructs an interval from a start and end instant with a chronology.

Parameters:
chronology the chronology to use, null is ISO default
startInstant start of this interval, as milliseconds from 1970-01-01T00:00:00Z.
endInstant end of this interval, as milliseconds from 1970-01-01T00:00:00Z.
Throws:
java.lang.IllegalArgumentException if the end is before the start
 
     public MutableInterval(long startInstantlong endInstantChronology chronology) {
         super(startInstantendInstantchronology);
     }

    
Constructs an interval from a start and end instant.

The chronology used is that of the start instant.

Parameters:
start start of this interval, null means now
end end of this interval, null means now
Throws:
java.lang.IllegalArgumentException if the end is before the start
 
     public MutableInterval(ReadableInstant startReadableInstant end) {
        super(startend);
    }

    
Constructs an interval from a start instant and a duration.

Parameters:
start start of this interval, null means now
duration the duration of this interval, null means zero length
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the end instant exceeds the capacity of a long
    public MutableInterval(ReadableInstant startReadableDuration duration) {
        super(startduration);
    }

    
Constructs an interval from a millisecond duration and an end instant.

Parameters:
duration the duration of this interval, null means zero length
end end of this interval, null means now
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the start instant exceeds the capacity of a long
    public MutableInterval(ReadableDuration durationReadableInstant end) {
        super(durationend);
    }

    
Constructs an interval from a start instant and a time period.

When forming the interval, the chronology from the instant is used if present, otherwise the chronology of the period is used.

Parameters:
start start of this interval, null means now
period the period of this interval, null means zero length
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the end instant exceeds the capacity of a long
    public MutableInterval(ReadableInstant startReadablePeriod period) {
        super(startperiod);
    }

    
Constructs an interval from a time period and an end instant.

When forming the interval, the chronology from the instant is used if present, otherwise the chronology of the period is used.

Parameters:
period the period of this interval, null means zero length
end end of this interval, null means now
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the start instant exceeds the capacity of a long
    public MutableInterval(ReadablePeriod periodReadableInstant end) {
        super(periodend);
    }

    
Constructs a time interval by converting or copying from another object.

The recognised object types are defined in ConverterManager and include ReadableInterval and String. The String formats are described by org.goda.time.format.ISODateTimeFormat.dateTimeParser() and org.goda.time.format.ISOPeriodFormat.standard(), and may be 'datetime/datetime', 'datetime/period' or 'period/datetime'.

Parameters:
interval the time interval to copy
Throws:
java.lang.IllegalArgumentException if the interval is invalid
    public MutableInterval(Object interval) {
        super(intervalnull);
    }

    
Constructs a time interval by converting or copying from another object, overriding the chronology.

The recognised object types are defined in ConverterManager and include ReadableInterval and String. The String formats are described by org.goda.time.format.ISODateTimeFormat.dateTimeParser() and org.goda.time.format.ISOPeriodFormat.standard(), and may be 'datetime/datetime', 'datetime/period' or 'period/datetime'.

Parameters:
interval the time interval to copy
chronology the chronology to use, null means ISO default
Throws:
java.lang.IllegalArgumentException if the interval is invalid
    public MutableInterval(Object intervalChronology chronology) {
        super(intervalchronology);
    }
    //-----------------------------------------------------------------------
    
Sets this interval from two millisecond instants retaining the chronology.

Parameters:
startInstant the start of the time interval
endInstant the start of the time interval
Throws:
java.lang.IllegalArgumentException if the end is before the start
    public void setInterval(long startInstantlong endInstant) {
        super.setInterval(startInstantendInstantgetChronology());
    }

    
Sets this interval to be the same as another.

Parameters:
interval the interval to copy
Throws:
java.lang.IllegalArgumentException if the interval is null
    public void setInterval(ReadableInterval interval) {
        if (interval == null) {
            throw new IllegalArgumentException("Interval must not be null");
        }
        long startMillis = interval.getStartMillis();
        long endMillis = interval.getEndMillis();
        Chronology chrono = interval.getChronology();
        super.setInterval(startMillisendMillischrono);
    }

    
Sets this interval from two instants, replacing the chronology with that from the start instant.

Parameters:
start the start of the time interval
end the start of the time interval
Throws:
java.lang.IllegalArgumentException if the end is before the start
    public void setInterval(ReadableInstant startReadableInstant end) {
        if (start == null && end == null) {
            long now = DateTimeUtils.currentTimeMillis();
            setInterval(nownow);
        } else {
            long startMillis = DateTimeUtils.getInstantMillis(start);
            long endMillis = DateTimeUtils.getInstantMillis(end);
            Chronology chrono = DateTimeUtils.getInstantChronology(start);
            super.setInterval(startMillisendMillischrono);
        }
    }
    //-----------------------------------------------------------------------
    
Sets the chronology of this time interval.

Parameters:
chrono the chronology to use, null means ISO default
    public void setChronology(Chronology chrono) {
        super.setInterval(getStartMillis(), getEndMillis(), chrono);
    }

    
Sets the start of this time interval.

Parameters:
startInstant the start of the time interval, millisecond instant from 1970-01-01T00:00:00Z
Throws:
java.lang.IllegalArgumentException if the end is before the start
    public void setStartMillis(long startInstant) {
        super.setInterval(startInstantgetEndMillis(), getChronology());
    }

    
Sets the start of this time interval as an Instant.

Parameters:
start the start of the time interval, null means now
Throws:
java.lang.IllegalArgumentException if the end is before the start
    public void setStart(ReadableInstant start) {
        long startMillis = DateTimeUtils.getInstantMillis(start);
        super.setInterval(startMillisgetEndMillis(), getChronology());
    }

    
Sets the end of this time interval.

Parameters:
endInstant the end of the time interval, millisecond instant from 1970-01-01T00:00:00Z
Throws:
java.lang.IllegalArgumentException if the end is before the start
    public void setEndMillis(long endInstant) {
        super.setInterval(getStartMillis(), endInstantgetChronology());
    }

    
Sets the end of this time interval as an Instant.

Parameters:
end the end of the time interval, null means now
Throws:
java.lang.IllegalArgumentException if the end is before the start
    public void setEnd(ReadableInstant end) {
        long endMillis = DateTimeUtils.getInstantMillis(end);
        super.setInterval(getStartMillis(), endMillisgetChronology());
    }
    //-----------------------------------------------------------------------
    
Sets the duration of this time interval, preserving the start instant.

Parameters:
duration new duration for interval
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the end instant exceeds the capacity of a long
    public void setDurationAfterStart(long duration) {
        setEndMillis(FieldUtils.safeAdd(getStartMillis(), duration));
    }

    
Sets the duration of this time interval, preserving the end instant.

Parameters:
duration new duration for interval
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the start instant exceeds the capacity of a long
    public void setDurationBeforeEnd(long duration) {
        setStartMillis(FieldUtils.safeAdd(getEndMillis(), -duration));
    }
    //-----------------------------------------------------------------------
    
Sets the duration of this time interval, preserving the start instant.

Parameters:
duration new duration for interval, null means zero length
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the end instant exceeds the capacity of a long
    public void setDurationAfterStart(ReadableDuration duration) {
        long durationMillis = DateTimeUtils.getDurationMillis(duration);
        setEndMillis(FieldUtils.safeAdd(getStartMillis(), durationMillis));
    }

    
Sets the duration of this time interval, preserving the end instant.

Parameters:
duration new duration for interval, null means zero length
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the start instant exceeds the capacity of a long
    public void setDurationBeforeEnd(ReadableDuration duration) {
        long durationMillis = DateTimeUtils.getDurationMillis(duration);
        setStartMillis(FieldUtils.safeAdd(getEndMillis(), -durationMillis));
    }
    //-----------------------------------------------------------------------
    
Sets the period of this time interval, preserving the start instant and using the ISOChronology in the default zone for calculations.

Parameters:
period new period for interval, null means zero length
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the end instant exceeds the capacity of a long
    public void setPeriodAfterStart(ReadablePeriod period) {
        if (period == null) {
            setEndMillis(getStartMillis());
        } else {
            setEndMillis(getChronology().add(periodgetStartMillis(), 1));
        }
    }

    
Sets the period of this time interval, preserving the end instant and using the ISOChronology in the default zone for calculations.

Parameters:
period new period for interval, null means zero length
Throws:
java.lang.IllegalArgumentException if the end is before the start
java.lang.ArithmeticException if the start instant exceeds the capacity of a long
    public void setPeriodBeforeEnd(ReadablePeriod period) {
        if (period == null) {
            setStartMillis(getEndMillis());
        } else {
            setStartMillis(getChronology().add(periodgetEndMillis(), -1));
        }
    }
    //-----------------------------------------------------------------------
    
Clone this object without having to cast the returned object.

Returns:
a clone of the this object.
    public MutableInterval copy() {
        return (MutableIntervalclone();
    }

    
Clone this object.

Returns:
a clone of this object.
    public Object clone() {
          throw new Error("Clone error");
    }
New to GrepCode? Check out our FAQ X