File Source: tag.java
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 net.sourceforge.pebble.util.StringUtils;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import java.util.*;
40
41 /**
42 * Represents a tag.
43 *
44 * @author Simon Brown
45 */
46 public class Tag implements Permalinkable, Comparable {
47
48 /** the log used by this class */
49 private static final Log log = LogFactory.getLog(Tag.class);
50
51 /** the owning blog */
52 private Blog blog;
53
54 /** the name of the tag */
55 private String name = "";
56
57 /** the rank for this tag */
58 protected int rank;
59
60 /**
61 * Creates a new tag with the specified properties.
62 *
63 * @param name the name
64 * @param blog a Blog instance
65 */
66 public Tag(String name, Blog blog) {
67 setName(name);
68 this.blog = blog;
69 }
70
71 /**
72 * Gets the name of this tag.
73 *
74 * @return the name as a String
75 */
76 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* init'ed(this.name)
*
* Postconditions:
* return_value == this.name
* init'ed(return_value)
*/
77 return name;
78 }
79
80 /**
81 * Sets the name of this tag.
82 *
83 * @param name the new tag name
84 */
85 public void setName(String name) {
/*
P/P * Method: void setName(String)
*
* Postconditions:
* this.name != null
*/
86 this.name = Tag.encode(name);
87 }
88
89 /**
90 * Gets the permalink for this object.
91 *
92 * @return a URL as a String
93 */
94 public String getPermalink() {
/*
P/P * Method: String getPermalink()
*
* Preconditions:
* this.blog != null
* init'ed(this.name)
*
* Postconditions:
* return_value != null
*
* Preconditions:
* (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
* (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
* (soft) init'ed(this.blog.id)
*/
95 return blog.getUrl() + "tags/" + name + "/";
96 }
97
98 /**
99 * Gets the hashcode of this object.
100 *
101 * @return the hashcode as an int
102 */
103 public int hashCode() {
/*
P/P * Method: int hashCode()
*
* Preconditions:
* this.name != null
*
* Postconditions:
* init'ed(return_value)
*/
104 return name.hashCode();
105 }
106
107 /**
108 * Determines whether the specified object is equal to this one.
109 *
110 * @param o the object to compare against
111 * @return true if Object o represents the same tag, false otherwise
112 */
113 public boolean equals(Object o) {
/*
P/P * Method: bool equals(Object)
*
* Preconditions:
* (soft) o.name != null
* (soft) init'ed(this.name)
*
* Postconditions:
* init'ed(return_value)
*/
114 if (!(o instanceof Tag)) {
115 return false;
116 }
117
118 Tag tag = (Tag)o;
119 return tag.getName().equals(name);
120 }
121
122 /**
123 * Compares this object with the specified object for order. Returns a
124 * negative integer, zero, or a positive integer as this object is less
125 * than, equal to, or greater than the specified object.<p>
126 *
127 * @param o the Object to be compared.
128 * @return a negative integer, zero, or a positive integer as this object
129 * is less than, equal to, or greater than the specified object.
130 *
131 * @throws ClassCastException if the specified object's type prevents it
132 * from being compared to this Object.
133 */
134 public int compareTo(Object o) {
/*
P/P * Method: int compareTo(Object)
*
* Preconditions:
* o != null
* init'ed(o.name)
* this.name != null
*
* Postconditions:
* init'ed(return_value)
*/
135 Tag tag = (Tag)o;
136 return getName().compareTo(tag.getName());
137 }
138
139 /**
140 * Returns a String representation of this object.
141 *
142 * @return a String
143 */
144 public String toString() {
/*
P/P * Method: String toString()
*
* Preconditions:
* init'ed(this.name)
*
* Postconditions:
* return_value == this.name
* init'ed(return_value)
*/
145 return this.name;
146 }
147
148 /**
149 * Gets the rank for this tag.
150 *
151 * @return an int between 1 and 10;
152 */
153 public int getRank() {
/*
P/P * Method: int getRank()
*
* Preconditions:
* init'ed(this.rank)
*
* Postconditions:
* return_value == this.rank
* init'ed(return_value)
*/
154 return this.rank;
155 }
156
157 /**
158 * Given a string containing whitespace separated tags, this method returns a
159 * List containing the tags.
160 *
161 * @param tags a whitespace separated list of tags
162 * @return a List of Tag instances
163 */
164 public static List<Tag> parse(Blog blog, String tags) {
/*
P/P * Method: List parse(Blog, String)
*
* Presumptions:
* s.length@168 <= 232-1
* s[i]@168 != null
*
* Postconditions:
* return_value == &new ArrayList(parse#1)
* new ArrayList(parse#1) num objects == 1
*
* Test Vectors:
* tags: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@167: {0}, {1..232-1}
* java.util.List:contains(...)@171: {1}, {0}
*/
165 List<Tag> list = new ArrayList<Tag>();
166
167 if (tags != null && tags.trim().length() > 0) {
168 String s[] = tags.trim().split(" ");
169 for (int i = 0; i < s.length; i++) {
170 Tag tag = new Tag(StringUtils.transformHTML(s[i].trim()), blog);
171 if (!list.contains(tag)) {
172 list.add(tag);
173 }
174 }
175 }
176
177 return list;
178 }
179
180 /**
181 * Given a list of tags, this method formats them into a comma separated list.
182 *
183 * @param tags a List of tags
184 * @return a comma separated String
185 */
186 public static String format(List<Tag> tags) {
/*
P/P * Method: String format(List)
*
* Presumptions:
* java.util.Iterator:next(...)@192 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* tags: Addr_Set{null}, Inverse{null}
* java.util.Iterator:hasNext(...)@191: {1}, {0}
* java.util.Iterator:hasNext(...)@194: {0}, {1}
*/
187 StringBuilder builder = new StringBuilder();
188
189 if (tags != null) {
190 Iterator it = tags.iterator();
191 while (it.hasNext()) {
192 Tag tag = (Tag)it.next();
193 builder.append(tag.getName());
194 if (it.hasNext()) {
195 builder.append(", ");
196 }
197 }
198 }
199
200 return builder.toString();
201 }
202
203 /**
204 * Encodes a tag.
205 *
206 * @param tag a String
207 */
208 public static String encode(String tag) {
/*
P/P * Method: String encode(String)
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* tag: Inverse{null}, Addr_Set{null}
*/
209 if (tag == null) {
210 return "";
211 } else {
212 String encodedTag = tag.trim().toLowerCase();
213 encodedTag = encodedTag.replaceAll("\\+", " ");
214 return encodedTag;
215 }
216 }
217
218 }
SofCheck Inspector Build Version : 2.22510
| tag.java |
2010-Jun-25 19:40:32 |
| tag.class |
2010-Jul-19 20:23:40 |