Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
67   414   48   2,31
28   159   0,72   29
29     1,66  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  Category       Line # 47 67 0% 48 3 97,6% 0.9758065
 
  (296)
 
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 org.apache.commons.logging.Log;
35    import org.apache.commons.logging.LogFactory;
36   
37    import java.io.Serializable;
38    import java.util.*;
39   
40    import net.sourceforge.pebble.comparator.ReverseBlogEntryIdComparator;
41   
42    /**
43    * Represents a blog category.
44    *
45    * @author Simon Brown
46    */
 
47    public class Category implements Permalinkable, Comparable, Serializable {
48   
49    /** the log used by this class */
50    private static final Log log = LogFactory.getLog(Category.class);
51   
52    /** the root category identifier */
53    private static final String ROOT_CATEGORY_IDENTIFIER = "/";
54   
55    /** the owning blog */
56    private transient Blog blog;
57   
58    /** the id of the category */
59    private String id = "";
60   
61    /** the name of the category */
62    private String name = "";
63   
64    /** the parent category, if applicable */
65    private Category parent = null;
66   
67    /** the set of tags for this category */
68    private String tags = "";
69   
70    /** the list of tags for this category */
71    private List tagsAsList = new ArrayList();
72   
73    /** the sub-categories */
74    private List subCategories = new ArrayList();
75   
76    /** the blog entries associated with this category */
77    private List<String> blogEntries = new ArrayList<String>();
78   
79    /**
80    * Default, no args constructor.
81    */
 
82  6 toggle public Category() {
83    }
84   
85    /**
86    * Creates a new category with the specified properties.
87    *
88    * @param id the id
89    * @param name the name
90    */
 
91  1570 toggle public Category(String id, String name) {
92  1570 setId(id);
93   
94  1570 this.name = name;
95    }
96   
97    /**
98    * Gets the id of this category.
99    *
100    * @return the id as a String
101    */
 
102  854 toggle public String getId() {
103  854 return id;
104    }
105   
106    /**
107    * Sets the id of this category.
108    *
109    * @param id the id as a String
110    */
 
111  1570 toggle public void setId(String id) {
112  1570 this.id = id;
113  1570 if (this.id == null || !this.id.startsWith("/")) {
114  42 this.id = "/" + this.id;
115    }
116    }
117   
118    /**
119    * Gets the name of this category.
120    *
121    * @return the name as a String
122    */
 
123  40 toggle public String getName() {
124  40 return name;
125    }
126   
127    /**
128    * Sets the name of this category.
129    *
130    * @param name the new category name
131    */
 
132  0 toggle public void setName(String name) {
133  0 this.name = name;
134    }
135   
136    /**
137    * Determines whether this category is a root category.
138    *
139    * @return true if the ID is "/", false otherwise
140    */
 
141  1512 toggle public boolean isRootCategory() {
142  1512 return id.equals(ROOT_CATEGORY_IDENTIFIER);
143    }
144   
145    /**
146    * Gets the parent of thie category.
147    *
148    * @return a Category instance, or null if this category has no parent
149    */
 
150  1176 toggle public Category getParent() {
151  1176 return this.parent;
152    }
153   
154    /**
155    * Determines whether this category has the specified parent.
156    *
157    * @param category a Category to test for
158    * @return true if this category has the specified category as one of its
159    * parents, false otherwise
160    */
 
161  14 toggle public boolean hasParent(Category category) {
162  14 Category parent = getParent();
163  24 while (parent != null) {
164  14 if (parent.equals(category)) {
165  4 return true;
166    } else {
167  10 parent = parent.getParent();
168    }
169    }
170   
171  10 return false;
172    }
173   
174    /**
175    * Sets the parent of this category.
176    *
177    * @param parent a Category instance
178    */
 
179  132 toggle public void setParent(Category parent) {
180  132 this.parent = parent;
181    }
182   
183    /**
184    * Gets the number of parents that this category has.
185    *
186    * @return the number of parents this category has, or 0 if it is
187    * the root category
188    */
 
189  6 toggle public int getNumberOfParents() {
190  6 int parents = 0;
191  6 Category parent = getParent();
192  12 while (parent != null) {
193  6 parents++;
194  6 parent = parent.getParent();
195    }
196   
197  6 return parents;
198    }
199   
200    /**
201    * Adds a sub-category.
202    *
203    * @param category a Category instance
204    */
 
205  130 toggle public synchronized void addSubCategory(Category category) {
206  130 if (subCategories != null && !subCategories.contains(category)) {
207  128 subCategories.add(category);
208  128 category.setParent(this);
209    }
210    }
211   
212    /**
213    * Removes a sub-category.
214    *
215    * @param category a Category instance
216    */
 
217  6 toggle public synchronized void removeSubCategory(Category category) {
218  6 if (subCategories != null && subCategories.contains(category)) {
219  4 subCategories.remove(category);
220  4 category.setParent(null);
221    }
222    }
223   
224    /**
225    * Gets the list of sub-categories.
226    *
227    * @return a List of Category instances
228    */
 
229  478 toggle public List getSubCategories() {
230  478 return Collections.unmodifiableList(subCategories);
231    }
232   
233    /**
234    * Gets the tags associated with this category.
235    *
236    * @return a list of tags
237    */
 
238  52 toggle public String getTags() {
239  52 return this.tags;
240    }
241   
242    /**
243    * Gets the tags associated with this category, as a List.
244    *
245    * @return a List of tags
246    */
 
247  1066 toggle public List getTagsAsList() {
248  1066 return this.tagsAsList;
249    }
250   
251    /**
252    * Gets the tags associated with this category and its parents.
253    *
254    * @return a list of tags
255    */
 
256  880 toggle public List getAllTags() {
257  880 List l = new ArrayList();
258   
259  880 l.addAll(getTagsAsList());
260  880 Category parent = getParent();
261  1014 while (parent != null) {
262  134 l.addAll(parent.getTagsAsList());
263  134 parent = parent.getParent();
264    }
265   
266  880 return l;
267    }
268   
269    /**
270    * Sets the set of tags associated with this category.
271    *
272    * @param newTags a set of tags
273    */
 
274  68 toggle public void setTags(String newTags) {
275  68 if (newTags != null && newTags.indexOf(",") > -1) {
276    // if the tags have been comma separated, convert them to
277    // whitespace separated by
278    // - remove whitespace
279    // - convert commas to whitespace
280  2 newTags = newTags.replaceAll(" ", "").replaceAll(",", " ");
281    }
282  68 this.tags = newTags;
283  68 this.tagsAsList = Tag.parse(blog, tags);
284    }
285   
286    /**
287    * Sets the owning blog.
288    *
289    * @param blog a Blog instance
290    */
 
291  1500 toggle public void setBlog(Blog blog) {
292  1500 this.blog = blog;
293    }
294   
295    /**
296    * Gets the permalink for this object.
297    *
298    * @return a URL as a String
299    */
 
300  14 toggle public String getPermalink() {
301  14 if (isRootCategory()) {
302  2 return blog.getUrl() + "categories/";
303    } else {
304  12 return blog.getUrl() + "categories" + id + "/";
305    }
306    }
307   
308    /**
309    * Gets the hashcode of this object.
310    *
311    * @return the hashcode as an int
312    */
 
313  978 toggle public int hashCode() {
314  978 return id.hashCode();
315    }
316   
317    /**
318    * Determines whether the specified object is equal to this one.
319    *
320    * @param o the object to compare against
321    * @return true if Object o represents the same category, false otherwise
322    */
 
323  136 toggle public boolean equals(Object o) {
324  136 if (!(o instanceof Category)) {
325  2 return false;
326    }
327   
328  134 Category cat = (Category)o;
329  134 return (cat.getId().equals(id));
330    }
331   
332    /**
333    * Compares this object with the specified object for order. Returns a
334    * negative integer, zero, or a positive integer as this object is less
335    * than, equal to, or greater than the specified object.<p>
336    *
337    * @param o the Object to be compared.
338    * @return a negative integer, zero, or a positive integer as this object
339    * is less than, equal to, or greater than the specified object.
340    *
341    * @throws ClassCastException if the specified object's type prevents it
342    * from being compared to this Object.
343    */
 
344  58 toggle public int compareTo(Object o) {
345  58 Category category = (Category)o;
346  58 return getId().compareTo(category.getId());
347    }
348   
349    /**
350    * Returns a String representation of this object.
351    *
352    * @return a String
353    */
 
354  2 toggle public String toString() {
355  2 return this.name;
356    }
357   
358    /**
359    * Gets the blog entries associated with this category.
360    *
361    * @return a Collection of BlogEntry instances
362    */
 
363  182 toggle public List<String> getBlogEntries() {
364  182 return new ArrayList<String>(blogEntries);
365    }
366   
367    /**
368    * Adds a blog entry to this category.
369    *
370    * @param blogEntry a blog entry id
371    */
 
372  68 toggle public synchronized void addBlogEntry(String blogEntry) {
373  68 if (blogEntry != null && !blogEntries.contains(blogEntry)) {
374  64 blogEntries.add(blogEntry);
375  64 Collections.sort(blogEntries, new ReverseBlogEntryIdComparator());
376   
377  64 if (getParent() != null) {
378  34 getParent().addBlogEntry(blogEntry);
379    }
380    }
381    }
382   
383    /**
384    * Removes a blog entry from this category.
385    *
386    * @param blogEntry a blog entry id
387    */
 
388  10 toggle public synchronized void removeBlogEntry(String blogEntry) {
389  10 if (blogEntry != null) {
390  10 blogEntries.remove(blogEntry);
391   
392  10 if (getParent() != null) {
393  2 getParent().removeBlogEntry(blogEntry);
394    }
395    }
396    }
397   
398    /**
399    * Removes all blog entries from this category.
400    */
 
401  18 toggle public synchronized void removeAllBlogEntries() {
402  18 blogEntries = new ArrayList<String>();
403    }
404   
405    /**
406    * Gets the number of blog entries associated with this category.
407    *
408    * @return an int
409    */
 
410  4 toggle public int getNumberOfBlogEntries() {
411  4 return this.blogEntries.size();
412    }
413   
414    }