1 16 package org.joda.time.field; 17 18 import java.io.Serializable ; 19 import org.joda.time.DurationField; 20 import org.joda.time.DurationFieldType; 21 22 37 public abstract class BaseDurationField extends DurationField implements Serializable { 38 39 40 private static final long serialVersionUID = -2554245107589433218L; 41 42 43 private final DurationFieldType iType; 44 45 protected BaseDurationField(DurationFieldType type) { 46 super(); 47 if (type == null) { 48 throw new IllegalArgumentException ("The type must not be null"); 49 } 50 iType = type; 51 } 52 53 public final DurationFieldType getType() { 54 return iType; 55 } 56 57 public final String getName() { 58 return iType.getName(); 59 } 60 61 64 public final boolean isSupported() { 65 return true; 66 } 67 68 77 public int getValue(long duration) { 78 return FieldUtils.safeToInt(getValueAsLong(duration)); 79 } 80 81 89 public long getValueAsLong(long duration) { 90 return duration / getUnitMillis(); 91 } 92 93 109 public int getValue(long duration, long instant) { 110 return FieldUtils.safeToInt(getValueAsLong(duration, instant)); 111 } 112 113 121 public long getMillis(int value) { 122 return value * getUnitMillis(); } 124 125 133 public long getMillis(long value) { 134 return FieldUtils.safeMultiply(value, getUnitMillis()); 135 } 136 137 public int getDifference(long minuendInstant, long subtrahendInstant) { 140 return FieldUtils.safeToInt(getDifferenceAsLong(minuendInstant, subtrahendInstant)); 141 } 142 143 public int compareTo(Object durationField) { 145 DurationField otherField = (DurationField) durationField; 146 long otherMillis = otherField.getUnitMillis(); 147 long thisMillis = getUnitMillis(); 148 if (thisMillis == otherMillis) { 150 return 0; 151 } 152 if (thisMillis < otherMillis) { 153 return -1; 154 } else { 155 return 1; 156 } 157 } 158 159 164 public String toString() { 165 return "DurationField[" + getName() + ']'; 166 } 167 168 } 169 | Popular Tags |