| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Month |
|
| 1.85;1.85 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | package net.sourceforge.pebble.domain; | |
| 33 | ||
| 34 | import java.text.SimpleDateFormat; | |
| 35 | import java.util.*; | |
| 36 | ||
| 37 | /** | |
| 38 | * Represents a blog at a monthly level. This manages a collection of Day instances. | |
| 39 | * | |
| 40 | * @author Simon Brown | |
| 41 | */ | |
| 42 | public class Month extends TimePeriod implements Permalinkable { | |
| 43 | ||
| 44 | /** the parent, Year instance */ | |
| 45 | private Year year; | |
| 46 | ||
| 47 | /** an integer representing the month that this Month is for */ | |
| 48 | private int month; | |
| 49 | ||
| 50 | /** the collection of Day instances that this blog is managing */ | |
| 51 | private Day[] dailyBlogs; | |
| 52 | ||
| 53 | /** the last day in this month */ | |
| 54 | private int lastDayInMonth; | |
| 55 | ||
| 56 | /** | |
| 57 | * Creates a new Month based upon the specified Year and month. | |
| 58 | * | |
| 59 | * @param year the owning Year instance | |
| 60 | * @param month the month as an int | |
| 61 | */ | |
| 62 | Month(Year year, int month) { | |
| 63 | 9008 | super(year.getBlog()); |
| 64 | ||
| 65 | 9008 | this.year = year; |
| 66 | 9008 | this.month = month; |
| 67 | 9008 | setDate(getCalendar().getTime()); |
| 68 | ||
| 69 | 9008 | Calendar cal = getBlog().getCalendar(); |
| 70 | 9008 | cal.setTime(getDate()); |
| 71 | 9008 | this.lastDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 72 | ||
| 73 | // and create all days | |
| 74 | 9008 | dailyBlogs = new Day[lastDayInMonth]; |
| 75 | 283560 | for (int day = 1; day <= lastDayInMonth; day++) { |
| 76 | 274552 | dailyBlogs[day-1] = new Day(this, day); |
| 77 | } | |
| 78 | 9008 | } |
| 79 | ||
| 80 | private Calendar getCalendar() { | |
| 81 | // set the date corresponding to the 1st of the month | |
| 82 | // (this is used in determining whether another Month is | |
| 83 | // before or after this one) | |
| 84 | 9008 | Calendar cal = getBlog().getCalendar(); |
| 85 | 9008 | cal.set(Calendar.YEAR, year.getYear()); |
| 86 | 9008 | cal.set(Calendar.MONTH, month - 1); |
| 87 | 9008 | cal.set(Calendar.DAY_OF_MONTH, 1); |
| 88 | 9008 | cal.set(Calendar.HOUR_OF_DAY, 0); |
| 89 | 9008 | cal.set(Calendar.MINUTE, 0); |
| 90 | 9008 | cal.set(Calendar.SECOND, 0); |
| 91 | 9008 | cal.set(Calendar.MILLISECOND, 0); |
| 92 | ||
| 93 | 9008 | return cal; |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Gets a reference to the parent Year instance. | |
| 98 | * | |
| 99 | * @return a Year instance | |
| 100 | */ | |
| 101 | public Year getYear() { | |
| 102 | 274640 | return year; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Gets an integer representing the month that this monthly blog is for. | |
| 107 | * | |
| 108 | * @return an int representing the month (i.e. 1 to 12) | |
| 109 | */ | |
| 110 | public int getMonth() { | |
| 111 | 277760 | return month; |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Gets the permalink to display all entries for this Month. | |
| 116 | * | |
| 117 | * @return an absolute URL | |
| 118 | */ | |
| 119 | public String getPermalink() { | |
| 120 | 8 | String s = getBlog().getPermalinkProvider().getPermalink(this); |
| 121 | 8 | if (s != null && s.length() > 0) { |
| 122 | 8 | return getBlog().getUrl() + s.substring(1); |
| 123 | } else { | |
| 124 | 0 | return ""; |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * Determines whether this monthly blog has entries. | |
| 130 | * | |
| 131 | * @return true if this blog contains entries, false otherwise | |
| 132 | */ | |
| 133 | public boolean hasBlogEntries() { | |
| 134 | 0 | for (int i = 1; i <= lastDayInMonth; i++) { |
| 135 | 0 | if (getBlogForDay(i).hasBlogEntries()) { |
| 136 | 0 | return true; |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | 0 | return false; |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Gets all blog entries for this month. | |
| 145 | * | |
| 146 | * @return a List of BlogEntry instances, reverse ordered by date | |
| 147 | */ | |
| 148 | public List<String> getBlogEntries() { | |
| 149 | 3076 | Day days[] = getAllDays(); |
| 150 | 3076 | List blogEntries = new ArrayList(); |
| 151 | 96844 | for (Day day : days) { |
| 152 | 93768 | blogEntries.addAll(day.getBlogEntries()); |
| 153 | } | |
| 154 | 3076 | return blogEntries; |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Gets the number of blog entries for this month. | |
| 159 | * | |
| 160 | * @return an int | |
| 161 | */ | |
| 162 | public int getNumberOfBlogEntries() { | |
| 163 | 0 | int count = 0; |
| 164 | 0 | Day days[] = getAllDays(); |
| 165 | 0 | for (Day day : days) { |
| 166 | 0 | count += day.getNumberOfBlogEntries(); |
| 167 | } | |
| 168 | ||
| 169 | 0 | return count; |
| 170 | } | |
| 171 | ||
| 172 | /** | |
| 173 | * Gets an array of all Days. | |
| 174 | * | |
| 175 | * @return a Collection of Day instances for all those days | |
| 176 | * that have entries (this can return an empty collection) | |
| 177 | */ | |
| 178 | public Day[] getAllDays() { | |
| 179 | 3080 | Day blogs[] = new Day[dailyBlogs.length]; |
| 180 | 96972 | for (int day = 0; day < dailyBlogs.length; day++) { |
| 181 | 93892 | blogs[day] = getBlogForDay(day + 1); |
| 182 | } | |
| 183 | ||
| 184 | 3080 | return blogs; |
| 185 | } | |
| 186 | ||
| 187 | /** | |
| 188 | * Gets a Day instance for the specified day. This lazy loads Day | |
| 189 | * instances as needed. | |
| 190 | * | |
| 191 | * @param day the day as an int (i.e. 1 to 31) | |
| 192 | * @return the corresponding Day instance | |
| 193 | */ | |
| 194 | public synchronized Day getBlogForDay(int day) { | |
| 195 | // some bounds checking | |
| 196 | 95106 | if (day < 1 || day > lastDayInMonth) { |
| 197 | 12 | throw new IllegalArgumentException("Invalid day of " + day + " specified, should be between 1 and " + lastDayInMonth); |
| 198 | } | |
| 199 | ||
| 200 | 95094 | return dailyBlogs[day-1]; |
| 201 | } | |
| 202 | ||
| 203 | /** | |
| 204 | * Gets a Day instance for the first day of the month. | |
| 205 | * | |
| 206 | * @return the Day instance representing the first day in the month | |
| 207 | */ | |
| 208 | public Day getBlogForFirstDay() { | |
| 209 | 12 | return getBlogForDay(1); |
| 210 | } | |
| 211 | ||
| 212 | /** | |
| 213 | * Gets a Day instance for the last day of the month. | |
| 214 | * | |
| 215 | * @return the Day instance representing the last day in the month | |
| 216 | */ | |
| 217 | public Day getBlogForLastDay() { | |
| 218 | 0 | return getBlogForDay(lastDayInMonth); |
| 219 | } | |
| 220 | ||
| 221 | /** | |
| 222 | * Gets the last day of the month. | |
| 223 | * | |
| 224 | * @return an int representing the last day in the month | |
| 225 | */ | |
| 226 | public int getLastDayInMonth() { | |
| 227 | 16 | return lastDayInMonth; |
| 228 | } | |
| 229 | ||
| 230 | /** | |
| 231 | * Gets the Month instance for the previous month. | |
| 232 | * | |
| 233 | * @return a Month instance | |
| 234 | */ | |
| 235 | public Month getPreviousMonth() { | |
| 236 | 12 | return year.getBlogForPreviousMonth(this); |
| 237 | } | |
| 238 | ||
| 239 | /** | |
| 240 | * Gets the Month instance for the next month. | |
| 241 | * | |
| 242 | * @return a Month instance | |
| 243 | */ | |
| 244 | public Month getNextMonth() { | |
| 245 | 12 | return year.getBlogForNextMonth(this); |
| 246 | } | |
| 247 | ||
| 248 | /** | |
| 249 | * Determines if the this Month is before (in the calendar) the | |
| 250 | * specified Month. | |
| 251 | * | |
| 252 | * @return true if this instance represents an earlier month than the | |
| 253 | * specified Month instance, false otherwise | |
| 254 | */ | |
| 255 | public boolean before(Month month) { | |
| 256 | 20 | return getDate().before(month.getDate()); |
| 257 | } | |
| 258 | ||
| 259 | /** | |
| 260 | * Determines if the this Month is after (in the calendar) the | |
| 261 | * specified Month. | |
| 262 | * | |
| 263 | * @return true if this instance represents a later month than the | |
| 264 | * specified Month instance, false otherwise | |
| 265 | */ | |
| 266 | public boolean after(Month month) { | |
| 267 | 0 | return getDate().after(month.getDate()); |
| 268 | } | |
| 269 | ||
| 270 | /** | |
| 271 | * Given a Day, this method returns the Day instance for the | |
| 272 | * previous day. | |
| 273 | * | |
| 274 | * @param day a Day instance | |
| 275 | * @return a Day instance representing the previous day | |
| 276 | */ | |
| 277 | Day getBlogForPreviousDay(Day day) { | |
| 278 | 90 | if (day.getDay() > 1) { |
| 279 | 90 | return this.getBlogForDay(day.getDay() - 1); |
| 280 | } else { | |
| 281 | 0 | return year.getBlogForPreviousMonth(this).getBlogForLastDay(); |
| 282 | } | |
| 283 | } | |
| 284 | ||
| 285 | /** | |
| 286 | * Given a Day, this method returns the Day instance for the | |
| 287 | * next day. | |
| 288 | * | |
| 289 | * @param day a Day instance | |
| 290 | * @return a Day instance representing the next day | |
| 291 | */ | |
| 292 | Day getBlogForNextDay(Day day) { | |
| 293 | 8 | if (day.getDay() < lastDayInMonth) { |
| 294 | 8 | return this.getBlogForDay(day.getDay() + 1); |
| 295 | } else { | |
| 296 | 0 | return year.getBlogForNextMonth(this).getBlogForFirstDay(); |
| 297 | } | |
| 298 | } | |
| 299 | ||
| 300 | /** | |
| 301 | * Gets a string representation of this object. | |
| 302 | * | |
| 303 | * @return a String | |
| 304 | */ | |
| 305 | public String toString() { | |
| 306 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("MMMM"); |
| 307 | 0 | return sdf.format(getDate()); |
| 308 | } | |
| 309 | ||
| 310 | } |