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

The following examples show how to use org.joda.time.ReadablePeriod#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: BaseChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param duration  the duration to query
 * @return the values of the period extracted from the duration
 */
public int[] get(ReadablePeriod period, long duration) {
    int size = period.size();
    int[] values = new int[size];
    if (duration != 0) {
        long current = 0;
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            if (field.isPrecise()) {
                int value = field.getDifference(duration, current);
                current = field.add(current, value);
                values[i] = value;
            }
        }
    }
    return values;
}
 
Example 2
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] addPeriodInto(int[] values, ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        if (value != 0) {
            int index = indexOf(type);
            if (index == -1) {
                throw new IllegalArgumentException(
                    "Period does not support field '" + type.getName() + "'");
            } else {
                values[index] = FieldUtils.safeAdd(getValue(index), value);
            }
        }
    }
    return values;
}
 
Example 3
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param duration  the duration to query
 * @return the values of the period extracted from the duration
 */
public int[] get(ReadablePeriod period, long duration) {
    int size = period.size();
    int[] values = new int[size];
    if (duration != 0) {
        long current = 0;
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            if (field.isPrecise()) {
                int value = field.getDifference(duration, current);
                current = field.add(current, value);
                values[i] = value;
            }
        }
    }
    return values;
}
 
Example 4
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Adds the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] addPeriodInto(int[] values, ReadablePeriod period) {
     for (int i = 0, isize = period.size(); i < isize; i++) {
         DurationFieldType type = period.getFieldType(i);
         int value = period.getValue(i);
         if (value != 0) {
             int index = indexOf(type);
             if (index == -1) {
                 throw new IllegalArgumentException(
                     "Period does not support field '" + type.getName() + "'");
             } else {
                 values[index] = FieldUtils.safeAdd(getValue(index), value);
             }
         }
     }
     return values;
}
 
Example 5
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance representing the number of complete standard length units
 * in the specified period.
 * <p>
 * This factory method converts all fields from the period to hours using standardised
 * durations for each field. Only those fields which have a precise duration in
 * the ISO UTC chronology can be converted.
 * <ul>
 * <li>One week consists of 7 days.
 * <li>One day consists of 24 hours.
 * <li>One hour consists of 60 minutes.
 * <li>One minute consists of 60 seconds.
 * <li>One second consists of 1000 milliseconds.
 * </ul>
 * Months and Years are imprecise and periods containing these values cannot be converted.
 *
 * @param period  the period to get the number of hours from, must not be null
 * @param millisPerUnit  the number of milliseconds in one standard unit of this period
 * @throws IllegalArgumentException if the period contains imprecise duration values
 */
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    if (period == null) {
        return 0;
    }
    Chronology iso = ISOChronology.getInstanceUTC();
    long duration = 0L;
    for (int i = 0; i < period.size(); i++) {
        int value = period.getValue(i);
        if (value != 0) {
            DurationField field = period.getFieldType(i).getField(iso);
            if (field.isPrecise() == false) {
                throw new IllegalArgumentException(
                        "Cannot convert period to duration as " + field.getName() +
                        " is not precise in the period " + period);
            }
            duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
        }
    }
    return FieldUtils.safeToInt(duration / millisPerUnit);
}
 
Example 6
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 8
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Private method called from constructor.
 */
private void setPeriodInternal(ReadablePeriod period) {
    int[] newValues = new int[size()];
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, newValues, value);
    }
    setValues(newValues);
}
 
Example 9
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param startInstant  the start instant of an interval to query
 * @param endInstant  the start instant of an interval to query
 * @return the values of the period extracted from the interval
 */
public int[] get(ReadablePeriod period, long startInstant, long endInstant) {
    int size = period.size();
    int[] values = new int[size];
    if (startInstant != endInstant) {
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            int value = field.getDifference(endInstant, startInstant);
            startInstant = field.add(startInstant, value);
            values[i] = value;
        }
    }
    return values;
}
 
Example 10
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, values, value);
    }
    return values;
}
 
