| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.lang3.time; |
| 18 | |
|
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.Calendar; |
| 21 | |
import java.util.Date; |
| 22 | |
import java.util.GregorianCalendar; |
| 23 | |
import java.util.TimeZone; |
| 24 | |
|
| 25 | |
import org.apache.commons.lang3.StringUtils; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class DurationFormatUtils { |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public DurationFormatUtils() { |
| 53 | 1 | super(); |
| 54 | 1 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public static final String ISO_EXTENDED_FORMAT_PATTERN = "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'"; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public static String formatDurationHMS(final long durationMillis) { |
| 76 | 11 | return formatDuration(durationMillis, "H:mm:ss.SSS"); |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public static String formatDurationISO(final long durationMillis) { |
| 91 | 5 | return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static String formatDuration(final long durationMillis, final String format) { |
| 106 | 99 | return formatDuration(durationMillis, format, true); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public static String formatDuration(long durationMillis, final String format, final boolean padWithZeros) { |
| 123 | |
|
| 124 | 104 | final Token[] tokens = lexx(format); |
| 125 | |
|
| 126 | 104 | int days = 0; |
| 127 | 104 | int hours = 0; |
| 128 | 104 | int minutes = 0; |
| 129 | 104 | int seconds = 0; |
| 130 | 104 | int milliseconds = 0; |
| 131 | |
|
| 132 | 104 | if (Token.containsTokenWithValue(tokens, d) ) { |
| 133 | 77 | days = (int) (durationMillis / DateUtils.MILLIS_PER_DAY); |
| 134 | 77 | durationMillis = durationMillis - (days * DateUtils.MILLIS_PER_DAY); |
| 135 | |
} |
| 136 | 104 | if (Token.containsTokenWithValue(tokens, H) ) { |
| 137 | 87 | hours = (int) (durationMillis / DateUtils.MILLIS_PER_HOUR); |
| 138 | 87 | durationMillis = durationMillis - (hours * DateUtils.MILLIS_PER_HOUR); |
| 139 | |
} |
| 140 | 104 | if (Token.containsTokenWithValue(tokens, m) ) { |
| 141 | 88 | minutes = (int) (durationMillis / DateUtils.MILLIS_PER_MINUTE); |
| 142 | 88 | durationMillis = durationMillis - (minutes * DateUtils.MILLIS_PER_MINUTE); |
| 143 | |
} |
| 144 | 104 | if (Token.containsTokenWithValue(tokens, s) ) { |
| 145 | 88 | seconds = (int) (durationMillis / DateUtils.MILLIS_PER_SECOND); |
| 146 | 88 | durationMillis = durationMillis - (seconds * DateUtils.MILLIS_PER_SECOND); |
| 147 | |
} |
| 148 | 104 | if (Token.containsTokenWithValue(tokens, S) ) { |
| 149 | 19 | milliseconds = (int) durationMillis; |
| 150 | |
} |
| 151 | |
|
| 152 | 104 | return format(tokens, 0, 0, days, hours, minutes, seconds, milliseconds, padWithZeros); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public static String formatDurationWords( |
| 167 | |
final long durationMillis, |
| 168 | |
final boolean suppressLeadingZeroElements, |
| 169 | |
final boolean suppressTrailingZeroElements) { |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | 69 | String duration = formatDuration(durationMillis, "d' days 'H' hours 'm' minutes 's' seconds'"); |
| 175 | 69 | if (suppressLeadingZeroElements) { |
| 176 | |
|
| 177 | 12 | duration = " " + duration; |
| 178 | 12 | String tmp = StringUtils.replaceOnce(duration, " 0 days", ""); |
| 179 | 12 | if (tmp.length() != duration.length()) { |
| 180 | 10 | duration = tmp; |
| 181 | 10 | tmp = StringUtils.replaceOnce(duration, " 0 hours", ""); |
| 182 | 10 | if (tmp.length() != duration.length()) { |
| 183 | 8 | duration = tmp; |
| 184 | 8 | tmp = StringUtils.replaceOnce(duration, " 0 minutes", ""); |
| 185 | 8 | duration = tmp; |
| 186 | 8 | if (tmp.length() != duration.length()) { |
| 187 | 0 | duration = StringUtils.replaceOnce(tmp, " 0 seconds", ""); |
| 188 | |
} |
| 189 | |
} |
| 190 | |
} |
| 191 | 12 | if (duration.length() != 0) { |
| 192 | |
|
| 193 | 12 | duration = duration.substring(1); |
| 194 | |
} |
| 195 | |
} |
| 196 | 69 | if (suppressTrailingZeroElements) { |
| 197 | 12 | String tmp = StringUtils.replaceOnce(duration, " 0 seconds", ""); |
| 198 | 12 | if (tmp.length() != duration.length()) { |
| 199 | 6 | duration = tmp; |
| 200 | 6 | tmp = StringUtils.replaceOnce(duration, " 0 minutes", ""); |
| 201 | 6 | if (tmp.length() != duration.length()) { |
| 202 | 2 | duration = tmp; |
| 203 | 2 | tmp = StringUtils.replaceOnce(duration, " 0 hours", ""); |
| 204 | 2 | if (tmp.length() != duration.length()) { |
| 205 | 2 | duration = StringUtils.replaceOnce(tmp, " 0 days", ""); |
| 206 | |
} |
| 207 | |
} |
| 208 | |
} |
| 209 | |
} |
| 210 | |
|
| 211 | 69 | duration = " " + duration; |
| 212 | 69 | duration = StringUtils.replaceOnce(duration, " 1 seconds", " 1 second"); |
| 213 | 69 | duration = StringUtils.replaceOnce(duration, " 1 minutes", " 1 minute"); |
| 214 | 69 | duration = StringUtils.replaceOnce(duration, " 1 hours", " 1 hour"); |
| 215 | 69 | duration = StringUtils.replaceOnce(duration, " 1 days", " 1 day"); |
| 216 | 69 | return duration.trim(); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public static String formatPeriodISO(final long startMillis, final long endMillis) { |
| 230 | 0 | return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault()); |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public static String formatPeriod(final long startMillis, final long endMillis, final String format) { |
| 243 | 95230 | return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault()); |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros, |
| 270 | |
final TimeZone timezone) { |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | 95232 | final Token[] tokens = lexx(format); |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | 95232 | final Calendar start = Calendar.getInstance(timezone); |
| 283 | 95232 | start.setTime(new Date(startMillis)); |
| 284 | 95232 | final Calendar end = Calendar.getInstance(timezone); |
| 285 | 95232 | end.setTime(new Date(endMillis)); |
| 286 | |
|
| 287 | |
|
| 288 | 95232 | int milliseconds = end.get(Calendar.MILLISECOND) - start.get(Calendar.MILLISECOND); |
| 289 | 95232 | int seconds = end.get(Calendar.SECOND) - start.get(Calendar.SECOND); |
| 290 | 95232 | int minutes = end.get(Calendar.MINUTE) - start.get(Calendar.MINUTE); |
| 291 | 95232 | int hours = end.get(Calendar.HOUR_OF_DAY) - start.get(Calendar.HOUR_OF_DAY); |
| 292 | 95232 | int days = end.get(Calendar.DAY_OF_MONTH) - start.get(Calendar.DAY_OF_MONTH); |
| 293 | 95232 | int months = end.get(Calendar.MONTH) - start.get(Calendar.MONTH); |
| 294 | 95232 | int years = end.get(Calendar.YEAR) - start.get(Calendar.YEAR); |
| 295 | |
|
| 296 | |
|
| 297 | 95232 | while (milliseconds < 0) { |
| 298 | 0 | milliseconds += 1000; |
| 299 | 0 | seconds -= 1; |
| 300 | |
} |
| 301 | 95232 | while (seconds < 0) { |
| 302 | 0 | seconds += 60; |
| 303 | 0 | minutes -= 1; |
| 304 | |
} |
| 305 | 95232 | while (minutes < 0) { |
| 306 | 0 | minutes += 60; |
| 307 | 0 | hours -= 1; |
| 308 | |
} |
| 309 | 95233 | while (hours < 0) { |
| 310 | 1 | hours += 24; |
| 311 | 1 | days -= 1; |
| 312 | |
} |
| 313 | |
|
| 314 | 95232 | if (Token.containsTokenWithValue(tokens, M)) { |
| 315 | 1502 | while (days < 0) { |
| 316 | 10 | days += start.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 317 | 10 | months -= 1; |
| 318 | 10 | start.add(Calendar.MONTH, 1); |
| 319 | |
} |
| 320 | |
|
| 321 | 1618 | while (months < 0) { |
| 322 | 126 | months += 12; |
| 323 | 126 | years -= 1; |
| 324 | |
} |
| 325 | |
|
| 326 | 1492 | if (!Token.containsTokenWithValue(tokens, y) && years != 0) { |
| 327 | 2912 | while (years != 0) { |
| 328 | 1456 | months += 12 * years; |
| 329 | 1456 | years = 0; |
| 330 | |
} |
| 331 | |
} |
| 332 | |
} else { |
| 333 | |
|
| 334 | |
|
| 335 | 93740 | if( !Token.containsTokenWithValue(tokens, y) ) { |
| 336 | 93737 | int target = end.get(Calendar.YEAR); |
| 337 | 93737 | if (months < 0) { |
| 338 | |
|
| 339 | 379 | target -= 1; |
| 340 | |
} |
| 341 | |
|
| 342 | 104933 | while ( (start.get(Calendar.YEAR) != target)) { |
| 343 | 11196 | days += start.getActualMaximum(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR); |
| 344 | |
|
| 345 | |
|
| 346 | 11196 | if (start instanceof GregorianCalendar && |
| 347 | |
start.get(Calendar.MONTH) == Calendar.FEBRUARY && |
| 348 | |
start.get(Calendar.DAY_OF_MONTH) == 29) { |
| 349 | 2247 | days += 1; |
| 350 | |
} |
| 351 | |
|
| 352 | 11196 | start.add(Calendar.YEAR, 1); |
| 353 | |
|
| 354 | 11196 | days += start.get(Calendar.DAY_OF_YEAR); |
| 355 | |
} |
| 356 | |
|
| 357 | 93737 | years = 0; |
| 358 | |
} |
| 359 | |
|
| 360 | 134219 | while( start.get(Calendar.MONTH) != end.get(Calendar.MONTH) ) { |
| 361 | 40479 | days += start.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 362 | 40479 | start.add(Calendar.MONTH, 1); |
| 363 | |
} |
| 364 | |
|
| 365 | 93740 | months = 0; |
| 366 | |
|
| 367 | 93740 | while (days < 0) { |
| 368 | 0 | days += start.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 369 | 0 | months -= 1; |
| 370 | 0 | start.add(Calendar.MONTH, 1); |
| 371 | |
} |
| 372 | |
|
| 373 | |
} |
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | 95232 | if (!Token.containsTokenWithValue(tokens, d)) { |
| 380 | 87896 | hours += 24 * days; |
| 381 | 87896 | days = 0; |
| 382 | |
} |
| 383 | 95232 | if (!Token.containsTokenWithValue(tokens, H)) { |
| 384 | 8827 | minutes += 60 * hours; |
| 385 | 8827 | hours = 0; |
| 386 | |
} |
| 387 | 95232 | if (!Token.containsTokenWithValue(tokens, m)) { |
| 388 | 8826 | seconds += 60 * minutes; |
| 389 | 8826 | minutes = 0; |
| 390 | |
} |
| 391 | 95232 | if (!Token.containsTokenWithValue(tokens, s)) { |
| 392 | 8826 | milliseconds += 1000 * seconds; |
| 393 | 8826 | seconds = 0; |
| 394 | |
} |
| 395 | |
|
| 396 | 95232 | return format(tokens, years, months, days, hours, minutes, seconds, milliseconds, padWithZeros); |
| 397 | |
} |
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
static String format(final Token[] tokens, final int years, final int months, final int days, final int hours, final int minutes, final int seconds, |
| 415 | |
int milliseconds, final boolean padWithZeros) { |
| 416 | 95336 | final StringBuilder buffer = new StringBuilder(); |
| 417 | 95336 | boolean lastOutputSeconds = false; |
| 418 | 95336 | final int sz = tokens.length; |
| 419 | 536971 | for (int i = 0; i < sz; i++) { |
| 420 | 441635 | final Token token = tokens[i]; |
| 421 | 441635 | final Object value = token.getValue(); |
| 422 | 441635 | final int count = token.getCount(); |
| 423 | 441635 | if (value instanceof StringBuilder) { |
| 424 | 173190 | buffer.append(value.toString()); |
| 425 | |
} else { |
| 426 | 268445 | if (value == y) { |
| 427 | 26 | buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(years), count, '0') : Integer |
| 428 | |
.toString(years)); |
| 429 | 26 | lastOutputSeconds = false; |
| 430 | 268419 | } else if (value == M) { |
| 431 | 1501 | buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(months), count, '0') : Integer |
| 432 | |
.toString(months)); |
| 433 | 1501 | lastOutputSeconds = false; |
| 434 | 266918 | } else if (value == d) { |
| 435 | 7413 | buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(days), count, '0') : Integer |
| 436 | |
.toString(days)); |
| 437 | 7413 | lastOutputSeconds = false; |
| 438 | 259505 | } else if (value == H) { |
| 439 | 86492 | buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(hours), count, '0') : Integer |
| 440 | |
.toString(hours)); |
| 441 | 86492 | lastOutputSeconds = false; |
| 442 | 173013 | } else if (value == m) { |
| 443 | 86494 | buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(minutes), count, '0') : Integer |
| 444 | |
.toString(minutes)); |
| 445 | 86494 | lastOutputSeconds = false; |
| 446 | 86519 | } else if (value == s) { |
| 447 | 86494 | buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(seconds), count, '0') : Integer |
| 448 | |
.toString(seconds)); |
| 449 | 86494 | lastOutputSeconds = true; |
| 450 | 25 | } else if (value == S) { |
| 451 | 25 | if (lastOutputSeconds) { |
| 452 | 19 | milliseconds += 1000; |
| 453 | 19 | final String str = padWithZeros |
| 454 | |
? StringUtils.leftPad(Integer.toString(milliseconds), count, '0') |
| 455 | |
: Integer.toString(milliseconds); |
| 456 | 19 | buffer.append(str.substring(1)); |
| 457 | 19 | } else { |
| 458 | 6 | buffer.append(padWithZeros |
| 459 | |
? StringUtils.leftPad(Integer.toString(milliseconds), count, '0') |
| 460 | |
: Integer.toString(milliseconds)); |
| 461 | |
} |
| 462 | 25 | lastOutputSeconds = false; |
| 463 | |
} |
| 464 | |
} |
| 465 | |
} |
| 466 | 95336 | return buffer.toString(); |
| 467 | |
} |
| 468 | |
|
| 469 | 1 | static final Object y = "y"; |
| 470 | 1 | static final Object M = "M"; |
| 471 | 1 | static final Object d = "d"; |
| 472 | 1 | static final Object H = "H"; |
| 473 | 1 | static final Object m = "m"; |
| 474 | 1 | static final Object s = "s"; |
| 475 | 1 | static final Object S = "S"; |
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | |
static Token[] lexx(final String format) { |
| 484 | 95339 | final char[] array = format.toCharArray(); |
| 485 | 95339 | final ArrayList<Token> list = new ArrayList<Token>(array.length); |
| 486 | |
|
| 487 | 95339 | boolean inLiteral = false; |
| 488 | |
|
| 489 | |
|
| 490 | 95339 | StringBuilder buffer = null; |
| 491 | 95339 | Token previous = null; |
| 492 | 95339 | final int sz = array.length; |
| 493 | 539661 | for(int i=0; i<sz; i++) { |
| 494 | 444322 | final char ch = array[i]; |
| 495 | 444322 | if(inLiteral && ch != '\'') { |
| 496 | 2176 | buffer.append(ch); |
| 497 | 2176 | continue; |
| 498 | |
} |
| 499 | 442146 | Object value = null; |
| 500 | 442146 | switch(ch) { |
| 501 | |
|
| 502 | |
case '\'' : |
| 503 | 676 | if(inLiteral) { |
| 504 | 338 | buffer = null; |
| 505 | 338 | inLiteral = false; |
| 506 | |
} else { |
| 507 | 338 | buffer = new StringBuilder(); |
| 508 | 338 | list.add(new Token(buffer)); |
| 509 | 338 | inLiteral = true; |
| 510 | |
} |
| 511 | 338 | break; |
| 512 | 66 | case 'y' : value = y; break; |
| 513 | 1521 | case 'M' : value = M; break; |
| 514 | 7440 | case 'd' : value = d; break; |
| 515 | 86496 | case 'H' : value = H; break; |
| 516 | 86512 | case 'm' : value = m; break; |
| 517 | 86512 | case 's' : value = s; break; |
| 518 | 60 | case 'S' : value = S; break; |
| 519 | |
default : |
| 520 | 172863 | if(buffer == null) { |
| 521 | 172863 | buffer = new StringBuilder(); |
| 522 | 172863 | list.add(new Token(buffer)); |
| 523 | |
} |
| 524 | 172863 | buffer.append(ch); |
| 525 | |
} |
| 526 | |
|
| 527 | 442146 | if(value != null) { |
| 528 | 268607 | if(previous != null && previous.getValue() == value) { |
| 529 | 144 | previous.increment(); |
| 530 | |
} else { |
| 531 | 268463 | final Token token = new Token(value); |
| 532 | 268463 | list.add(token); |
| 533 | 268463 | previous = token; |
| 534 | |
} |
| 535 | 268607 | buffer = null; |
| 536 | |
} |
| 537 | |
} |
| 538 | 95339 | return list.toArray( new Token[list.size()] ); |
| 539 | |
} |
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
static class Token { |
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
|
| 550 | |
|
| 551 | |
|
| 552 | |
|
| 553 | |
|
| 554 | |
static boolean containsTokenWithValue(final Token[] tokens, final Object value) { |
| 555 | 571912 | final int sz = tokens.length; |
| 556 | 2432635 | for (int i = 0; i < sz; i++) { |
| 557 | 2129143 | if (tokens[i].getValue() == value) { |
| 558 | 268420 | return true; |
| 559 | |
} |
| 560 | |
} |
| 561 | 303492 | return false; |
| 562 | |
} |
| 563 | |
|
| 564 | |
private final Object value; |
| 565 | |
private int count; |
| 566 | |
|
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
|
| 571 | |
|
| 572 | 441665 | Token(final Object value) { |
| 573 | 441665 | this.value = value; |
| 574 | 441665 | this.count = 1; |
| 575 | 441665 | } |
| 576 | |
|
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | 32 | Token(final Object value, final int count) { |
| 585 | 32 | this.value = value; |
| 586 | 32 | this.count = count; |
| 587 | 32 | } |
| 588 | |
|
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
void increment() { |
| 593 | 144 | count++; |
| 594 | 144 | } |
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | |
|
| 599 | |
|
| 600 | |
|
| 601 | |
int getCount() { |
| 602 | 441635 | return count; |
| 603 | |
} |
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
|
| 609 | |
|
| 610 | |
Object getValue() { |
| 611 | 2744046 | return value; |
| 612 | |
} |
| 613 | |
|
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
@Override |
| 621 | |
public boolean equals(final Object obj2) { |
| 622 | 33 | if (obj2 instanceof Token) { |
| 623 | 32 | final Token tok2 = (Token) obj2; |
| 624 | 32 | if (this.value.getClass() != tok2.value.getClass()) { |
| 625 | 1 | return false; |
| 626 | |
} |
| 627 | 31 | if (this.count != tok2.count) { |
| 628 | 1 | return false; |
| 629 | |
} |
| 630 | 30 | if (this.value instanceof StringBuilder) { |
| 631 | 11 | return this.value.toString().equals(tok2.value.toString()); |
| 632 | 19 | } else if (this.value instanceof Number) { |
| 633 | 1 | return this.value.equals(tok2.value); |
| 634 | |
} else { |
| 635 | 18 | return this.value == tok2.value; |
| 636 | |
} |
| 637 | |
} |
| 638 | 1 | return false; |
| 639 | |
} |
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | |
|
| 648 | |
@Override |
| 649 | |
public int hashCode() { |
| 650 | 0 | return this.value.hashCode(); |
| 651 | |
} |
| 652 | |
|
| 653 | |
|
| 654 | |
|
| 655 | |
|
| 656 | |
|
| 657 | |
|
| 658 | |
@Override |
| 659 | |
public String toString() { |
| 660 | 58 | return StringUtils.repeat(this.value.toString(), this.count); |
| 661 | |
} |
| 662 | |
} |
| 663 | |
|
| 664 | |
} |