File Source: categorybuilder.java
/*
P/P * Method: net.sourceforge.pebble.domain.CategoryBuilder__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.ArrayList;
35 import java.util.Collections;
36 import java.util.Iterator;
37 import java.util.List;
38
39 import net.sourceforge.pebble.util.I18n;
40
41 /**
42 * A class to manage blog categories.
43 *
44 * @author Simon Brown
45 */
46 public class CategoryBuilder {
47
48 /** the category separator */
49 private static final String CATEGORY_SEPARATOR = "/";
50
51 /** the owning blog */
52 private Blog blog;
53
54 /** the root category */
55 private Category rootCategory;
56
57 /**
58 * Creates a new instance.
59 */
/*
P/P * Method: void net.sourceforge.pebble.domain.CategoryBuilder(Blog)
*
* Postconditions:
* this.blog == blog
* init'ed(this.blog)
*/
60 public CategoryBuilder(Blog blog) {
61 this.blog = blog;
62 }
63
64 /**
65 * Creates a new instance.
66 */
/*
P/P * Method: void net.sourceforge.pebble.domain.CategoryBuilder(Blog, Category)
*
* Postconditions:
* this.blog == blog
* init'ed(this.blog)
* this.rootCategory == rootCategory
* init'ed(this.rootCategory)
*/
67 public CategoryBuilder(Blog blog, Category rootCategory) {
68 this.blog = blog;
69 this.rootCategory = rootCategory;
70 }
71
72 /**
73 * Adds a category.
74 *
75 * @param category a Category instance
76 */
77 public void addCategory(Category category) {
/*
P/P * Method: void addCategory(Category)
*
* Preconditions:
* category != null
* category.id != null
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* possibly_updated(category.blog)
* possibly_updated(category.parent)
* (soft) init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).subCategories)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* ...
*
* Test Vectors:
* java.lang.String:equals(...)@142: {0}, {1}
*/
78 category.setBlog(blog);
79
80 if (category.isRootCategory()) {
81 this.rootCategory = category;
82 } else {
83 Category parent = getParent(category, true);
84 parent.addSubCategory(category);
85 }
86 }
87
88 /**
89 * Removes a category.
90 *
91 * @param category a Category instance
92 */
93 public void removeCategory(Category category) {
/*
P/P * Method: void removeCategory(Category)
*
* Preconditions:
* category != null
* category.id != null
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* possibly_updated(category.parent)
* init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* possibly_updated(new Category(getCategory#2).id)
* init'ed(new Category(getCategory#2).name)
* ...
*/
94 Category parent = getParent(category);
95 parent.removeSubCategory(category);
96 }
97
98 /**
99 * Gets (and creates if necessary), the parent of the specified category.
100 *
101 * @param category the Category to find the parent of
102 * @param create true if the parent should be created if it doesn't
103 * exist, false otherwise
104 * @return a Category instance
105 *
106 */
107 private Category getParent(Category category, boolean create) {
/*
P/P * Method: Category getParent(Category, bool)
*
* Preconditions:
* category != null
* category.id != null
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* possibly_updated(new Category(getCategory#2).id)
* init'ed(new Category(getCategory#2).name)
* ...
*
* Test Vectors:
* java.lang.String:equals(...)@111: {0}, {1}
*/
108 String id = category.getId();
109 int index = id.lastIndexOf(CATEGORY_SEPARATOR);
110 String parentId = id.substring(0, index);
111 if (parentId.equals("")) {
112 // the parent is the root category
113 parentId = "/";
114 }
115
116 return getCategory(parentId, create);
117 }
118
119 /**
120 * Gets the parent of the specified category.
121 *
122 * @return a Category instance
123 */
124 private Category getParent(Category category) {
/*
P/P * Method: Category getParent(Category)
*
* Preconditions:
* category != null
* category.id != null
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* possibly_updated(new Category(getCategory#2).id)
* init'ed(new Category(getCategory#2).name)
* ...
*/
125 return getParent(category, false);
126 }
127
128 /**
129 * Gets (and creates if necessary), the specified category.
130 *
131 * @param id the id of the category to find
132 * @param create true if the category should be created if it doesn't
133 * exist, false otherwise
134 * @return a Category instance
135 */
136 private Category getCategory(String id, boolean create) {
/*
P/P * Method: Category getCategory(String, bool)
*
* Preconditions:
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Presumptions:
* category.id@165 != null
* java.util.Collections:unmodifiableList(...)@230 != null
* java.util.Iterator:next(...)@165 != null
*
* Postconditions:
* init'ed(return_value)
* (soft) init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* new Category(getCategory#2) num objects <= 1
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* possibly_updated(new Category(getCategory#2).id)
* ...
*
* Test Vectors:
* create: {0}, {1}
* id: Addr_Set{null}, Inverse{null}
* this.rootCategory: Inverse{null}, Addr_Set{null}
* java.lang.String:equals(...)@141: {0}, {1}
* java.lang.String:equals(...)@166: {0}, {1}
* java.lang.String:indexOf(...)@154: {-231..-2, 0..232-2}, {-1}
* java.lang.String:startsWith(...)@137: {1}, {0}
* java.util.Iterator:hasNext(...)@164: {1}, {0}
*/
137 if (id == null || !id.startsWith("/")) {
138 id = "/" + id;
139 }
140
141 if (id.equals("/")) {
142 if (rootCategory == null) {
143 Category category = new Category("/", I18n.getMessage(blog.getLocale(), "category.all"));
144 addCategory(category);
145 }
146
147 return rootCategory;
148 } else {
149 Category parentCategory = getRootCategory();
150 Category category = null;
151 boolean found = false;
152 int index = 0;
153 do {
154 index = id.indexOf("/", index+1);
155 String categoryId;
156 if (index == -1) {
157 categoryId = id.substring(0, id.length());
158 } else {
159 categoryId = id.substring(0, index);
160 }
161
162 found = false;
163 Iterator it = parentCategory.getSubCategories().iterator();
164 while (it.hasNext()) {
165 category = (Category)it.next();
166 if (category.getId().equals(categoryId)) {
167 found = true;
168 break;
169 }
170 }
171
172 if (!found) {
173 if (create) {
174 category = new Category(categoryId, categoryId);
175 addCategory(category);
176 } else {
177 return null;
178 }
179 }
180
181 parentCategory = category;
182 } while (index != -1);
183
184 return category;
185 }
186 }
187
188 /**
189 * Gets (and creates if necessary), the specified category.
190 *
191 * @param id the id of the category to find
192 * @return a Category instance
193 */
194 public Category getCategory(String id) {
/*
P/P * Method: Category getCategory(String)
*
* Preconditions:
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* init'ed(new Category(getCategory#2*) num objects)
* possibly_updated(new Category(getCategory#2*).blog)
* init'ed(new Category(getCategory#2*).blogEntries)
* possibly_updated(new Category(getCategory#2*).id)
* init'ed(new Category(getCategory#2*).name)
* ...
*/
195 return getCategory(id, false);
196 }
197
198 /**
199 * Gets the root category.
200 *
201 * @return a Category instance
202 */
203 public Category getRootCategory() {
/*
P/P * Method: Category getRootCategory()
*
* Preconditions:
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* possibly_updated(new Category(getCategory#2).id)
* init'ed(new Category(getCategory#2).name)
* ...
*/
204 return getCategory("/", true);
205 }
206
207 /**
208 * Gets a collection containing all blog categories,
209 * ordered by category name.
210 *
211 * @return a sorted List of Category instances
212 */
213 public List<Category> getCategories() {
/*
P/P * Method: List getCategories()
*
* Preconditions:
* (soft) init'ed(this.rootCategory)
* (soft) this.blog != null
*
* Postconditions:
* return_value == &new ArrayList(getCategories#1)
* init'ed(this.rootCategory)
* init'ed(new ArrayList(Category#1) num objects)
* init'ed(new ArrayList(Category#2) num objects)
* init'ed(new ArrayList(Category#3) num objects)
* new ArrayList(getCategories#1) num objects == 1
* init'ed(new Category(getCategory#2) num objects)
* possibly_updated(new Category(getCategory#2).blog)
* init'ed(new Category(getCategory#2).blogEntries)
* possibly_updated(new Category(getCategory#2).id)
* ...
*/
214 List<Category> allCategories = new ArrayList<Category>();
215 allCategories.addAll(getCategories(getRootCategory()));
216 Collections.sort(allCategories);
217
218 return allCategories;
219 }
220
221 public List<Category> getCategories(Category category) {
/*
P/P * Method: List getCategories(Category)
*
* Preconditions:
* category != null
* init'ed(category.subCategories)
*
* Presumptions:
* java.util.Collections:unmodifiableList(...)@230 != null
* java.util.Iterator:next(...)@226 != null
*
* Postconditions:
* return_value == &new ArrayList(getCategories#1)
* new ArrayList(getCategories#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@225: {1}, {0}
*/
222 List<Category> allCategories = new ArrayList<Category>();
223 allCategories.add(category);
224 Iterator it = category.getSubCategories().iterator();
225 while (it.hasNext()) {
226 allCategories.addAll(getCategories((Category)it.next()));
227 }
228
229 return allCategories;
230 }
231
232 }
SofCheck Inspector Build Version : 2.22510
| categorybuilder.java |
2010-Jun-25 19:40:32 |
| categorybuilder.class |
2010-Jul-19 20:23:40 |