Чистый код. Создание, анализ и рефакторинг (Мартин) - страница 308

>  83   public static final int LATEST_DATE_ORDINAL = 2958465; // 12/31/9999

>  84   public static final int MINIMUM_YEAR_SUPPORTED = 1900;

>  85   public static final int MAXIMUM_YEAR_SUPPORTED = 9999;

>  86   static final int[] AGGREGATE_DAYS_TO_END_OF_PRECEDING_MONTH =

>  87     {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

>  88   static final int[] LEAP_YEAR_AGGREGATE_DAYS_TO_END_OF_PRECEDING_MONTH =

>  89     {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

>  90

>  91   private int ordinalDay;

>  92   private int day;

>  93   private Month month;

>  94   private int year;


Листинг Б.16 (продолжение)

>  95

>  96   public SpreadsheetDate(int day, Month month, int year) {

>  97     if (year < MINIMUM_YEAR_SUPPORTED || year > MAXIMUM_YEAR_SUPPORTED)

>  98       throw new IllegalArgumentException(

>  99         "The 'year' argument must be in range " +

>100         MINIMUM_YEAR_SUPPORTED + " to " + MAXIMUM_YEAR_SUPPORTED + ".");

>101     if (day < 1 || day > DateUtil.lastDayOfMonth(month, year))

>102       throw new IllegalArgumentException("Invalid 'day' argument.");

>103

>104     this.year = year;

>105     this.month = month;

>106     this.day = day;

>107     ordinalDay = calcOrdinal(day, month, year);

>108   }

>109

>110   public SpreadsheetDate(int day, int month, int year) {

>111     this(day, Month.fromInt(month), year);

>112   }

>113

>114   public SpreadsheetDate(int serial) {

>115     if (serial < EARLIEST_DATE_ORDINAL || serial > LATEST_DATE_ORDINAL)

>116       throw new IllegalArgumentException(

>117         "SpreadsheetDate: Serial must be in range 2 to 2958465.");

>118

>119     ordinalDay = serial;

>120     calcDayMonthYear();

>121   }

>122

>123   public int getOrdinalDay() {

>124     return ordinalDay;

>125   }

>126

>127   public int getYear() {

>128     return year;

>129   }

>130

>131   public Month getMonth() {

>132     return month;

>133   }

>134

>135   public int getDayOfMonth() {

>136     return day;

>137   }

>138

>139   protected Day getDayOfWeekForOrdinalZero() {return Day.SATURDAY;}

>140

>141   public boolean equals(Object object) {

>142     if (!(object instanceof DayDate))

>143       return false;

>144

>145     DayDate date = (DayDate) object;

>146     return date.getOrdinalDay() == getOrdinalDay();

>147   }

>148

>149   public int hashCode() {

>150     return getOrdinalDay();

>151   }

>152

>153   public int compareTo(Object other) {

>154     return daysSince((DayDate) other);

>155   }

>156

>157   private int calcOrdinal(int day, Month month, int year) {

>158     int leapDaysForYear = DateUtil.leapYearCount(year - 1);

>159     int daysUpToYear = (year - MINIMUM_YEAR_SUPPORTED) * 365 + leapDaysForYear;