Clover coverage report - brownies library - 1.0-beta-1
Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00
file stats: LOC: 285   Methods: 19
NCLOC: 157   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
SimpleDate.java 39.6% 38.7% 47.4% 40%
coverage coverage
 1   
 package org.asyrinx.brownie.core.util;
 2   
 
 3   
 import java.util.Calendar;
 4   
 import java.util.Date;
 5   
 
 6   
 /**
 7   
  * 日付のみを表すクラスです。 monthは必ず1から始まり12月で終わります。0月はありません。
 8   
  * 
 9   
  * @author akima
 10   
  */
 11   
 public final class SimpleDate {
 12   
 
 13   
     private final int year;
 14   
 
 15   
     private final int month;
 16   
 
 17   
     private final int day;
 18   
 
 19   
     /**
 20   
      *  
 21   
      */
 22  104
     public SimpleDate(int year, int month, int day) {
 23  104
         super();
 24  104
         this.year = year;
 25  104
         this.month = month;
 26  104
         this.day = day;
 27   
     }
 28   
 
 29   
     /**
 30   
      *  
 31   
      */
 32  16
     public SimpleDate(Date d) {
 33  16
         this(d, Calendar.getInstance());
 34   
     }
 35   
 
 36   
     /**
 37   
      *  
 38   
      */
 39  16
     public SimpleDate(Date d, Calendar calendar) {
 40  16
         super();
 41  16
         calendar.setTime(d);
 42  16
         this.year = calendar.get(Calendar.YEAR);
 43  16
         this.month = calendar.get(Calendar.MONTH) + 1;
 44  16
         this.day = calendar.get(Calendar.DAY_OF_MONTH);
 45   
     }
 46   
 
 47   
     /**
 48   
      * @return
 49   
      */
 50  0
     public Date toDate() {
 51  0
         return toDateBegin();
 52   
     }
 53   
 
 54   
     /**
 55   
      * @return
 56   
      */
 57  5
     public Date toDateBegin() {
 58  5
         return DateUtils.toDate(year, month, day, 0, 0, 0, 0);
 59   
     }
 60   
 
 61   
     /**
 62   
      * @return
 63   
      */
 64  1
     public Date toDateEnd() {
 65  1
         return DateUtils.toDate(year, month, day, 23, 59, 59, 999);
 66   
     }
 67   
 
 68   
     /**
 69   
      * @param obj
 70   
      * @return @see java.lang.Object#equals(java.lang.Object)
 71   
      */
 72  61
     public boolean equals(Object obj) {
 73  61
         if (obj instanceof SimpleDate) {
 74  56
             final SimpleDate date = (SimpleDate) obj;
 75  56
             return (this.year == date.year) && (this.month == date.month)
 76   
                     && (this.day == date.day);
 77  5
         } else if (obj instanceof Date) {
 78  5
             return this.equals(new SimpleDate((Date) obj));
 79   
         } else {
 80  0
             return super.equals(obj);
 81   
         }
 82   
     }
 83   
 
 84  60
     public boolean after(SimpleDate when) {
 85  60
         if (when.year < this.year) {
 86  16
             return true;
 87  44
         } else if (when.year > this.year) {
 88  14
             return false;
 89   
         } else {
 90  30
             if (when.month < this.month) {
 91  13
                 return true;
 92  17
             } else if (when.month > this.month) {
 93  8
                 return false;
 94   
             } else {
 95  9
                 return when.day < this.day;
 96   
             }
 97   
         }
 98   
     }
 99   
 
 100  74
     public boolean before(SimpleDate when) {
 101  74
         if (when.year > this.year) {
 102  21
             return true;
 103  53
         } else if (when.year < this.year) {
 104  20
             return false;
 105   
         } else {
 106  33
             if (when.month > this.month) {
 107  14
                 return true;
 108  19
             } else if (when.month < this.month) {
 109  9
                 return false;
 110   
             } else {
 111  10
                 return when.day > this.day;
 112   
             }
 113   
         }
 114   
     }
 115   
 
 116   
     /**
 117   
      * 日単位での差を計算します。 <br>
 118   
      * 2004/3/11 - 2004/3/11 = 0 <br>
 119   
      * 2004/3/11 - 2004/3/1 = 10 <br>
 120   
      * 2004/3/1 - 2004/3/11 = -10 <br>
 121   
      * 
 122   
      * @param when
 123   
      * @return when - this
 124   
      */
 125  0
     public int getDayGap(SimpleDate when) {
 126  0
         int result = 0;
 127  0
         if (when.getYear() > this.getYear()) {
 128   
             //年末日までの距離
 129  0
             result = getDayGap(new SimpleDate(this.getYear(), 12, 31));
 130   
             //年末尾から次の元旦までの距離
 131  0
             result++;
 132   
             //各年の元旦から次の年の元旦までの距離
 133  0
             for (int y = this.getYear() + 1; y < when.getYear(); y++)
 134  0
                 result += (DateUtils.isLeapYear(y) ? 366 : 365);
 135   
             //whenの年の元旦からwhenまでの距離
 136  0
             result += new SimpleDate(when.getYear(), 1, 1).getDayGap(when);
 137  0
             return result;
 138  0
         } else if (when.getYear() < this.getYear()) {
 139   
             //元旦までの距離
 140  0
             result = getDayGap(new SimpleDate(this.getYear(), 1, 1));
 141   
             //各年の元旦からその前の年の元旦までの距離
 142  0
             for (int y = this.getYear() - 1; y > when.getYear(); y--)
 143  0
                 result -= (DateUtils.isLeapYear(y) ? 366 : 365);
 144   
             //元旦から大晦日までの距離
 145  0
             result--;
 146   
             //whenの年の大晦日からwhenまでの距離
 147  0
             result += new SimpleDate(when.getYear(), 12, 31).getDayGap(when);
 148  0
             return result;
 149   
         } else {
 150  0
             if (when.getMonth() > this.getMonth()) {
 151   
                 //月末日までの距離
 152  0
                 result += (DateUtils.getLastDayOfMonth(this.getYear(), this
 153   
                         .getMonth()) - this.getDay());
 154   
                 //月末日から次の月の1日までの距離
 155  0
                 result++;
 156   
                 //各月の1日から次の月の1日まで距離
 157  0
                 for (int m = this.getMonth() + 1; m < when.getMonth(); m++)
 158  0
                     result += DateUtils.getLastDayOfMonth(this.getYear(), m);
 159   
                 //whenの月の1日からwhenの日まで距離
 160  0
                 result += (when.getDay() - 1);
 161  0
                 return result;
 162  0
             } else if (when.month < this.month) {
 163   
                 //先月末日までの距離
 164  0
                 result -= this.getDay();
 165   
                 //各月の末日から前の月の末日までの距離
 166  0
                 for (int m = this.getMonth() - 1; m > when.getMonth(); m--)
 167  0
                     result -= DateUtils.getLastDayOfMonth(this.getYear(), m);
 168   
                 //whenの月の末日からwhenの日までの距離
 169  0
                 result -= (DateUtils.getLastDayOfMonth(when.getYear(), when
 170   
                         .getMonth()) - when.getDay());
 171  0
                 return result;
 172   
             } else {
 173  0
                 return when.day - this.day;
 174   
             }
 175   
         }
 176   
     }
 177   
 
 178   
     /**
 179   
      * 月単位での差を計算します。 <br>
 180   
      * 2004/3/11 - 2004/3/11 = 0 <br>
 181   
      * 2004/3/11 - 2004/3/1 = 0 <br>
 182   
      * 2004/3/1 - 2004/3/31 = 0 <br>
 183   
      * 2004/3/11 - 2004/2/1 = 1 <br>
 184   
      * 2004/2/1 - 2004/3/11 = -1<br>
 185   
      * 2004/2/29 - 2004/3/1 = -1<br>
 186   
      * 2004/4/1 - 2004/3/31 = 1 <br>
 187   
      * 2004/6/1 - 2004/3/31 = 3 <br>
 188   
      * 
 189   
      * @param when
 190   
      * @return when - this
 191   
      */
 192  0
     public int getMonthGap(SimpleDate when) {
 193  0
         int result = 0;
 194  0
         if (when.getYear() > this.getYear()) {
 195   
             //12月までの距離
 196  0
             result = 12 - this.getMonth();
 197   
             //各年の12月から次の年の12月までの距離
 198  0
             for (int y = this.getYear() + 1; y < when.getYear(); y++)
 199  0
                 result += 12;
 200   
             //whenの年の前の12月からwhenまでの距離
 201  0
             result += when.getMonth();
 202  0
             return result;
 203  0
         } else if (when.getYear() < this.getYear()) {
 204   
             //thisの年の前の12月までの距離
 205  0
             result -= this.getMonth();
 206   
             //各年の12月からその前の年の12月までの距離
 207  0
             for (int y = this.getYear() + 1; y < when.getYear(); y++)
 208  0
                 result -= 12;
 209   
             //whenの年の12月からwhenまでの距離
 210  0
             result -= (12 - when.getMonth());
 211  0
             return result;
 212   
         } else {
 213  0
             return when.getMonth() - this.getMonth();
 214   
         }
 215   
     }
 216   
 
 217   
     /**
 218   
      * 年単位での差を計算します。 <br>
 219   
      * 2004/3/11 - 2004/3/11 = 0 <br>
 220   
      * 2004/1/1 - 2004/12/31 = 0 <br>
 221   
      * 2005/3/11 - 2004/2/1 = 1 <br>
 222   
      * 2003/2/1 - 2004/3/11 = -1<br>
 223   
      * 2004/12/31 - 2003/1/1 = 1 <br>
 224   
      * 2004/1/1 - 2003/12/31 = 1 <br>
 225   
      * 2005/1/1 - 2003/12/1 = 2 <br>
 226   
      * 2003/12/31 - 2005/1/1 = -2<br>
 227   
      * 
 228   
      * @param when
 229   
      * @return when - this
 230   
      */
 231  0
     public int getYearGap(SimpleDate when) {
 232  0
         return when.getYear() - this.getYear();
 233   
     }
 234   
 
 235   
     /**
 236   
      * 曜日を返します。
 237   
      * 
 238   
      * @return Calendarクラスで定義されているSUNDAYからSATURDAYまでのどれか。
 239   
      */
 240  0
     public int getDayOfWeek() {
 241  0
         Calendar calendar = Calendar.getInstance();
 242  0
         calendar.setTime(this.toDate());
 243  0
         return calendar.get(Calendar.DAY_OF_WEEK);
 244   
     }
 245   
 
 246   
     /**
 247   
      * @return @see java.lang.Object#toString()
 248   
      */
 249  0
     public String toString() {
 250  0
         return year + "/" + month + "/" + day;
 251   
     }
 252   
 
 253   
     /**
 254   
      * @return
 255   
      */
 256  0
     public int getDay() {
 257  0
         return day;
 258   
     }
 259   
 
 260   
     /**
 261   
      * @return
 262   
      */
 263  0
     public int getMonth() {
 264  0
         return month;
 265   
     }
 266   
 
 267   
     /**
 268   
      * @return
 269   
      */
 270  29
     public int getYear() {
 271  29
         return year;
 272   
     }
 273   
 
 274  0
     public SimpleDate prev(int length, int unit) {
 275  0
         return next(-length, unit);
 276   
     }
 277   
 
 278  0
     public SimpleDate next(int length, int unit) {
 279  0
         final Calendar calendar = Calendar.getInstance();
 280  0
         calendar.setTime(this.toDate());
 281  0
         calendar.add(unit, length);
 282  0
         return new SimpleDate(calendar.getTime());
 283   
     }
 284   
 
 285   
 }