/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/util/AbsoluteSMPPDate.java

http://mobicents.googlecode.com/ · Java · 171 lines · 117 code · 25 blank · 29 comment · 17 complexity · 974c5acf7eeeea27d2d1c8f2d4948e70 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.smpp.util;
  23. import java.util.Calendar;
  24. import java.util.TimeZone;
  25. /**
  26. * Implementation of {@link org.mobicents.protocols.smpp.util.SMPPDate} that represents an
  27. * absolute time specification.
  28. *
  29. * @version $Id: AbsoluteSMPPDate.java 463 2009-06-16 12:07:19Z orank $
  30. */
  31. class AbsoluteSMPPDate extends SMPPDate {
  32. private static final long serialVersionUID = 2L;
  33. private Calendar calendar;
  34. private boolean hasTimeZone;
  35. AbsoluteSMPPDate(Calendar calendar) {
  36. this(calendar, true);
  37. }
  38. AbsoluteSMPPDate(Calendar calendar, boolean withTimeZone) {
  39. this.calendar = calendar;
  40. this.hasTimeZone = withTimeZone;
  41. }
  42. public Calendar getCalendar() {
  43. return calendar;
  44. }
  45. public int getDay() {
  46. return calendar.get(Calendar.DAY_OF_MONTH);
  47. }
  48. public int getHour() {
  49. return calendar.get(Calendar.HOUR_OF_DAY);
  50. }
  51. public int getMinute() {
  52. return calendar.get(Calendar.MINUTE);
  53. }
  54. public int getMonth() {
  55. return calendar.get(Calendar.MONTH) + 1;
  56. }
  57. public int getSecond() {
  58. return calendar.get(Calendar.SECOND);
  59. }
  60. public int getYear() {
  61. return calendar.get(Calendar.YEAR);
  62. }
  63. public int getTenth() {
  64. return calendar.get(Calendar.MILLISECOND) / 100;
  65. }
  66. public boolean hasTimezone() {
  67. return hasTimeZone;
  68. }
  69. public boolean isAbsolute() {
  70. return true;
  71. }
  72. public TimeZone getTimeZone() {
  73. if (hasTimeZone) {
  74. return calendar.getTimeZone();
  75. } else {
  76. return null;
  77. }
  78. }
  79. public int getUtcOffset() {
  80. if (hasTimeZone) {
  81. TimeZone tz = calendar.getTimeZone();
  82. int offset = Math.abs(tz.getOffset(System.currentTimeMillis()));
  83. // Divide by 900k to get the number of 15 minute intervals in the
  84. // offset.
  85. return offset / 900000;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. public char getSign() {
  91. char sign;
  92. if (!hasTimeZone) {
  93. sign = (char) 0;
  94. } else {
  95. TimeZone tz = calendar.getTimeZone();
  96. int offset = tz.getOffset(System.currentTimeMillis());
  97. if (offset >= 0) {
  98. sign = '+';
  99. } else {
  100. sign = '-';
  101. }
  102. }
  103. return sign;
  104. }
  105. @Override
  106. public int getLength() {
  107. return hasTimeZone ? 17 : 13;
  108. }
  109. public boolean equals(Object obj) {
  110. if (obj == null || !(obj instanceof AbsoluteSMPPDate)) {
  111. return false;
  112. }
  113. AbsoluteSMPPDate other = (AbsoluteSMPPDate) obj;
  114. return hasTimeZone == other.hasTimeZone
  115. && compareCalendarFields(this.calendar, other.calendar);
  116. }
  117. public int hashCode() {
  118. int hc1 = calendar.hashCode();
  119. return hc1 + (hasTimeZone ? 6203 : 7907);
  120. }
  121. public String toString() {
  122. return new StringBuffer(calendar.toString()).append(" hasTz=")
  123. .append(hasTimeZone).toString();
  124. }
  125. private boolean compareCalendarFields(Calendar calendar1, Calendar calendar2) {
  126. boolean equal = true;
  127. equal &= compareField(calendar1, calendar2, Calendar.YEAR);
  128. equal &= compareField(calendar1, calendar2, Calendar.MONTH);
  129. equal &= compareField(calendar1, calendar2, Calendar.DAY_OF_MONTH);
  130. equal &= compareField(calendar1, calendar2, Calendar.HOUR_OF_DAY);
  131. equal &= compareField(calendar1, calendar2, Calendar.MINUTE);
  132. equal &= compareField(calendar1, calendar2, Calendar.SECOND);
  133. int tenth1 = calendar1.get(Calendar.MILLISECOND) / 100;
  134. int tenth2 = calendar2.get(Calendar.MILLISECOND) / 100;
  135. equal &= tenth1 == tenth2;
  136. if (hasTimeZone) {
  137. int rawOffset1 = calendar1.getTimeZone().getRawOffset();
  138. int rawOffset2 = calendar2.getTimeZone().getRawOffset();
  139. equal &= rawOffset1 == rawOffset2;
  140. }
  141. return equal;
  142. }
  143. private boolean compareField(Calendar calendar1, Calendar calendar2, int field) {
  144. return calendar1.get(field) == calendar2.get(field);
  145. }
  146. }