PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/andya/confluence/utils/Duration.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 110 lines | 55 code | 15 blank | 40 comment | 5 complexity | d699c1a38b6efece11c596ca78e61fc6 MD5 | raw file
  1. /*
  2. * Copyright (c) 2006, 2007 Andy Armstrong, Kelsey Grant and other contributors.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * The names of contributors may not
  14. * be used to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package org.andya.confluence.utils;
  29. import com.atlassian.core.util.DateUtils;
  30. import com.atlassian.core.util.InvalidDurationException;
  31. /**
  32. * A number that represents a duration in seconds.
  33. */
  34. public class Duration extends Number {
  35. private static final long serialVersionUID = -91792353858202089L;
  36. private final long duration;
  37. public Duration(long duration) {
  38. this.duration = duration;
  39. }
  40. /**
  41. * Creates a duration if the string is in the correct format, else returns
  42. * null.
  43. */
  44. public static Duration parseDuration(String durationString) {
  45. if (!isDuration(durationString))
  46. return null;
  47. durationString = durationString.trim();
  48. try {
  49. long duration = DateUtils.getDuration(durationString);
  50. return new Duration(duration);
  51. } catch (InvalidDurationException e) {
  52. return null;
  53. }
  54. }
  55. /**
  56. * Returns true if the specified string is a duration in the Confluence
  57. * format.
  58. */
  59. public static boolean isDuration(String durationString) {
  60. // todo: this should be part of DateUtils
  61. durationString = durationString.trim();
  62. int durationLength = durationString.length();
  63. if (durationLength == 0)
  64. return false;
  65. char lastChar = durationString.charAt(durationLength - 1);
  66. return "mhdw".indexOf(String.valueOf(lastChar)) >= 0;
  67. }
  68. /** Returns the string representation of this duration (e.g. -1h 30m). */
  69. public String getDurationString() {
  70. return DateUtils.getDurationStringWithNegative(longValue());
  71. }
  72. public double doubleValue() {
  73. return duration;
  74. }
  75. public float floatValue() {
  76. return duration;
  77. }
  78. public int intValue() {
  79. return (int) duration;
  80. }
  81. public long longValue() {
  82. return duration;
  83. }
  84. public boolean equals(Object object) {
  85. if (object instanceof Duration)
  86. return longValue() == ((Duration) object).longValue();
  87. return false;
  88. }
  89. public int hashCode() {
  90. return intValue();
  91. }
  92. public String toString() {
  93. return getDurationString();
  94. }
  95. }