1 16 package org.joda.time.field; 17 18 import org.joda.time.DurationField; 19 import org.joda.time.DurationFieldType; 20 21 32 public class ScaledDurationField extends DecoratedDurationField { 33 34 private static final long serialVersionUID = -3205227092378684157L; 35 36 private final int iScalar; 37 38 46 public ScaledDurationField(DurationField field, DurationFieldType type, int scalar) { 47 super(field, type); 48 if (scalar == 0 || scalar == 1) { 49 throw new IllegalArgumentException ("The scalar must not be 0 or 1"); 50 } 51 iScalar = scalar; 52 } 53 54 public int getValue(long duration) { 55 return getWrappedField().getValue(duration) / iScalar; 56 } 57 58 public long getValueAsLong(long duration) { 59 return getWrappedField().getValueAsLong(duration) / iScalar; 60 } 61 62 public int getValue(long duration, long instant) { 63 return getWrappedField().getValue(duration, instant) / iScalar; 64 } 65 66 public long getValueAsLong(long duration, long instant) { 67 return getWrappedField().getValueAsLong(duration, instant) / iScalar; 68 } 69 70 public long getMillis(int value) { 71 long scaled = ((long) value) * ((long) iScalar); 72 return getWrappedField().getMillis(scaled); 73 } 74 75 public long getMillis(long value) { 76 long scaled = FieldUtils.safeMultiply(value, iScalar); 77 return getWrappedField().getMillis(scaled); 78 } 79 80 public long getMillis(int value, long instant) { 81 long scaled = ((long) value) * ((long) iScalar); 82 return getWrappedField().getMillis(scaled, instant); 83 } 84 85 public long getMillis(long value, long instant) { 86 long scaled = FieldUtils.safeMultiply(value, iScalar); 87 return getWrappedField().getMillis(scaled, instant); 88 } 89 90 public long add(long instant, int value) { 91 long scaled = ((long) value) * ((long) iScalar); 92 return getWrappedField().add(instant, scaled); 93 } 94 95 public long add(long instant, long value) { 96 long scaled = FieldUtils.safeMultiply(value, iScalar); 97 return getWrappedField().add(instant, scaled); 98 } 99 100 public int getDifference(long minuendInstant, long subtrahendInstant) { 101 return getWrappedField().getDifference(minuendInstant, subtrahendInstant) / iScalar; 102 } 103 104 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 105 return getWrappedField().getDifferenceAsLong(minuendInstant, subtrahendInstant) / iScalar; 106 } 107 108 public long getUnitMillis() { 109 return getWrappedField().getUnitMillis() * iScalar; 110 } 111 112 118 public int getScalar() { 119 return iScalar; 120 } 121 122 129 public boolean equals(Object obj) { 130 if (this == obj) { 131 return true; 132 } else if (obj instanceof ScaledDurationField) { 133 ScaledDurationField other = (ScaledDurationField) obj; 134 return (getWrappedField().equals(other.getWrappedField())) && 135 (getType() == other.getType()) && 136 (iScalar == other.iScalar); 137 } 138 return false; 139 } 140 141 146 public int hashCode() { 147 long scalar = iScalar; 148 int hash = (int) (scalar ^ (scalar >>> 32)); 149 hash += getType().hashCode(); 150 hash += getWrappedField().hashCode(); 151 return hash; 152 } 153 154 } 155 | Popular Tags |