1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.base.BaseDuration; 21 import org.joda.time.field.FieldUtils; 22 23 37 public final class Duration 38 extends BaseDuration 39 implements ReadableDuration, Serializable { 40 41 42 public static final Duration ZERO = new Duration(0L); 43 44 45 private static final long serialVersionUID = 2471658376918L; 46 47 52 public Duration(long duration) { 53 super(duration); 54 } 55 56 63 public Duration(long startInstant, long endInstant) { 64 super(startInstant, endInstant); 65 } 66 67 74 public Duration(ReadableInstant start, ReadableInstant end) { 75 super(start, end); 76 } 77 78 85 public Duration(Object duration) { 86 super(duration); 87 } 88 89 96 public Duration toDuration() { 97 return this; 98 } 99 100 107 public Duration withMillis(long duration) { 108 if (duration == getMillis()) { 109 return this; 110 } 111 return new Duration(duration); 112 } 113 114 124 public Duration withDurationAdded(long durationToAdd, int scalar) { 125 if (durationToAdd == 0 || scalar == 0) { 126 return this; 127 } 128 long add = FieldUtils.safeMultiply(durationToAdd, scalar); 129 long duration = FieldUtils.safeAdd(getMillis(), add); 130 return new Duration(duration); 131 } 132 133 143 public Duration withDurationAdded(ReadableDuration durationToAdd, int scalar) { 144 if (durationToAdd == null || scalar == 0) { 145 return this; 146 } 147 return withDurationAdded(durationToAdd.getMillis(), scalar); 148 } 149 150 160 public Duration plus(long amount) { 161 return withDurationAdded(amount, 1); 162 } 163 164 173 public Duration plus(ReadableDuration amount) { 174 if (amount == null) { 175 return this; 176 } 177 return withDurationAdded(amount.getMillis(), 1); 178 } 179 180 189 public Duration minus(long amount) { 190 return withDurationAdded(amount, -1); 191 } 192 193 202 public Duration minus(ReadableDuration amount) { 203 if (amount == null) { 204 return this; 205 } 206 return withDurationAdded(amount.getMillis(), -1); 207 } 208 209 } 210 | Popular Tags |