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

The following examples show how to use org.joda.time.ReadablePeriod#getValue() . 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: 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 2
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 3
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 4
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 5
Source File: Time_22_BasePeriod_t.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 6
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 7
Source File: Time_10_BaseSingleFieldPeriod_s.java    From coming with MIT License 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 8
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 9
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 5 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, but only
 * those with a matching <code>PeriodType</code> can return true.
 *
 * @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;
    return (other.getPeriodType() == getPeriodType() && other.getValue(0) == getValue());
}
 
Example 10
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 11
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 5 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, but only
 * those with a matching <code>PeriodType</code> can return true.
 *
 * @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;
    return (other.getPeriodType() == getPeriodType() && other.getValue(0) == getValue());
}
 
Example 12
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 13
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 14
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 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_13_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 19
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 20
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;
}