Coverage Report - net.sourceforge.pebble.domain.Content
 
Classes in this File Line Coverage Branch Coverage Complexity
Content
96%
29/30
83%
5/6
1.529
Content$1
100%
8/8
91%
11/12
1.529
 
 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.api.event.PebbleEvent;
 35  
 import net.sourceforge.pebble.util.StringUtils;
 36  
 
 37  
 import java.beans.PropertyChangeEvent;
 38  
 import java.beans.PropertyChangeListener;
 39  
 import java.beans.PropertyChangeSupport;
 40  
 import java.io.Serializable;
 41  
 import java.util.ArrayList;
 42  
 import java.util.List;
 43  
 
 44  
 /**
 45  
  * Superclass for blog entries, comments and TrackBacks.
 46  
  *
 47  
  * @author    Simon Brown
 48  
  */
 49  420
 public abstract class Content implements Permalinkable, Cloneable, Serializable {
 50  
 
 51  
   /** the state of the object */
 52  
   private State state;
 53  
 
 54  
   /** flag to indicate whether events are enabled */
 55  4344
   private boolean eventsEnabled = false;
 56  
 
 57  
   /** the class responsible for managing property change events */
 58  
   protected transient PropertyChangeSupport propertyChangeSupport;
 59  
 
 60  
   /** the collection of properties that have changed since the last store */
 61  
   private transient ArrayList propertyChangeEvents;
 62  
 
 63  
   /** the collection of PebbleEvent instances that have been initiated */
 64  4344
   private transient List<PebbleEvent> events = new ArrayList<PebbleEvent>();
 65  
 
 66  
   /**
 67  
    * Default, no args constructor.
 68  
    */
 69  4344
   public Content() {
 70  4344
     this.propertyChangeSupport = new PropertyChangeSupport(this);
 71  4344
     this.propertyChangeEvents = new ArrayList();
 72  4344
     this.propertyChangeSupport.addPropertyChangeListener(new PropertyChangeListener() {
 73  
       public void propertyChange(PropertyChangeEvent event) {
 74  12283
         if (areEventsEnabled()) {
 75  432
           Object oldValue = event.getOldValue();
 76  432
           Object newValue = event.getNewValue();
 77  432
           if ((oldValue == null && newValue == null) ||
 78  
               (oldValue != null && newValue != null && oldValue.equals(newValue))) {
 79  12
             return;
 80  
           } else {
 81  420
             Content.this.propertyChangeEvents.add(event);
 82  
           }
 83  
         }
 84  12271
       }
 85  
     });
 86  4344
   }
 87  
 
 88  
   /**
 89  
    * Gets the content of this response.
 90  
    *
 91  
    * @return  a String
 92  
    */
 93  
   public abstract String getContent();
 94  
 
 95  
   /**
 96  
    * Gets the content of this response, truncated and without HTML tags.
 97  
    *
 98  
    * @return    the content of this response as a String
 99  
    */
 100  
   public String getTruncatedContent() {
 101  168
     return StringUtils.truncate(getContent());
 102  
   }
 103  
 
 104  
   /**
 105  
    * Gets the state of this comment.
 106  
    *
 107  
    * @return  a State instance (APPROVED, REJECTED or PENDING)
 108  
    */
 109  
   public State getState() {
 110  17572
     return this.state;
 111  
   }
 112  
 
 113  
   /**
 114  
    * Sets the state of this comment.
 115  
    */
 116  
   void setState(State state) {
 117  8312
     this.state = state;
 118  8312
   }
 119  
 
 120  
   /**
 121  
    * Sets whether events are enabled.
 122  
    *
 123  
    * @param b   true to enable events, false otherwise
 124  
    */
 125  
   void setEventsEnabled(boolean b) {
 126  5092
     this.eventsEnabled = b;
 127  5092
   }
 128  
 
 129  
   /**
 130  
    * Determines whether events are enabled.
 131  
    *
 132  
    * @return  true if events are enabled, false otherwise
 133  
    */
 134  
   boolean areEventsEnabled() {
 135  21279
     return this.eventsEnabled;
 136  
   }
 137  
 
 138  
   /**
 139  
    * Clears existing property change events.
 140  
    */
 141  
   public void clearPropertyChangeEvents() {
 142  1052
     this.propertyChangeEvents = new ArrayList();
 143  1052
   }
 144  
 
 145  
   /**
 146  
    * Determines whether this class has had properties changed since it
 147  
    * was last persisted.
 148  
    *
 149  
    * @return  true if properties have changed, false otherwise
 150  
    */
 151  
   public boolean isDirty() {
 152  304
     return !propertyChangeEvents.isEmpty();
 153  
   }
 154  
 
 155  
   /**
 156  
    * Gets the list of property change events.
 157  
    *
 158  
    * @return  a List of PropertyChangeEvent instances
 159  
    */
 160  
   public List getPropertyChangeEvents() {
 161  72
     return (List)propertyChangeEvents.clone();
 162  
   }
 163  
 
 164  
   /**
 165  
    * Adds an event to the list.
 166  
    *
 167  
    * @param event   a PebbleEvent instance
 168  
    */
 169  
   synchronized void addEvent(PebbleEvent event) {
 170  504
     events.add(event);
 171  504
   }
 172  
 
 173  
   /**
 174  
    * Inserts an event to the list into the front of the list.
 175  
    *
 176  
    * @param event   a PebbleEvent instance
 177  
    */
 178  
   synchronized void insertEvent(PebbleEvent event) {
 179  652
     events.add(0, event);
 180  652
   }
 181  
 
 182  
   /**
 183  
    * Determines whether this object has outstanding events.
 184  
    *
 185  
    * @return    true if events are outstanding, false otherwise
 186  
    */
 187  
   public synchronized boolean hasEvents() {
 188  2832
     return !events.isEmpty();
 189  
   }
 190  
 
 191  
   /**
 192  
    * Gets the next event to be handled.
 193  
    *
 194  
    * @return  a PebbleEvent instance, or null if no more events
 195  
    */
 196  
   public synchronized PebbleEvent nextEvent() {
 197  1024
     if (hasEvents()) {
 198  1024
       return events.remove(0);
 199  
     } else {
 200  0
       return null;
 201  
     }
 202  
   }
 203  
 
 204  
   synchronized void clearEvents() {
 205  1332
     events = new ArrayList<PebbleEvent>();
 206  1332
   }
 207  
 
 208  
   public synchronized List<PebbleEvent> getEvents() {
 209  8
     return new ArrayList<PebbleEvent>(events);
 210  
   }
 211  
 
 212  
 }