Coverage Report - org.jtheque.utils.bean.IntDate
 
Classes in this File Line Coverage Branch Coverage Complexity
IntDate
95%
60/63
78%
22/28
2.529
IntDate$Fields
33%
1/3
100%
6/6
2.529
 
 1  
 package org.jtheque.utils.bean;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.text.DateFormat;
 21  
 import java.text.SimpleDateFormat;
 22  
 import java.util.Calendar;
 23  
 import java.util.Locale;
 24  
 
 25  
 /**
 26  
  * This class represent an int data in format yyyymmdd.
 27  
  *
 28  
  * @author Baptiste Wicht
 29  
  */
 30  4
 public final class IntDate implements Serializable, Comparable<IntDate> {
 31  
     private static final long serialVersionUID = -5493916511728423774L;
 32  
     private static final int DATE_STR_LENGTH = 20;
 33  
 
 34  
     /**
 35  
      * This internal class contains all the different fields of an IntDate.
 36  
      *
 37  
      * @author Baptiste Wicht
 38  
      */
 39  
     public static final class Fields {
 40  
         public static final int MONTH = Calendar.MONTH;
 41  
         public static final int DAY = Calendar.DAY_OF_MONTH;
 42  
         public static final int YEAR = Calendar.YEAR;
 43  
 
 44  
         /**
 45  
          * Create a new Fields. This class isn't instanciable
 46  
          */
 47  0
         private Fields() {
 48  
 
 49  0
         }
 50  
 
 51  
         /**
 52  
          * Test the validity of a field.
 53  
          *
 54  
          * @param field The field to test.
 55  
          * @return <code>true</code> if the field is valid else <code>false</code>.
 56  
          */
 57  
         public static boolean isValid(int field) {
 58  26
             return field == MONTH || field == DAY || field == YEAR;
 59  
         }
 60  
     }
 61  
 
 62  2
     private static final IntDate TODAY_DATE = new IntDate(Calendar.getInstance());
 63  
 
 64  44
     private final DateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
 65  
     private final Calendar calendar;
 66  
 
 67  
     /**
 68  
      * Construct a new int date from the int representation of the date.
 69  
      *
 70  
      * @param date The int representation (yyyymmdd) of the date.
 71  
      */
 72  
     public IntDate(int date) {
 73  40
         super();
 74  
 
 75  40
         calendar = Calendar.getInstance();
 76  40
         calendar.clear();
 77  
 
 78  40
         String dateString = Integer.toString(date);
 79  
 
 80  40
         int day = Integer.parseInt(dateString.substring(6, 8));
 81  40
         int month = Integer.parseInt(dateString.substring(4, 6)) - 1;
 82  40
         int year = Integer.parseInt(dateString.substring(0, 4));
 83  
 
 84  40
         calendar.set(year, month, day);
 85  40
     }
 86  
 
 87  
     /**
 88  
      * Construct a new date from a Calendar instance. Not all the precision of the Calendar object will be used.
 89  
      *
 90  
      * @param calendar The calendar instance of the date.
 91  
      */
 92  
     private IntDate(Calendar calendar) {
 93  2
         super();
 94  
 
 95  2
         this.calendar = calendar;
 96  2
     }
 97  
 
 98  
     /**
 99  
      * Construct a new IntDate from an another one.
 100  
      *
 101  
      * @param value The another IntDate.
 102  
      */
 103  
     public IntDate(IntDate value) {
 104  2
         super();
 105  
 
 106  2
         calendar = (Calendar) value.calendar.clone();
 107  2
     }
 108  
 
 109  
     /**
 110  
      * Return the day.
 111  
      *
 112  
      * @return The day.
 113  
      */
 114  
     public int getDay() {
 115  48
         return calendar.get(Calendar.DAY_OF_MONTH);
 116  
     }
 117  
 
 118  
     /**
 119  
      * Return the year.
 120  
      *
 121  
      * @return The year.
 122  
      */
 123  
     public int getYear() {
 124  70
         return calendar.get(Calendar.YEAR);
 125  
     }
 126  
 
 127  
     /**
 128  
      * Return the month.
 129  
      *
 130  
      * @return The month.
 131  
      */
 132  
     public int getMonth() {
 133  
         //We add 1 because calendar starts at 0 for the months
 134  52
         return calendar.get(Calendar.MONTH) + 1;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Add a value to specified field of the date.
 139  
      *
 140  
      * @param field The field to add the value to.
 141  
      * @param toAdd The value to add.
 142  
      * @throws IllegalArgumentException if the specified field is not valid.
 143  
      */
 144  
     public void add(int field, int toAdd) {
 145  8
         if (Fields.isValid(field)) {
 146  6
             calendar.add(field, toAdd);
 147  
         } else {
 148  2
             throw new IllegalArgumentException();
 149  
         }
 150  6
     }
 151  
 
 152  
     /**
 153  
      * Set the value of a specified field of the date.
 154  
      *
 155  
      * @param field The field to set the value to.
 156  
      * @param value The value to set to the field.
 157  
      */
 158  
     public void set(final int field, final int value) {
 159  8
         if (Fields.isValid(field)) {
 160  6
             if (field == Fields.MONTH) {
 161  2
                 calendar.set(Fields.MONTH, value - 1);
 162  
             } else {
 163  4
                 calendar.set(field, value);
 164  
             }
 165  
         } else {
 166  2
             throw new IllegalArgumentException();
 167  
         }
 168  6
     }
 169  
 
 170  
     /**
 171  
      * Return the <code>String</code> representation of the date.
 172  
      *
 173  
      * @return A <code>String</code> representing the date.
 174  
      */
 175  
     public String getStrDate() {
 176  14
         StringBuilder builder = new StringBuilder(DATE_STR_LENGTH);
 177  
 
 178  14
         builder.append(getYear());
 179  
 
 180  14
         if (getMonth() < 10) {
 181  14
             builder.append('0');
 182  
         }
 183  
 
 184  14
         builder.append(getMonth());
 185  
 
 186  14
         if (getDay() < 10) {
 187  14
             builder.append('0');
 188  
         }
 189  
 
 190  14
         builder.append(getDay());
 191  
 
 192  14
         return builder.toString();
 193  
     }
 194  
 
 195  
     /**
 196  
      * Return the IntDate of today.
 197  
      *
 198  
      * @return The today IntDate
 199  
      */
 200  
     public static IntDate today() {
 201  2
         return TODAY_DATE;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Return the int value of the date.
 206  
      *
 207  
      * @return A int representation of the date
 208  
      */
 209  
     public int intValue() {
 210  8
         return Integer.parseInt(getStrDate());
 211  
     }
 212  
 
 213  
     @Override
 214  
     public int compareTo(IntDate o) {
 215  16
         if (getYear() > o.getYear()) {
 216  8
             return 1;
 217  8
         } else if (getYear() < o.getYear()) {
 218  4
             return -1;
 219  
         }
 220  
 
 221  4
         if (getMonth() > o.getMonth()) {
 222  0
             return 1;
 223  4
         } else if (getMonth() < o.getMonth()) {
 224  0
             return -1;
 225  
         }
 226  
 
 227  4
         if (getDay() > o.getDay()) {
 228  2
             return 1;
 229  2
         } else if (getDay() < o.getDay()) {
 230  0
             return -1;
 231  
         }
 232  
 
 233  2
         return 0;
 234  
     }
 235  
 
 236  
     @Override
 237  
     public boolean equals(Object o) {
 238  14
         if (this == o) {
 239  4
             return true;
 240  
         }
 241  
 
 242  10
         if (o == null || getClass() != o.getClass()) {
 243  2
             return false;
 244  
         }
 245  
 
 246  8
         IntDate other = (IntDate) o;
 247  
 
 248  8
         return calendar.equals(other.calendar);
 249  
 
 250  
     }
 251  
 
 252  
     @Override
 253  
     public int hashCode() {
 254  8
         return calendar.hashCode();
 255  
     }
 256  
 
 257  
     @Override
 258  
     public String toString() {
 259  2
         return format.format(calendar.getTime());
 260  
     }
 261  
 }