1 16 package org.joda.time.field; 17 18 import org.joda.time.DurationFieldType; 19 20 29 public class PreciseDurationField extends BaseDurationField { 30 31 private static final long serialVersionUID = -8346152187724495365L; 32 33 34 private final long iUnitMillis; 35 36 42 public PreciseDurationField(DurationFieldType type, long unitMillis) { 43 super(type); 44 iUnitMillis = unitMillis; 45 } 46 47 53 public final boolean isPrecise() { 54 return true; 55 } 56 57 62 public final long getUnitMillis() { 63 return iUnitMillis; 64 } 65 66 75 public long getValueAsLong(long duration, long instant) { 76 return duration / iUnitMillis; } 78 79 87 public long getMillis(int value, long instant) { 88 return value * iUnitMillis; } 90 91 99 public long getMillis(long value, long instant) { 100 return FieldUtils.safeMultiply(value, iUnitMillis); 101 } 102 103 public long add(long instant, int value) { 104 long addition = value * iUnitMillis; return FieldUtils.safeAdd(instant, addition); 106 } 107 108 public long add(long instant, long value) { 109 long addition = FieldUtils.safeMultiply(value, iUnitMillis); 110 return FieldUtils.safeAdd(instant, addition); 111 } 112 113 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 114 long difference = FieldUtils.safeSubtract(minuendInstant, subtrahendInstant); 115 return difference / iUnitMillis; 116 } 117 118 126 public boolean equals(Object obj) { 127 if (this == obj) { 128 return true; 129 } else if (obj instanceof PreciseDurationField) { 130 PreciseDurationField other = (PreciseDurationField) obj; 131 return (getType() == other.getType()) && (iUnitMillis == other.iUnitMillis); 132 } 133 return false; 134 } 135 136 141 public int hashCode() { 142 long millis = iUnitMillis; 143 int hash = (int) (millis ^ (millis >>> 32)); 144 hash += getType().hashCode(); 145 return hash; 146 } 147 148 } 149 | Popular Tags |