Example 11
Source File: Time_27_PeriodFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 12
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds the period to the instant, specifying the number of times to add.
 *
 * @param period  the period to add, null means add nothing
 * @param instant  the instant to add to
 * @param scalar  the number of times to add
 * @return the updated instant
 */
public long add(ReadablePeriod period, long instant, int scalar) {
    if (scalar != 0 && period != null) {
        for (int i = 0, isize = period.size(); i < isize; i++) {
            long value = period.getValue(i); // use long to allow for multiplication (fits OK)
            if (value != 0) {
                instant = period.getFieldType(i).getField(this).add(instant, value * scalar);
            }
        }
    }
    return instant;
}
 
Example 13
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Merges the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
     for (int i = 0, isize = period.size(); i < isize; i++) {
         DurationFieldType type = period.getFieldType(i);
         int value = period.getValue(i);
         checkAndUpdate(type, values, value);
     }
     return values;
}
 
Example 14
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Private method called from constructor.
 */
private void setPeriodInternal(ReadablePeriod period) {
    int[] newValues = new int[size()];
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, newValues, value);
    }
    iValues = newValues;
}
 
Example 15
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Merges the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
     for (int i = 0, isize = period.size(); i < isize; i++) {
         DurationFieldType type = period.getFieldType(i);
         int value = period.getValue(i);
         checkAndUpdate(type, values, value);
     }
     return values;
}
 
Example 16
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Private method called from constructor.
 */
private void setPeriodInternal(ReadablePeriod period) {
    int[] newValues = new int[size()];
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, newValues, value);
    }
    iValues = newValues;
}
 
Example 17
Source File: Time_13_PeriodFormatterBuilder_t.java    From coming with MIT License 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 18
Source File: Time_27_PeriodFormatterBuilder_t.java    From coming with MIT License 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 19
Source File: AbstractPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares this object with the specified object for equality based
 * on the value of each field. All ReadablePeriod instances are accepted.
 * <p>
 * Note that a period of 1 day is not equal to a period of 24 hours,
 * nor is 1 hour equal to 60 minutes. Only periods with the same amount
 * in each field are equal.
 * <p>
 * This is because periods represent an abstracted definition of a time
 * period (eg. a day may not actually be 24 hours, it might be 23 or 25
 * at daylight savings boundary).
 * <p>
 * To compare the actual duration of two periods, convert both to
 * {@link org.joda.time.Duration Duration}s, an operation that emphasises
 * that the result may differ according to the date you choose.
 *
 * @param period  a readable period to check against
 * @return true if all the field values are equal, false if
 *  not or the period is null or of an incorrect type
 */
public boolean equals(Object period) {
    if (this == period) {
        return true;
    }
    if (period instanceof ReadablePeriod == false) {
        return false;
    }
    ReadablePeriod other = (ReadablePeriod) period;
    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 true;
}
 
Example 20
Source File: HourMinuteSecond.java    From fenixedu-academic with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Gets a copy of this date with the specified period added.
 * <p>
 * If the addition is zero, then <code>this</code> is returned. Fields in the period that aren't present in the partial are
 * ignored.
 * <p>
 * This method is typically used to add multiple copies of complex period instances. Adding one field is best achieved using
 * methods like {@link #withFieldAdded(DurationFieldType, int)} or {@link #plusHours(int)}.
 * 
 * @param period
 *            the period to add to this one, null means zero
 * @param scalar
 *            the amount of times to add, such as -1 to subtract once
 * @return a copy of this instance with the period added
 * @throws ArithmeticException
 *             if the new datetime exceeds the capacity
 */
public HourMinuteSecond withPeriodAdded(ReadablePeriod period, int scalar) {
    if (period == null || scalar == 0) {
        return this;
    }
    int[] newValues = getValues();
    for (int i = 0; i < period.size(); i++) {
        DurationFieldType fieldType = period.getFieldType(i);
        int index = indexOf(fieldType);
        if (index >= 0) {
            newValues = getField(index).add(this, index, newValues, FieldUtils.safeMultiply(period.getValue(i), scalar));
        }
    }
    return new HourMinuteSecond(this, newValues);
}