Coverage Report - net.sourceforge.pebble.logging.CountedUrl
 
Classes in this File Line Coverage Branch Coverage Complexity
CountedUrl
90%
37/41
93%
15/16
1.611
 
 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.logging;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 
 36  
 import java.util.List;
 37  
 import java.util.LinkedList;
 38  
 import java.util.Collections;
 39  
 
 40  
 /**
 41  
  * Represents a visited or referer URL along with a count of how many times
 42  
  * that URL has been accessed/referred from.
 43  
  *
 44  
  * @author    Simon Brown
 45  
  */
 46  
 public abstract class CountedUrl {
 47  
 
 48  
   /** the maximum length of the name */
 49  
   public static final int NAME_LENGTH_LIMIT = 60;
 50  
 
 51  
   /** the URL as a String */
 52  
   private String url;
 53  
 
 54  
   /** the displayable name for the URL */
 55  
   private String name;
 56  
 
 57  
   /** the collection of log entries that relate to this url */
 58  556
   private List<LogEntry> logEntries = new LinkedList<LogEntry>();
 59  
 
 60  556
   private boolean newsFeed = false;
 61  556
   private boolean pageView = false;
 62  556
   private boolean fileDownload = false;
 63  
 
 64  
   protected Blog blog;
 65  
 
 66  
   /**
 67  
    * Creates a new CountedUrl representing the specified url.
 68  
    *
 69  
    * @param url   the url as a String
 70  
    */
 71  420
   public CountedUrl(String url) {
 72  420
     setUrl(url);
 73  420
   }
 74  
 
 75  
   /**
 76  
    * Creates a new CountedUrl representing the specified url.
 77  
    *
 78  
    * @param url   the url as a String
 79  
    */
 80  136
   public CountedUrl(String url, Blog blog) {
 81  136
     this.blog = blog;
 82  136
     setUrl(url);
 83  136
   }
 84  
 
 85  
   /**
 86  
    * Gets the underlying url.
 87  
    *
 88  
    * @return    the url as a String
 89  
    */
 90  
   public String getUrl() {
 91  88
     return url;
 92  
   }
 93  
 
 94  
   /**
 95  
    * Sets the underlying url.
 96  
    *
 97  
    * @param url   the url as a String
 98  
    */
 99  
   protected void setUrl(String url) {
 100  556
     this.url = url;
 101  556
   }
 102  
 
 103  
   /**
 104  
    * Gets a name representation of the url. This is just the url, but truncated
 105  
    * to a maximum number of characters.
 106  
    *
 107  
    * @return    a String
 108  
    */
 109  
   public String getName() {
 110  688
     return this.name;
 111  
   }
 112  
 
 113  
   /**
 114  
    * Sets the name.
 115  
    *
 116  
    * @param name    the name as a String
 117  
    */
 118  
   protected void setName(String name) {
 119  556
     this.name = name;
 120  556
   }
 121  
 
 122  
   /**
 123  
    * Gets a name representation of the url. This is just the url, but truncated
 124  
    * to a maximum number of characters.
 125  
    *
 126  
    * @return    a String
 127  
    */
 128  
   public String getTruncatedName() {
 129  16
     String s = getName();
 130  16
     if (s.length() <= NAME_LENGTH_LIMIT) {
 131  8
       return s;
 132  
     } else {
 133  8
       return s.substring(0, NAME_LENGTH_LIMIT - 3) + "...";
 134  
     }
 135  
   }
 136  
 
 137  
   /**
 138  
    * Adds a LogEntry.
 139  
    *
 140  
    * @param logEntry    a LogEntry instance
 141  
    */
 142  
   public void addLogEntry(LogEntry logEntry) {
 143  184
     logEntries.add(logEntry);
 144  184
   }
 145  
 
 146  
   /**
 147  
    * Gets the list of log entries associated with this URL
 148  
    *
 149  
    * @return  a List of LogEntry instances
 150  
    */
 151  
   public List<LogEntry> getLogEntries() {
 152  0
     return new LinkedList<LogEntry>(logEntries);
 153  
   }
 154  
 
 155  
   /**
 156  
    * Gets the count associated with this url.
 157  
    *
 158  
    * @return    the count as an int
 159  
    */
 160  
   public int getCount() {
 161  112
     return logEntries.size();
 162  
   }
 163  
 
 164  
   /**
 165  
    * Implementation of the hashCode() method.
 166  
    *
 167  
    * @return  the hashcode of the underlying url
 168  
    */
 169  
   public int hashCode() {
 170  16
     return url == null ? 0 : url.hashCode();
 171  
   }
 172  
 
 173  
   /**
 174  
    * Determines whether this object is equal to another.
 175  
    *
 176  
    * @param o   the object to test against
 177  
    * @return  true if the specified object is the same as this one (i.e. the
 178  
    *          underlying urls match, false otherwise
 179  
    */
 180  
   public boolean equals(Object o) {
 181  64
     if (this == o) return true;
 182  56
     if (!(o instanceof CountedUrl)) return false;
 183  
 
 184  40
     CountedUrl cUrl = (CountedUrl)o;
 185  
 
 186  40
     if (url == null && cUrl.getUrl() == null) {
 187  8
       return true;
 188  
     } else {
 189  32
       return (url != null && url.equals(cUrl.getUrl()));
 190  
     }
 191  
   }
 192  
 
 193  
   public boolean isNewsFeed() {
 194  0
     return newsFeed;
 195  
   }
 196  
 
 197  
   public void setNewsFeed(boolean newsFeed) {
 198  80
     this.newsFeed = newsFeed;
 199  80
   }
 200  
 
 201  
   public boolean isPageView() {
 202  0
     return pageView;
 203  
   }
 204  
 
 205  
   public void setPageView(boolean pageView) {
 206  208
     this.pageView = pageView;
 207  208
   }
 208  
 
 209  
   public boolean isFileDownload() {
 210  0
     return fileDownload;
 211  
   }
 212  
 
 213  
   public void setFileDownload(boolean fileDownload) {
 214  8
     this.fileDownload = fileDownload;
 215  8
   }
 216  
   
 217  
 }