Coverage Report - net.sourceforge.pebble.domain.Tag
 
Classes in this File Line Coverage Branch Coverage Complexity
Tag
90%
38/42
94%
17/18
1.917
 
 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 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  4
   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  1234
   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  1234
   public Tag(String name, Blog blog) {
 67  1234
     setName(name);
 68  1234
     this.blog = blog;
 69  1234
   }
 70  
 
 71  
   /**
 72  
    * Gets the name of this tag.
 73  
    *
 74  
    * @return    the name as a String
 75  
    */
 76  
   public String getName() {
 77  7794
     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) {
 86  1242
     this.name = Tag.encode(name);
 87  1242
   }
 88  
 
 89  
   /**
 90  
    * Gets the permalink for this object.
 91  
    *
 92  
    * @return  a URL as a String
 93  
    */
 94  
   public String getPermalink() {
 95  12
     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() {
 104  0
     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) {
 114  3574
     if (!(o instanceof Tag)) {
 115  0
       return false;
 116  
     }
 117  
 
 118  3574
     Tag tag = (Tag)o;
 119  3574
     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) {
 135  1579
     Tag tag = (Tag)o;
 136  1579
     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() {
 145  0
     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() {
 154  0
     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) {
 165  1928
     List<Tag> list = new ArrayList<Tag>();
 166  
 
 167  1928
     if (tags != null && tags.trim().length() > 0) {
 168  451
       String s[] = tags.trim().split(" ");
 169  1485
       for (int i = 0; i < s.length; i++) {
 170  1034
         Tag tag = new Tag(StringUtils.transformHTML(s[i].trim()), blog);
 171  1034
         if (!list.contains(tag)) {
 172  1026
           list.add(tag);
 173  
         }
 174  
       }
 175  
     }
 176  
 
 177  1928
     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) {
 187  1788
     StringBuilder builder = new StringBuilder();
 188  
 
 189  1788
     if (tags != null) {
 190  1784
       Iterator it = tags.iterator();
 191  2682
       while (it.hasNext()) {
 192  898
         Tag tag = (Tag)it.next();
 193  898
         builder.append(tag.getName());
 194  898
         if (it.hasNext()) {
 195  587
           builder.append(", ");
 196  
         }
 197  898
       }
 198  
     }
 199  
 
 200  1788
     return builder.toString();
 201  
   }
 202  
 
 203  
   /**
 204  
    * Encodes a tag.
 205  
    *
 206  
    * @param tag   a String
 207  
    */
 208  
   public static String encode(String tag) {
 209  1282
     if (tag == null) {
 210  4
       return "";
 211  
     } else {
 212  1278
       String encodedTag = tag.trim().toLowerCase();
 213  1278
       encodedTag = encodedTag.replaceAll("\\+", " ");
 214  1278
       return encodedTag;
 215  
     }
 216  
   }
 217  
 
 218  
 }