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.field;
 
Abstract datetime field class that defines its own DurationField, which delegates back into this ImpreciseDateTimeField.

This DateTimeField is useful for defining DateTimeFields that are composed of imprecise durations. If both duration fields are precise, then a PreciseDateTimeField should be used instead.

When defining imprecise DateTimeFields where a matching DurationField is already available, just extend BaseDateTimeField directly so as not to create redundant DurationField instances.

ImpreciseDateTimeField is thread-safe and immutable, and its subclasses must be as well.

Author(s):
Brian S O'Neill
Since:
1.0
See also:
PreciseDateTimeField
 
 public abstract class ImpreciseDateTimeField extends BaseDateTimeField {
 
     @SuppressWarnings("unused")
     private static final long serialVersionUID = 7190739608550251860L;
 
     final long iUnitMillis;
     private final DurationField iDurationField;

    
Constructor.

Parameters:
type the field type
unitMillis the average duration unit milliseconds
 
     public ImpreciseDateTimeField(DateTimeFieldType typelong unitMillis) {
         super(type);
          = unitMillis;
          = new LinkedDurationField(type.getDurationType());
     }
 
     public abstract int get(long instant);
 
     public abstract long set(long instantint value);
 
     public abstract long add(long instantint value);
 
     public abstract long add(long instantlong value);

    
Computes the difference between two instants, as measured in the units of this field. Any fractional units are dropped from the result. Calling getDifference reverses the effect of calling add. In the following code:
 long instant = ...
 int v = ...
 int age = getDifference(add(instant, v), instant);
 
The value 'age' is the same as the value 'v'.

The default implementation call getDifferenceAsLong and converts the return value to an int.

Parameters:
minuendInstant the milliseconds from 1970-01-01T00:00:00Z to subtract from
subtrahendInstant the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
Returns:
the difference in the units of this field
 
     public int getDifference(long minuendInstantlong subtrahendInstant) {
         return FieldUtils.safeToInt(getDifferenceAsLong(minuendInstantsubtrahendInstant));
     }

    
Computes the difference between two instants, as measured in the units of this field. Any fractional units are dropped from the result. Calling getDifference reverses the effect of calling add. In the following code:
 long instant = ...
 long v = ...
 long age = getDifferenceAsLong(add(instant, v), instant);
 
The value 'age' is the same as the value 'v'.

The default implementation performs a guess-and-check algorithm using getDurationField().getUnitMillis() and the add() method. Subclasses are encouraged to provide a more efficient implementation.

Parameters:
minuendInstant the milliseconds from 1970-01-01T00:00:00Z to subtract from
subtrahendInstant the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
Returns:
the difference in the units of this field
    public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
        if (minuendInstant < subtrahendInstant) {
            return -getDifferenceAsLong(subtrahendInstantminuendInstant);
        }
        
        long difference = (minuendInstant - subtrahendInstant) / ;
        if (add(subtrahendInstantdifference) < minuendInstant) {
            do {
                difference++;
            } while (add(subtrahendInstantdifference) <= minuendInstant);
            difference--;
        } else if (add(subtrahendInstantdifference) > minuendInstant) {
            do {
                difference--;
            } while (add(subtrahendInstantdifference) > minuendInstant);
        }
        return difference;
    }
    public final DurationField getDurationField() {
        return ;
    }
    public abstract DurationField getRangeDurationField();
    public abstract long roundFloor(long instant);
    protected final long getDurationUnitMillis() {
        return ;
    }
    private final class LinkedDurationField extends BaseDurationField {
        private static final long serialVersionUID = -203813474600094134L;
        LinkedDurationField(DurationFieldType type) {
            super(type);
        }
    
        public boolean isPrecise() {
            return false;
        }
    
        public long getUnitMillis() {
            return ;
        }
        public int getValue(long durationlong instant) {
            return ImpreciseDateTimeField.this
                .getDifference(instant + durationinstant);
        }
        public long getValueAsLong(long durationlong instant) {
            return ImpreciseDateTimeField.this
                .getDifferenceAsLong(instant + durationinstant);
        }
        
        public long getMillis(int valuelong instant) {
            return ImpreciseDateTimeField.this.add(instantvalue) - instant;
        }
        public long getMillis(long valuelong instant) {
            return ImpreciseDateTimeField.this.add(instantvalue) - instant;
        }
        public long add(long instantint value) {
            return ImpreciseDateTimeField.this.add(instantvalue);
        }
        
        public long add(long instantlong value) {
            return ImpreciseDateTimeField.this.add(instantvalue);
        }
        
        public int getDifference(long minuendInstantlong subtrahendInstant) {
            return ImpreciseDateTimeField.this
                .getDifference(minuendInstantsubtrahendInstant);
        }
        
        public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
            return ImpreciseDateTimeField.this
                .getDifferenceAsLong(minuendInstantsubtrahendInstant);
        }
    }
New to GrepCode? Check out our FAQ X