Java Code Examples for org.joda.time.ReadablePartial#size()

The following examples show how to use org.joda.time.ReadablePartial#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GJChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
    // overridden as superclass algorithm can't handle
    // 2004-02-29 + 48 months -> 2008-02-29 type dates
    if (valueToAdd == 0) {
        return values;
    }
    if (DateTimeUtils.isContiguous(partial)) {
        long instant = 0L;
        for (int i = 0, isize = partial.size(); i < isize; i++) {
            instant = partial.getFieldType(i).getField(GJChronology.this).set(instant, values[i]);
        }
        instant = add(instant, valueToAdd);
        return GJChronology.this.get(partial, instant);
    } else {
        return super.add(partial, fieldIndex, values, valueToAdd);
    }
}
 
Example 2
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculates the number of whole units between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, validated to not be null
 * @param end  the end partial date, validated to not be null
 * @param zeroInstance  the zero instance constant, must not be null
 * @return the period
 * @throws IllegalArgumentException if the partials are null or invalid
 */
protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
    if (start == null || end == null) {
        throw new IllegalArgumentException("ReadablePartial objects must not be null");
    }
    if (start.size() != end.size()) {
        throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
    }
    for (int i = 0, isize = start.size(); i < isize; i++) {
        if (start.getFieldType(i) != end.getFieldType(i)) {
            throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
        }
    }
    if (DateTimeUtils.isContiguous(start) == false) {
        throw new IllegalArgumentException("ReadablePartial objects must be contiguous");
    }
    Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC();
    int[] values = chrono.get(zeroInstance, chrono.set(start, START_1972), chrono.set(end, START_1972));
    return values[0];
}
 
Example 3
Source File: Time_10_BaseSingleFieldPeriod_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Calculates the number of whole units between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, validated to not be null
 * @param end  the end partial date, validated to not be null
 * @param zeroInstance  the zero instance constant, must not be null
 * @return the period
 * @throws IllegalArgumentException if the partials are null or invalid
 */
protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
    if (start == null || end == null) {
        throw new IllegalArgumentException("ReadablePartial objects must not be null");
    }
    if (start.size() != end.size()) {
        throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
    }
    for (int i = 0, isize = start.size(); i < isize; i++) {
        if (start.getFieldType(i) != end.getFieldType(i)) {
            throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
        }
    }
    if (DateTimeUtils.isContiguous(start) == false) {
        throw new IllegalArgumentException("ReadablePartial objects must be contiguous");
    }
    Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC();
    int[] values = chrono.get(zeroInstance, chrono.set(start, 0L), chrono.set(end, 0L));
    return values[0];
}
 
Example 4
Source File: Time_10_BaseSingleFieldPeriod_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Calculates the number of whole units between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, validated to not be null
 * @param end  the end partial date, validated to not be null
 * @param zeroInstance  the zero instance constant, must not be null
 * @return the period
 * @throws IllegalArgumentException if the partials are null or invalid
 */
protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
    if (start == null || end == null) {
        throw new IllegalArgumentException("ReadablePartial objects must not be null");
    }
    if (start.size() != end.size()) {
        throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
    }
    for (int i = 0, isize = start.size(); i < isize; i++) {
        if (start.getFieldType(i) != end.getFieldType(i)) {
            throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
        }
    }
    if (DateTimeUtils.isContiguous(start) == false) {
        throw new IllegalArgumentException("ReadablePartial objects must be contiguous");
    }
    Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC();
    int[] values = chrono.get(zeroInstance, chrono.set(start, START_1972), chrono.set(end, START_1972));
    return values[0];
}
 
Example 5
Source File: Time_6_GJChronology_t.java    From coming with MIT License 6 votes vote down vote up
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
    // overridden as superclass algorithm can't handle
    // 2004-02-29 + 48 months -> 2008-02-29 type dates
    if (valueToAdd == 0) {
        return values;
    }
    if (DateTimeUtils.isContiguous(partial)) {
        long instant = 0L;
        for (int i = 0, isize = partial.size(); i < isize; i++) {
            instant = partial.getFieldType(i).getField(GJChronology.this).set(instant, values[i]);
        }
        instant = add(instant, valueToAdd);
        return GJChronology.this.get(partial, instant);
    } else {
        return super.add(partial, fieldIndex, values, valueToAdd);
    }
}
 
Example 6
Source File: GJChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
    // overridden as superclass algorithm can't handle
    // 2004-02-29 + 48 months -> 2008-02-29 type dates
    if (valueToAdd == 0) {
        return values;
    }
    if (DateTimeUtils.isContiguous(partial)) {
        long instant = 0L;
        for (int i = 0, isize = partial.size(); i < isize; i++) {
            instant = partial.getFieldType(i).getField(GJChronology.this).set(instant, values[i]);
        }
        instant = add(instant, valueToAdd);
        return GJChronology.this.get(partial, instant);
    } else {
        return super.add(partial, fieldIndex, values, valueToAdd);
    }
}
 
