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.joda.time.field;
 
Divides a DateTimeField such that the retrieved values are reduced by a fixed divisor. The field's unit duration is scaled accordingly, but the range duration is unchanged.

DividedDateTimeField is thread-safe and immutable.

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
See also:
org.joda.time.field.RemainderDateTimeField
 
 public class DividedDateTimeField extends DecoratedDateTimeField {
 
     private static final long serialVersionUID = 8318475124230605365L;
 
     // Shared with RemainderDateTimeField.
     final int iDivisor;
     final DurationField iDurationField;
 
     private final int iMin;
     private final int iMax;

    
Constructor.

Parameters:
field the field to wrap, like "year()".
type the field type this field will actually use
divisor divisor, such as 100 years in a century
Throws:
java.lang.IllegalArgumentException if divisor is less than two
 
     public DividedDateTimeField(DateTimeField field,
                                 DateTimeFieldType typeint divisor) {
         super(fieldtype);
                 
         if (divisor < 2) {
             throw new IllegalArgumentException("The divisor must be at least 2");
         }
 
         DurationField unitField = field.getDurationField();
         if (unitField == null) {
              = null;
         } else {
              = new ScaledDurationField(
                 unitFieldtype.getDurationType(), divisor);
         }
 
          = divisor;
 
         int i = field.getMinimumValue();
         int min = (i >= 0) ? i / divisor : ((i + 1) / divisor - 1);
 
         int j = field.getMaximumValue();
         int max = (j >= 0) ? j / divisor : ((j + 1) / divisor - 1);
 
          = min;
          = max;
     }

    
Construct a DividedDateTimeField that compliments the given RemainderDateTimeField.

Parameters:
remainderField complimentary remainder field, like "yearOfCentury()".
type the field type this field will actually use
 
     public DividedDateTimeField(RemainderDateTimeField remainderFieldDateTimeFieldType type) {
         super(remainderField.getWrappedField(), type);
         int divisor =  = remainderField.iDivisor;
          = remainderField.iRangeField;
 
         DateTimeField field = getWrappedField();
         int i = field.getMinimumValue();
         int min = (i >= 0) ? i / divisor : ((i + 1) / divisor - 1);
 
         int j = field.getMaximumValue();
         int max = (j >= 0) ? j / divisor : ((j + 1) / divisor - 1);
         = min;
         = max;
    }

    
Get the amount of scaled units from the specified time instant.

Parameters:
instant the time instant in millis to query.
Returns:
the amount of scaled units extracted from the input.
    public int get(long instant) {
        int value = getWrappedField().get(instant);
        if (value >= 0) {
            return value / ;
        } else {
            return ((value + 1) / ) - 1;
        }
    }

    
Add the specified amount of scaled units to the specified time instant. The amount added may be negative.

Parameters:
instant the time instant in millis to update.
amount the amount of scaled units to add (can be negative).
Returns:
the updated time instant.
    public long add(long instantint amount) {
        return getWrappedField().add(instantamount * );
    }

    
Add the specified amount of scaled units to the specified time instant. The amount added may be negative.

Parameters:
instant the time instant in millis to update.
amount the amount of scaled units to add (can be negative).
Returns:
the updated time instant.
    public long add(long instantlong amount) {
        return getWrappedField().add(instantamount * );
    }

    
Add to the scaled component of the specified time instant, wrapping around within that component if necessary.

Parameters:
instant the time instant in millis to update.
amount the amount of scaled units to add (can be negative).
Returns:
the updated time instant.
    public long addWrapField(long instantint amount) {
        return set(instant, FieldUtils.getWrappedValue(get(instant), amount));
    }
    public int getDifference(long minuendInstantlong subtrahendInstant) {
        return getWrappedField().getDifference(minuendInstantsubtrahendInstant) / ;
    }
    public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
        return getWrappedField().getDifferenceAsLong(minuendInstantsubtrahendInstant) / ;
    }

    
Set the specified amount of scaled units to the specified time instant.

Parameters:
instant the time instant in millis to update.
value value of scaled units to set.
Returns:
the updated time instant.
Throws:
java.lang.IllegalArgumentException if value is too large or too small.
    public long set(long instantint value) {
        FieldUtils.verifyValueBounds(thisvalue);
        int remainder = getRemainder(getWrappedField().get(instant));
        return getWrappedField().set(instantvalue *  + remainder);
    }

    
Returns a scaled version of the wrapped field's unit duration field.
    public DurationField getDurationField() {
        return ;
    }

    
Get the minimum value for the field.

Returns:
the minimum value
    public int getMinimumValue() {
        return ;
    }

    
Get the maximum value for the field.

Returns:
the maximum value
    public int getMaximumValue() {
        return ;
    }
    public long roundFloor(long instant) {
        DateTimeField field = getWrappedField();
        return field.roundFloor(field.set(instantget(instant) * ));
    }
    public long remainder(long instant) {
        return set(instantget(getWrappedField().remainder(instant)));
    }

    
Returns the divisor applied, in the field's units.

Returns:
the divisor
    public int getDivisor() {
        return ;
    }
    private int getRemainder(int value) {
        if (value >= 0) {
            return value % ;
        } else {
            return ( - 1) + ((value + 1) % );
        }
    }
New to GrepCode? Check out our FAQ X