| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.value; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 11 |
import net.sf.saxon.lib.StringCollator; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
import net.sf.saxon.type.AtomicType; |
| 15 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 16 |
import net.sf.saxon.type.ConversionResult; |
| 17 |
import net.sf.saxon.type.ValidationFailure; |
| 18 |
|
| 19 |
import java.math.BigDecimal; |
| 20 |
import java.math.RoundingMode; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class YearMonthDurationValue extends DurationValue implements Comparable<YearMonthDurationValue> { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private YearMonthDurationValue() { |
| 36 |
typeLabel = BuiltInAtomicType.YEAR_MONTH_DURATION; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static ConversionResult makeYearMonthDurationValue(CharSequence s) { |
| 49 |
ConversionResult d = DurationValue.makeDuration(s, true, false); |
| 50 |
if (d instanceof ValidationFailure) { |
| 51 |
return d; |
| 52 |
} |
| 53 |
DurationValue dv = (DurationValue) d; |
| 54 |
return YearMonthDurationValue.fromMonths((dv.getYears() * 12 + dv.getMonths()) * dv.signum()); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public AtomicValue copyAsSubType(AtomicType typeLabel) { |
| 66 |
YearMonthDurationValue v = YearMonthDurationValue.fromMonths(getLengthInMonths()); |
| 67 |
v.typeLabel = typeLabel; |
| 68 |
return v; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public BuiltInAtomicType getPrimitiveType() { |
| 79 |
return BuiltInAtomicType.YEAR_MONTH_DURATION; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public CharSequence getPrimitiveStringValue() { |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
int y = getYears(); |
| 93 |
int m = getMonths(); |
| 94 |
|
| 95 |
FastStringBuffer sb = new FastStringBuffer(32); |
| 96 |
if (negative) { |
| 97 |
sb.append('-'); |
| 98 |
} |
| 99 |
sb.append('P'); |
| 100 |
if (y != 0) { |
| 101 |
sb.append(y + "Y"); |
| 102 |
} |
| 103 |
if (m != 0 || y == 0) { |
| 104 |
sb.append(m + "M"); |
| 105 |
} |
| 106 |
return sb; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public int getLengthInMonths() { |
| 117 |
return months * (negative ? -1 : +1); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public static YearMonthDurationValue fromMonths(int months) { |
| 128 |
YearMonthDurationValue mdv = new YearMonthDurationValue(); |
| 129 |
mdv.negative = months < 0; |
| 130 |
mdv.months = months < 0 ? -months : months; |
| 131 |
mdv.seconds = 0; |
| 132 |
mdv.nanoseconds = 0; |
| 133 |
return mdv; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public YearMonthDurationValue multiply(long factor) throws XPathException { |
| 144 |
|
| 145 |
if (Math.abs(factor) < 30_000 && Math.abs(months) < 30_000) { |
| 146 |
return YearMonthDurationValue.fromMonths((int)factor * getLengthInMonths()); |
| 147 |
} else { |
| 148 |
return multiply((double)factor); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public YearMonthDurationValue multiply(double n) throws XPathException { |
| 157 |
if (Double.isNaN(n)) { |
| 158 |
XPathException err = new XPathException("Cannot multiply a duration by NaN"); |
| 159 |
err.setErrorCode("FOCA0005"); |
| 160 |
throw err; |
| 161 |
} |
| 162 |
double m = (double) getLengthInMonths(); |
| 163 |
double product = n * m; |
| 164 |
if (Double.isInfinite(product) || product > Integer.MAX_VALUE || product < Integer.MIN_VALUE) { |
| 165 |
XPathException err = new XPathException("Overflow when multiplying a duration by a number"); |
| 166 |
err.setErrorCode("FODT0002"); |
| 167 |
throw err; |
| 168 |
} |
| 169 |
return fromMonths((int) Math.round(product)); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public DurationValue divide(double n) throws XPathException { |
| 178 |
if (Double.isNaN(n)) { |
| 179 |
XPathException err = new XPathException("Cannot divide a duration by NaN"); |
| 180 |
err.setErrorCode("FOCA0005"); |
| 181 |
throw err; |
| 182 |
} |
| 183 |
double m = (double) getLengthInMonths(); |
| 184 |
double product = m / n; |
| 185 |
if (Double.isInfinite(product) || product > Integer.MAX_VALUE || product < Integer.MIN_VALUE) { |
| 186 |
XPathException err = new XPathException("Overflow when dividing a duration by a number"); |
| 187 |
err.setErrorCode("FODT0002"); |
| 188 |
throw err; |
| 189 |
} |
| 190 |
return fromMonths((int) Math.round(product)); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public BigDecimalValue divide(DurationValue other) throws XPathException { |
| 202 |
if (other instanceof YearMonthDurationValue) { |
| 203 |
BigDecimal v1 = BigDecimal.valueOf(getLengthInMonths()); |
| 204 |
BigDecimal v2 = BigDecimal.valueOf(((YearMonthDurationValue) other).getLengthInMonths()); |
| 205 |
if (v2.signum() == 0) { |
| 206 |
XPathException err = new XPathException("Divide by zero (durations)"); |
| 207 |
err.setErrorCode("FOAR0001"); |
| 208 |
throw err; |
| 209 |
} |
| 210 |
return new BigDecimalValue(v1.divide(v2, 20, RoundingMode.HALF_EVEN)); |
| 211 |
} else { |
| 212 |
XPathException err = new XPathException("Cannot divide two durations of different type"); |
| 213 |
err.setErrorCode("XPTY0004"); |
| 214 |
throw err; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public DurationValue add(DurationValue other) throws XPathException { |
| 223 |
if (other instanceof YearMonthDurationValue) { |
| 224 |
return fromMonths(getLengthInMonths() + |
| 225 |
((YearMonthDurationValue) other).getLengthInMonths()); |
| 226 |
} else { |
| 227 |
XPathException err = new XPathException("Cannot add two durations of different type"); |
| 228 |
err.setErrorCode("XPTY0004"); |
| 229 |
throw err; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public DurationValue subtract(DurationValue other) throws XPathException { |
| 238 |
if (other instanceof YearMonthDurationValue) { |
| 239 |
return fromMonths(getLengthInMonths() - |
| 240 |
((YearMonthDurationValue) other).getLengthInMonths()); |
| 241 |
} else { |
| 242 |
XPathException err = new XPathException("Cannot subtract two durations of different type"); |
| 243 |
err.setErrorCode("XPTY0004"); |
| 244 |
throw err; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public DurationValue negate() { |
| 253 |
return fromMonths(-getLengthInMonths()); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public int compareTo(YearMonthDurationValue other) { |
| 269 |
return Integer.compare(getLengthInMonths(), other.getLengthInMonths()); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public AtomicMatchKey getXPathComparable(boolean ordered, StringCollator collator, int implicitTimezone) { |
| 284 |
return this; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
} |
| 289 |
|