Example 7
Source File: BasicMonthOfYearDateTimeField.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
    // overridden as superclass algorithm can't handle
    // 2004-02-29 + 48 months -> 2008-02-29 type dates
    if (valueToAdd == 0) {
        return values;
    }
    if (partial.size() > 0 && partial.getFieldType(0).equals(DateTimeFieldType.monthOfYear()) && fieldIndex == 0) {
        // month is largest field and being added to, such as month-day
        int curMonth0 = partial.getValue(0) - 1;
        int newMonth = ((curMonth0 + (valueToAdd % 12) + 12) % 12) + 1;
        return set(partial, 0, values, newMonth);
    }
    if (DateTimeUtils.isContiguous(partial)) {
        long instant = 0L;
        for (int i = 0, isize = partial.size(); i < isize; i++) {
            instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
        }
        instant = add(instant, valueToAdd);
        return iChronology.get(partial, instant);
    } else {
        return super.add(partial, fieldIndex, values, valueToAdd);
    }
}
 
Example 8
Source File: Nopol2017_0086_t.java    From coming with MIT License 6 votes vote down vote up
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
    // overridden as superclass algorithm can't handle
    // 2004-02-29 + 48 months -> 2008-02-29 type dates
    if (valueToAdd == 0) {
        return values;
    }
        // month is largest field and being added to, such as month-day
    if ((!(((fieldIndex) != (1)) && (valueToAdd <= values.length))) || (valueToAdd < -1)) {
        long instant = 0L;
        for (int i = 0, isize = partial.size(); i < isize; i++) {
            instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
        }
        instant = add(instant, valueToAdd);
        return iChronology.get(partial, instant);
    } else {
        return super.add(partial, fieldIndex, values, valueToAdd);
    }
}
 
Example 9
Source File: AbstractPartial.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compares this ReadablePartial with another returning true if the chronology,
 * field types and values are equal.
 *
 * @param partial  an object to check against
 * @return true if fields and values are equal
 */
public boolean equals(Object partial) {
    if (this == partial) {
        return true;
    }
    if (partial instanceof ReadablePartial == false) {
        return false;
    }
    ReadablePartial other = (ReadablePartial) partial;
    if (size() != other.size()) {
        return false;
    }
    for (int i = 0, isize = size(); i < isize; i++) {
        if (getValue(i) != other.getValue(i) || getFieldType(i) != other.getFieldType(i)) {
            return false;
        }
    }
    return FieldUtils.equals(getChronology(), other.getChronology());
}
 
Example 10
Source File: BasicMonthOfYearDateTimeField.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
    // overridden as superclass algorithm can't handle
    // 2004-02-29 + 48 months -> 2008-02-29 type dates
    if (valueToAdd == 0) {
        return values;
    }
    if (partial.size() > 0 && partial.getFieldType(0).equals(DateTimeFieldType.monthOfYear()) && fieldIndex == 0) {
        // month is largest field and being added to, such as month-day
        int curMonth0 = partial.getValue(0) - 1;
        int newMonth = ((curMonth0 + (valueToAdd % 12) + 12) % 12) + 1;
        return set(partial, 0, values, newMonth);
    }
    if (DateTimeUtils.isContiguous(partial)) {
        long instant = 0L;
        for (int i = 0, isize = partial.size(); i < isize; i++) {
            instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
        }
        instant = add(instant, valueToAdd);
        return iChronology.get(partial, instant);
    } else {
        return super.add(partial, fieldIndex, values, valueToAdd);
    }
}
 
Example 11
Source File: GJChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int getMaximumValue(ReadablePartial partial, int[] values) {
    Chronology chrono = GJChronology.getInstanceUTC();
    long instant = 0L;
    for (int i = 0, isize = partial.size(); i < isize; i++) {
        DateTimeField field = partial.getFieldType(i).getField(chrono);
        if (values[i] <= field.getMaximumValue(instant)) {
            instant = field.set(instant, values[i]);
        }
    }
    return getMaximumValue(instant);
}
 
Example 12
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the values of a partial from an instant.
 *
 * @param partial  the partial instant to use
 * @param instant  the instant to query
 * @return the values of the partial extracted from the instant
 */
public int[] get(ReadablePartial partial, long instant) {
    int size = partial.size();
    int[] values = new int[size];
    for (int i = 0; i < size; i++) {
        values[i] = partial.getFieldType(i).getField(this).get(instant);
    }
    return values;
}
 
Example 13
Source File: DayOfMonthOfFixedYearDateTimeField.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int getMaximumValue(ReadablePartial partial, int[] values) {
  int size = partial.size();
  for (int i = 0; i < size; i++) {
    if (partial.getFieldType(i) == DateTimeFieldType.monthOfYear()) {
      int month = values[i];
      return this.daysInMonth[month - 1];
    }
  }
  return this.getMaximumValue();
}
 
Example 14
Source File: Time_6_GJChronology_s.java    From coming with MIT License 5 votes vote down vote up
public int getMaximumValue(ReadablePartial partial, int[] values) {
    Chronology chrono = GJChronology.getInstanceUTC();
    long instant = 0L;
    for (int i = 0, isize = partial.size(); i < isize; i++) {
        DateTimeField field = partial.getFieldType(i).getField(chrono);
        if (values[i] <= field.getMaximumValue(instant)) {
            instant = field.set(instant, values[i]);
        }
    }
    return getMaximumValue(instant);
}
 
