File Source: year.java
/*
P/P * Method: net.sourceforge.pebble.domain.Year__static_init
*/
1 /*
2 * Copyright (c) 2003-2006, 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) {
/*
P/P * Method: void net.sourceforge.pebble.domain.Year(Blog, int)
*
* Preconditions:
* blog != null
*
* Postconditions:
* this.blog != null
* init'ed(this.date)
* this.months == &new Month[](init#1)
* this.year == year
* init'ed(this.year)
* init'ed(new ArrayList(Day#1) num objects)
* init'ed(new ArrayList(Day#2) num objects)
* init'ed(new ArrayList(Day#3) num objects)
* init'ed(new Day(Month#2) num objects)
* init'ed(new Day(Month#2).blog)
* ...
*/
58 super(blog);
59
60 this.year = year;
61 init();
62 }
63
64 /**
65 * Initialises internal data, such as the collection of Month instances.
66 */
67 private void init() {
/*
P/P * Method: void init()
*
* Preconditions:
* this.blog != null
* init'ed(this.year)
*
* Postconditions:
* init'ed(this.date)
* this.months == &new Month[](init#1)
* init'ed(this.months[...])
* init'ed(new ArrayList(Day#1) num objects)
* init'ed(new ArrayList(Day#2) num objects)
* init'ed(new ArrayList(Day#3) num objects)
* init'ed(new Day(Month#2) num objects)
* init'ed(new Day(Month#2).blog)
* init'ed(new Day(Month#2).blogEntries)
* init'ed(new Day(Month#2).date)
* ...
*/
68 setDate(getCalendar().getTime());
69 this.months = new Month[12];
70
71 for (int i = 1; i <= 12; i++) {
72 months[i-1] = new Month(this, i);
73 }
74 }
75
76 private Calendar getCalendar() {
77 // set the date corresponding to the 1st of January of the specified year
/*
P/P * Method: Calendar getCalendar()
*
* Preconditions:
* this.blog != null
* init'ed(this.year)
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getCalendar(...)@78 != null
*
* Postconditions:
* (soft) return_value != null
*/
78 Calendar cal = getBlog().getCalendar();
79 cal.set(Calendar.YEAR, year);
80 cal.set(Calendar.MONTH, 0);
81 cal.set(Calendar.DAY_OF_MONTH, 2);
82 cal.set(Calendar.HOUR_OF_DAY, 0);
83 cal.set(Calendar.MINUTE, 0);
84 cal.set(Calendar.SECOND, 0);
85 cal.set(Calendar.MILLISECOND, 0);
86
87 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() {
/*
P/P * Method: int getYear()
*
* Preconditions:
* init'ed(this.year)
*
* Postconditions:
* return_value == this.year
* init'ed(return_value)
*/
96 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
/*
P/P * Method: Month getBlogForMonth(int)
*
* Preconditions:
* month in 1..12
* this.months != null
* this.months.length >= 1
* month <= this.months.length
* (soft) init'ed(this.months[...])
*
* Postconditions:
* return_value == this.months[...]
* init'ed(return_value)
*/
109 if (month < 1 || month > 12) {
110 throw new IllegalArgumentException("Invalid month of " + month + " specified, should be between 1 and 12");
111 }
112
113 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) {
/*
P/P * Method: Month getBlogForPreviousMonth(Month)
*
* Preconditions:
* month != null
* month.month <= 13
* (soft) month.month <= this.months.length + 1
* (soft) this.blog != null
* (soft) this.months != null
* (soft) init'ed(this.months[...])
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* month.month: {-231..1}, {2..13}
*
* Preconditions:
* (soft) this.blog.years != null
* (soft) this.year >= -231+1
*
* Presumptions:
* getBlogForPreviousYear(...).months.length@127 >= 12
* getBlogForPreviousYear(...).months@127 != null
*
* Postconditions:
* init'ed(new ArrayList(Day#1) num objects)
* init'ed(new ArrayList(Day#2) num objects)
* init'ed(new ArrayList(Day#3) num objects)
* init'ed(new Day(Month#2) num objects)
* init'ed(new Day(Month#2).blog)
* init'ed(new Day(Month#2).blogEntries)
* init'ed(new Day(Month#2).date)
* init'ed(new Day(Month#2).day)
* init'ed(new Day(Month#2).month)
* init'ed(new Day(Month#2).publishedBlogEntries)
* ...
*/
124 if (month.getMonth() > 1) {
125 return this.getBlogForMonth(month.getMonth() - 1);
126 } else {
127 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) {
/*
P/P * Method: Month getBlogForNextMonth(Month)
*
* Preconditions:
* month != null
* month.month >= 0
* (soft) month.month - this.months.length in {-Inf..-1, 12..232-1}
* (soft) this.blog != null
* (soft) this.months != null
* (soft) init'ed(this.months[...])
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* month.month: {12..232-1}, {0..11}
*
* Preconditions:
* (soft) this.blog.years != null
* (soft) this.year <= 232-2
*
* Presumptions:
* getBlogForNextYear(...).months.length@142 >= 1
* getBlogForNextYear(...).months@142 != null
*
* Postconditions:
* init'ed(new ArrayList(Day#1) num objects)
* init'ed(new ArrayList(Day#2) num objects)
* init'ed(new ArrayList(Day#3) num objects)
* init'ed(new Day(Month#2) num objects)
* init'ed(new Day(Month#2).blog)
* init'ed(new Day(Month#2).blogEntries)
* init'ed(new Day(Month#2).date)
* init'ed(new Day(Month#2).day)
* init'ed(new Day(Month#2).month)
* init'ed(new Day(Month#2).publishedBlogEntries)
* ...
*/
139 if (month.getMonth() < 12) {
140 return this.getBlogForMonth(month.getMonth() + 1);
141 } else {
142 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() {
/*
P/P * Method: Month getBlogForFirstMonth()
*
* Preconditions:
* this.months != null
* this.months.length >= 1
* (soft) init'ed(this.months[...])
*
* Postconditions:
* return_value == this.months[...]
* init'ed(return_value)
*/
152 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() {
/*
P/P * Method: Month[] getMonths()
*
* Preconditions:
* (soft) this.months != null
* (soft) this.months.length >= 1
* (soft) init'ed(this.months[...])
*
* Postconditions:
* return_value == &new Month[](getMonths#1)
* new Month[](getMonths#1) num objects == 1
* return_value.length == 12
* return_value[...] == null
*/
161 Month[] months = new Month[12];
162 for (int i = 1; i <= 12; i++) {
163 months[i-1] = getBlogForMonth(i);
164 }
165
166 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() {
/*
P/P * Method: List getArchives()
*
* Preconditions:
* this.blog != null
* (soft) this.months != null
* (soft) this.months.length >= 1
* (soft) this.months[...] != null
* (soft) this.months[...].date != null
*
* Postconditions:
* return_value == &new LinkedList(getArchives#1)
* new LinkedList(getArchives#1) num objects == 1
*
* Test Vectors:
* java.util.Date:after(...)@267: {1}, {0}
* java.util.Date:before(...)@256: {1}, {0}
*
* Preconditions:
* init'ed(this.blog.blogEntryIndex)
* this.blog.properties != null
* this.blog.years != null
* (soft) init'ed(this.blog.blogEntryIndex.indexEntries)
*/
175 List<Month> list = new LinkedList<Month>();
176 Month thisMonth = getBlog().getBlogForThisMonth();
177 Month firstMonth = getBlog().getBlogForFirstMonth();
178 for (int i = 12; i >=1; i--) {
179 Month month = getBlogForMonth(i);
+ 180 if (!month.after(thisMonth) && !month.before(firstMonth)) {
181 list.add(month);
182 }
183 }
184
185 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) {
/*
P/P * Method: int compareTo(Object)
*
* Preconditions:
* o != null
* init'ed(o.year)
* o.year - this.year in -232+1..231
* init'ed(this.year)
*
* Postconditions:
* return_value == -(o.year - this.year)
* init'ed(return_value)
*/
200 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() {
/*
P/P * Method: String toString()
*
* Preconditions:
* init'ed(this.year)
*
* Postconditions:
* return_value != null
*/
209 return "" + this.year;
210 }
211
212 }
SofCheck Inspector Build Version : 2.22510
| year.java |
2010-Jun-25 19:40:32 |
| year.class |
2010-Jul-19 20:23:40 |