use of org.joda.time.DurationField in project joda-time by JodaOrg.
the class BaseSingleFieldPeriod method standardPeriodIn.
/**
* 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);
}
use of org.joda.time.DurationField in project joda-time by JodaOrg.
the class TestPreciseDurationField method test_hashCode.
public void test_hashCode() {
assertEquals(true, iField.hashCode() == iField.hashCode());
assertEquals(false, iField.hashCode() == ISOChronology.getInstance().minutes().hashCode());
DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
assertEquals(false, iField.hashCode() == dummy.hashCode());
dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000);
assertEquals(true, iField.hashCode() == dummy.hashCode());
dummy = new PreciseDurationField(DurationFieldType.millis(), 1000);
assertEquals(false, iField.hashCode() == dummy.hashCode());
}
use of org.joda.time.DurationField in project joda-time by JodaOrg.
the class TestPreciseDurationField method test_equals.
//-----------------------------------------------------------------------
public void test_equals() {
assertEquals(true, iField.equals(iField));
assertEquals(false, iField.equals(ISOChronology.getInstance().minutes()));
DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
assertEquals(false, iField.equals(dummy));
dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000);
assertEquals(true, iField.equals(dummy));
dummy = new PreciseDurationField(DurationFieldType.millis(), 1000);
assertEquals(false, iField.equals(dummy));
assertEquals(false, iField.equals(""));
assertEquals(false, iField.equals(null));
}
use of org.joda.time.DurationField in project joda-time by JodaOrg.
the class TestPreciseDurationField method testSerialization.
//-----------------------------------------------------------------------
public void testSerialization() throws Exception {
DurationField test = iField;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(test);
oos.close();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
DurationField result = (DurationField) ois.readObject();
ois.close();
assertEquals(test, result);
}
use of org.joda.time.DurationField in project joda-time by JodaOrg.
the class TestPreciseDurationField method test_compareTo.
//-----------------------------------------------------------------------
public void test_compareTo() {
assertEquals(0, iField.compareTo(iField));
assertEquals(-1, iField.compareTo(ISOChronology.getInstance().minutes()));
DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
assertEquals(1, iField.compareTo(dummy));
// } catch (ClassCastException ex) {}
try {
iField.compareTo(null);
fail();
} catch (NullPointerException ex) {
}
}
Aggregations