| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Year |
|
| 2.0;2 |
| 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.util.Calendar; | |
| 35 | import java.util.List; | |
| 36 | import java.util.LinkedList; | |
| 37 | ||
| 38 | /** | |
| 39 | * Represents a blog at a yearly level. This manages a collection of Month instances. | |
| 40 | * | |
| 41 | * @author Simon Brown | |
| 42 | */ | |
| 43 | public class Year extends TimePeriod implements Comparable { | |
| 44 | ||
| 45 | /** the year that this blog is for */ | |
| 46 | private int year; | |
| 47 | ||
| 48 | /** a collection of the monthly blogs that this instance is managing */ | |
| 49 | private Month[] months; | |
| 50 | ||
| 51 | /** | |
| 52 | * Creates a new Year instance for the specified year. | |
| 53 | * | |
| 54 | * @param blog the Blog on which this Year is based | |
| 55 | * @param year the year that this Year is for | |
| 56 | */ | |
| 57 | public Year(Blog blog, int year) { | |
| 58 | 744 | super(blog); |
| 59 | ||
| 60 | 744 | this.year = year; |
| 61 | 744 | init(); |
| 62 | 744 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Initialises internal data, such as the collection of Month instances. | |
| 66 | */ | |
| 67 | private void init() { | |
| 68 | 744 | setDate(getCalendar().getTime()); |
| 69 | 744 | this.months = new Month[12]; |
| 70 | ||
| 71 | 9672 | for (int i = 1; i <= 12; i++) { |
| 72 | 8928 | months[i-1] = new Month(this, i); |
| 73 | } | |
| 74 | 744 | } |
| 75 | ||
| 76 | private Calendar getCalendar() { | |
| 77 | // set the date corresponding to the 1st of January of the specified year | |
| 78 | 744 | Calendar cal = getBlog().getCalendar(); |
| 79 | 744 | cal.set(Calendar.YEAR, year); |
| 80 | 744 | cal.set(Calendar.MONTH, 0); |
| 81 | 744 | cal.set(Calendar.DAY_OF_MONTH, 2); |
| 82 | 744 | cal.set(Calendar.HOUR_OF_DAY, 0); |
| 83 | 744 | cal.set(Calendar.MINUTE, 0); |
| 84 | 744 | cal.set(Calendar.SECOND, 0); |
| 85 | 744 | cal.set(Calendar.MILLISECOND, 0); |
| 86 | ||
| 87 | 744 | return cal; |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Gets an integer representing the year that this yearly blog is for. | |
| 92 | * | |
| 93 | * @return an int representing the year (e.g. 2003) | |
| 94 | */ | |
| 95 | public int getYear() { | |
| 96 | 290852 | return year; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Gets the Month for the specified month. Months are lazy | |
| 101 | * loaded as needed. | |
| 102 | * | |
| 103 | * @param month the month as an int | |
| 104 | * @return a Month instance | |
| 105 | */ | |
| 106 | public synchronized Month getBlogForMonth(int month) { | |
| 107 | ||
| 108 | // some bounds checking | |
| 109 | 7380 | if (month < 1 || month > 12) { |
| 110 | 12 | throw new IllegalArgumentException("Invalid month of " + month + " specified, should be between 1 and 12"); |
| 111 | } | |
| 112 | ||
| 113 | 7368 | return months[month-1]; |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Given a Month, this method returns the Month instance for the | |
| 118 | * previous month. | |
| 119 | * | |
| 120 | * @param month a Month instance | |
| 121 | * @return a Month instance representing the previous month | |
| 122 | */ | |
| 123 | Month getBlogForPreviousMonth(Month month) { | |
| 124 | 12 | if (month.getMonth() > 1) { |
| 125 | 8 | return this.getBlogForMonth(month.getMonth() - 1); |
| 126 | } else { | |
| 127 | 4 | return getBlog().getBlogForPreviousYear(this).getBlogForMonth(12); |
| 128 | } | |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Given a Month, this method returns the Month instance for the | |
| 133 | * next month. | |
| 134 | * | |
| 135 | * @param month a Month instance | |
| 136 | * @return a Month instance representing the next month | |
| 137 | */ | |
| 138 | Month getBlogForNextMonth(Month month) { | |
| 139 | 12 | if (month.getMonth() < 12) { |
| 140 | 8 | return this.getBlogForMonth(month.getMonth() + 1); |
| 141 | } else { | |
| 142 | 4 | return getBlog().getBlogForNextYear(this).getBlogForMonth(1); |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Gets the first Month that actually contains blog entries. | |
| 148 | * | |
| 149 | * @return a Month instance | |
| 150 | */ | |
| 151 | public Month getBlogForFirstMonth() { | |
| 152 | 4 | return getBlogForMonth(1); |
| 153 | } | |
| 154 | ||
| 155 | /** | |
| 156 | * Gets a collection of all Months managed by this blog. | |
| 157 | * | |
| 158 | * @return a Collection of Month instances | |
| 159 | */ | |
| 160 | public Month[] getMonths() { | |
| 161 | 256 | Month[] months = new Month[12]; |
| 162 | 3328 | for (int i = 1; i <= 12; i++) { |
| 163 | 3072 | months[i-1] = getBlogForMonth(i); |
| 164 | } | |
| 165 | ||
| 166 | 256 | return months; |
| 167 | } | |
| 168 | ||
| 169 | /** | |
| 170 | * Gets a collection of all Months, to date and in reverse order. | |
| 171 | * | |
| 172 | * @return a Collection of Month instances | |
| 173 | */ | |
| 174 | public List<Month> getArchives() { | |
| 175 | 0 | List<Month> list = new LinkedList<Month>(); |
| 176 | 0 | Month thisMonth = getBlog().getBlogForThisMonth(); |
| 177 | 0 | Month firstMonth = getBlog().getBlogForFirstMonth(); |
| 178 | 0 | for (int i = 12; i >=1; i--) { |
| 179 | 0 | Month month = getBlogForMonth(i); |
| 180 | 0 | if (!month.after(thisMonth) && !month.before(firstMonth)) { |
| 181 | 0 | list.add(month); |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 185 | 0 | return list; |
| 186 | } | |
| 187 | ||
| 188 | /** | |
| 189 | * Compares this object with the specified object for order. Returns a | |
| 190 | * negative integer, zero, or a positive integer as this object is less | |
| 191 | * than, equal to, or greater than the specified object.<p> | |
| 192 | * | |
| 193 | * @param o the Object to be compared. | |
| 194 | * @return a negative integer, zero, or a positive integer as this object | |
| 195 | * is less than, equal to, or greater than the specified object. | |
| 196 | * @throws ClassCastException if the specified object's type prevents it | |
| 197 | * from being compared to this Object. | |
| 198 | */ | |
| 199 | public int compareTo(Object o) { | |
| 200 | 108 | return this.getYear() - ((Year)o).getYear(); |
| 201 | } | |
| 202 | ||
| 203 | /** | |
| 204 | * Gets a string representation of this object. | |
| 205 | * | |
| 206 | * @return a String | |
| 207 | */ | |
| 208 | public String toString() { | |
| 209 | 4 | return "" + this.year; |
| 210 | } | |
| 211 | ||
| 212 | } |