Example 15
Source File: GJChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int getMaximumValue(ReadablePartial partial, int[] values) {
    Chronology chrono = GJChronology.getInstanceUTC();
    long instant = 0L;
    for (int i = 0, isize = partial.size(); i < isize; i++) {
        DateTimeField field = partial.getFieldType(i).getField(chrono);
        if (values[i] <= field.getMaximumValue(instant)) {
            instant = field.set(instant, values[i]);
        }
    }
    return getMaximumValue(instant);
}
 
Example 16
Source File: BaseDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets a value using the specified partial instant.
 * <p>
 * The value of this field (specified by the index) will be set.
 * If the value is invalid, an exception if thrown.
 * <p>
 * If setting this field would make other fields invalid, then those fields
 * may be changed. For example if the current date is the 31st January, and
 * the month is set to February, the day would be invalid. Instead, the day
 * would be changed to the closest value - the 28th/29th February as appropriate.
 * 
 * @param partial  the partial instant
 * @param fieldIndex  the index of this field in the instant
 * @param values  the values to update
 * @param newValue  the value to set, in the units of the field
 * @return the updated values
 * @throws IllegalArgumentException if the value is invalid
 */
public int[] set(ReadablePartial partial, int fieldIndex, int[] values, int newValue) {
    FieldUtils.verifyValueBounds(this, newValue, getMinimumValue(partial, values), getMaximumValue(partial, values));
    values[fieldIndex] = newValue;
    
    // may need to adjust smaller fields
    for (int i = fieldIndex + 1; i < partial.size(); i++) {
        DateTimeField field = partial.getField(i);
        if (values[i] > field.getMaximumValue(partial, values)) {
            values[i] = field.getMaximumValue(partial, values);
        }
        if (values[i] < field.getMinimumValue(partial, values)) {
            values[i] = field.getMinimumValue(partial, values);
        }
    }
    return values;
}
 
Example 17
Source File: BaseDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets a value using the specified partial instant.
 * <p>
 * The value of this field (specified by the index) will be set.
 * If the value is invalid, an exception if thrown.
 * <p>
 * If setting this field would make other fields invalid, then those fields
 * may be changed. For example if the current date is the 31st January, and
 * the month is set to February, the day would be invalid. Instead, the day
 * would be changed to the closest value - the 28th/29th February as appropriate.
 * 
 * @param partial  the partial instant
 * @param fieldIndex  the index of this field in the instant
 * @param values  the values to update
 * @param newValue  the value to set, in the units of the field
 * @return the updated values
 * @throws IllegalArgumentException if the value is invalid
 */
public int[] set(ReadablePartial partial, int fieldIndex, int[] values, int newValue) {
    FieldUtils.verifyValueBounds(this, newValue, getMinimumValue(partial, values), getMaximumValue(partial, values));
    values[fieldIndex] = newValue;
    
    // may need to adjust smaller fields
    for (int i = fieldIndex + 1; i < partial.size(); i++) {
        DateTimeField field = partial.getField(i);
        if (values[i] > field.getMaximumValue(partial, values)) {
            values[i] = field.getMaximumValue(partial, values);
        }
        if (values[i] < field.getMinimumValue(partial, values)) {
            values[i] = field.getMinimumValue(partial, values);
        }
    }
    return values;
}
 
Example 18
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Sets the partial into the instant.
 *
 * @param partial  the partial instant to use
 * @param instant  the instant to update
 * @return the updated instant
 */
public long set(ReadablePartial partial, long instant) {
    for (int i = 0, isize = partial.size(); i < isize; i++) {
        instant = partial.getFieldType(i).getField(this).set(instant, partial.getValue(i));
    }
    return instant;
}
 
Example 19
Source File: ReadablePartialConverter.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Extracts the values of the partial from an object of this converter's type.
 * The chrono parameter is a hint to the converter, should it require a
 * chronology to aid in conversion.
 * 
 * @param fieldSource  a partial that provides access to the fields.
 *  This partial may be incomplete and only getFieldType(int) should be used
 * @param object  the object to convert
 * @param chrono  the chronology to use, which is the non-null result of getChronology()
 * @return the array of field values that match the fieldSource, must be non-null valid
 * @throws ClassCastException if the object is invalid
 */
public int[] getPartialValues(ReadablePartial fieldSource, Object object, Chronology chrono) {
    ReadablePartial input = (ReadablePartial) object;
    int size = fieldSource.size();
    int[] values = new int[size];
    for (int i = 0; i < size; i++) {
        values[i] = input.get(fieldSource.getFieldType(i));
    }
    chrono.validate(fieldSource, values);
    return values;
}
 
Example 20
Source File: Cardumen_00190_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Get the maximum value for the field, which is one more than the wrapped
 * field's maximum value.
 * 
 * @return the maximum value
 */
public int getMaximumValue(ReadablePartial instant, int[] values) {
    return instant.size();
}