1 16 package org.joda.time.field; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.DurationField; 21 import org.joda.time.DurationFieldType; 22 23 32 public final class MillisDurationField extends DurationField implements Serializable { 33 34 35 private static final long serialVersionUID = 2656707858124633367L; 36 37 38 public static final DurationField INSTANCE = new MillisDurationField(); 39 40 43 private MillisDurationField() { 44 super(); 45 } 46 47 public DurationFieldType getType() { 49 return DurationFieldType.millis(); 50 } 51 52 public String getName() { 53 return "millis"; 54 } 55 56 61 public boolean isSupported() { 62 return true; 63 } 64 65 70 public final boolean isPrecise() { 71 return true; 72 } 73 74 79 public final long getUnitMillis() { 80 return 1; 81 } 82 83 public int getValue(long duration) { 85 return FieldUtils.safeToInt(duration); 86 } 87 88 public long getValueAsLong(long duration) { 89 return duration; 90 } 91 92 public int getValue(long duration, long instant) { 93 return FieldUtils.safeToInt(duration); 94 } 95 96 public long getValueAsLong(long duration, long instant) { 97 return duration; 98 } 99 100 public long getMillis(int value) { 101 return value; 102 } 103 104 public long getMillis(long value) { 105 return value; 106 } 107 108 public long getMillis(int value, long instant) { 109 return value; 110 } 111 112 public long getMillis(long value, long instant) { 113 return value; 114 } 115 116 public long add(long instant, int value) { 117 return FieldUtils.safeAdd(instant, value); 118 } 119 120 public long add(long instant, long value) { 121 return FieldUtils.safeAdd(instant, value); 122 } 123 124 public int getDifference(long minuendInstant, long subtrahendInstant) { 125 return FieldUtils.safeToInt(FieldUtils.safeSubtract(minuendInstant, subtrahendInstant)); 126 } 127 128 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 129 return FieldUtils.safeSubtract(minuendInstant, subtrahendInstant); 130 } 131 132 public int compareTo(Object durationField) { 134 DurationField otherField = (DurationField) durationField; 135 long otherMillis = otherField.getUnitMillis(); 136 long thisMillis = getUnitMillis(); 137 if (thisMillis == otherMillis) { 139 return 0; 140 } 141 if (thisMillis < otherMillis) { 142 return -1; 143 } else { 144 return 1; 145 } 146 } 147 148 153 public String toString() { 154 return "DurationField[millis]"; 155 } 156 157 160 private Object readResolve() { 161 return INSTANCE; 162 } 163 164 } 165 | Popular Tags |