PageRenderTime 34ms CodeModel.GetById 1ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 23package org.mobicents.protocols.smpp.util;
 24
 25import java.util.Calendar;
 26import java.util.TimeZone;
 27
 28/**
 29 * Implementation of {@link org.mobicents.protocols.smpp.util.SMPPDate} that represents an
 30 * absolute time specification.
 31 * 
 32 * @version $Id: AbsoluteSMPPDate.java 463 2009-06-16 12:07:19Z orank $
 33 */
 34class AbsoluteSMPPDate extends SMPPDate {
 35    private static final long serialVersionUID = 2L;
 36    
 37    private Calendar calendar;
 38    private boolean hasTimeZone;
 39    
 40    AbsoluteSMPPDate(Calendar calendar) {
 41        this(calendar, true);
 42    }
 43    
 44    AbsoluteSMPPDate(Calendar calendar, boolean withTimeZone) {
 45        this.calendar = calendar;
 46        this.hasTimeZone = withTimeZone;
 47    }
 48    
 49    public Calendar getCalendar() {
 50        return calendar;
 51    }
 52
 53    public int getDay() {
 54        return calendar.get(Calendar.DAY_OF_MONTH);
 55    }
 56
 57    public int getHour() {
 58        return calendar.get(Calendar.HOUR_OF_DAY);
 59    }
 60
 61    public int getMinute() {
 62        return calendar.get(Calendar.MINUTE);
 63    }
 64
 65    public int getMonth() {
 66        return calendar.get(Calendar.MONTH) + 1;
 67    }
 68
 69    public int getSecond() {
 70        return calendar.get(Calendar.SECOND);
 71    }
 72
 73    public int getYear() {
 74        return calendar.get(Calendar.YEAR);
 75    }
 76
 77    public int getTenth() {
 78        return calendar.get(Calendar.MILLISECOND) / 100;
 79    }
 80    
 81    public boolean hasTimezone() {
 82        return hasTimeZone;
 83    }
 84
 85    public boolean isAbsolute() {
 86        return true;
 87    }
 88
 89    public TimeZone getTimeZone() {
 90        if (hasTimeZone) {
 91            return calendar.getTimeZone();
 92        } else {
 93            return null;
 94        }
 95    }
 96    
 97    public int getUtcOffset() {
 98        if (hasTimeZone) {
 99            TimeZone tz = calendar.getTimeZone();
100            int offset = Math.abs(tz.getOffset(System.currentTimeMillis()));
101            // Divide by 900k to get the number of 15 minute intervals in the
102            // offset.
103            return offset / 900000;
104        } else {
105            return 0;
106        }
107    }
108    
109    public char getSign() {
110        char sign;
111        if (!hasTimeZone) {
112            sign = (char) 0;
113        } else {
114            TimeZone tz = calendar.getTimeZone();
115            int offset = tz.getOffset(System.currentTimeMillis());
116            if (offset >= 0) {
117                sign = '+';
118            } else {
119                sign = '-';
120            }
121        }
122        return sign;
123    }
124    
125    @Override
126    public int getLength() {
127        return hasTimeZone ? 17 : 13;
128    }
129    
130    public boolean equals(Object obj) {
131        if (obj == null || !(obj instanceof AbsoluteSMPPDate)) {
132            return false;
133        }
134        AbsoluteSMPPDate other = (AbsoluteSMPPDate) obj;
135        return hasTimeZone == other.hasTimeZone
136            && compareCalendarFields(this.calendar, other.calendar);
137    }
138    
139    public int hashCode() {
140        int hc1 = calendar.hashCode();
141        return hc1 + (hasTimeZone ? 6203 : 7907);
142    }
143    
144    public String toString() {
145        return new StringBuffer(calendar.toString()).append(" hasTz=")
146        .append(hasTimeZone).toString();
147    }
148    
149    private boolean compareCalendarFields(Calendar calendar1, Calendar calendar2) {
150        boolean equal = true;
151        equal &= compareField(calendar1, calendar2, Calendar.YEAR);
152        equal &= compareField(calendar1, calendar2, Calendar.MONTH);
153        equal &= compareField(calendar1, calendar2, Calendar.DAY_OF_MONTH);
154        equal &= compareField(calendar1, calendar2, Calendar.HOUR_OF_DAY);
155        equal &= compareField(calendar1, calendar2, Calendar.MINUTE);
156        equal &= compareField(calendar1, calendar2, Calendar.SECOND);
157        int tenth1 = calendar1.get(Calendar.MILLISECOND) / 100;
158        int tenth2 = calendar2.get(Calendar.MILLISECOND) / 100;
159        equal &= tenth1 == tenth2;
160        if (hasTimeZone) {
161            int rawOffset1 = calendar1.getTimeZone().getRawOffset();
162            int rawOffset2 = calendar2.getTimeZone().getRawOffset();
163            equal &= rawOffset1 == rawOffset2;
164        }
165        return equal;
166    }
167    
168    private boolean compareField(Calendar calendar1, Calendar calendar2, int field) {
169        return calendar1.get(field) == calendar2.get(field);
170    }
171}