responseindex.java

### File Source: responseindex.java

         1  package net.sourceforge.pebble.index;
         2
         3  import net.sourceforge.pebble.comparator.ReverseResponseIdComparator;
         4  import net.sourceforge.pebble.domain.Blog;
         5  import net.sourceforge.pebble.domain.BlogEntry;
         6  import net.sourceforge.pebble.domain.Response;
         7  import net.sourceforge.pebble.domain.State;
         8  import org.apache.commons.logging.Log;
         9  import org.apache.commons.logging.LogFactory;
        10
        11  import java.io.*;
        12  import java.util.ArrayList;
        13  import java.util.Collections;
        14  import java.util.List;
        15  import java.util.Collection;
        16
        17  /**
        18   * Keeps an index of all responses.
        19   *
        20   * @author    Simon Brown
        21   */
        22  public class ResponseIndex {
        23
               /*
        P/P     *  Method: net.sourceforge.pebble.index.ResponseIndex__static_init
                *
                *  Postconditions:
                *    init'ed(log)
                */
        24    private static final Log log = LogFactory.getLog(ResponseIndex.class);
        25
        26    private Blog blog;
        27
        28    private List<String> approvedResponses = new ArrayList<String>();
        29    private List<String> pendingResponses = new ArrayList<String>();
        30    private List<String> rejectedResponses = new ArrayList<String>();
        31
               /*
        P/P     *  Method: void net.sourceforge.pebble.index.ResponseIndex(Blog)
                *
                *  Preconditions:
                *    blog != null
                *
                *  Postconditions:
                *    this.approvedResponses == &new ArrayList(readIndex#1)
                *    this.blog == blog
                *    this.blog != null
                *    this.pendingResponses == &new ArrayList(readIndex#1)
                *    this.rejectedResponses == &new ArrayList(readIndex#1)
                *    new ArrayList(ResponseIndex#1) num objects == 1
                *    new ArrayList(ResponseIndex#2) num objects == 1
                *    new ArrayList(ResponseIndex#3) num objects == 1
                *    new ArrayList(readIndex#1) num objects == 3
                */
        32    public ResponseIndex(Blog blog) {
        33      this.blog = blog;
        34
        35      approvedResponses = readIndex(State.APPROVED);
        36      pendingResponses = readIndex(State.PENDING);
        37      rejectedResponses = readIndex(State.REJECTED);
        38    }
        39
        40    /**
        41     * Clears the index.
        42     */
        43    public void clear() {
                 /*
        P/P       *  Method: void clear()
                  *
                  *  Preconditions:
                  *    (soft) this.pendingResponses != null
                  *    (soft) this.rejectedResponses != null
                  *    (soft) this.blog != null
                  *
                  *  Postconditions:
                  *    this.approvedResponses == &new ArrayList(clear#1)
                  *    this.pendingResponses == &new ArrayList(clear#2)
                  *    this.rejectedResponses == &new ArrayList(clear#3)
                  *    new ArrayList(clear#1) num objects == 1
                  *    new ArrayList(clear#2) num objects == 1
                  *    new ArrayList(clear#3) num objects == 1
                  */
        44      approvedResponses = new ArrayList<String>();
        45      writeIndex(State.APPROVED);
        46
        47      pendingResponses = new ArrayList<String>();
        48      writeIndex(State.PENDING);
        49
        50      rejectedResponses = new ArrayList<String>();
        51      writeIndex(State.REJECTED);
        52    }
        53
        54    /**
        55     * Indexes one or more blog entries.
        56     *
        57     * @param blogEntries   a List of BlogEntry instances
        58     */
        59    public synchronized void index(Collection<BlogEntry> blogEntries) {
                 /*
        P/P       *  Method: void index(Collection)
                  *
                  *  Preconditions:
                  *    blogEntries != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#1).name != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#2).name != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#3).name != null
                  *    (soft) this.approvedResponses != null
                  *    (soft) this.blog != null
                  *    (soft) this.pendingResponses != null
                  *    (soft) this.rejectedResponses != null
                  *
                  *  Presumptions:
                  *    blogEntry.comments@60 != null
                  *    java.util.Iterator:next(...)@60 != null
                  *    java.util.Iterator:next(...)@61 != null
                  *    response.blogEntry@62 != null
                  *    response.blogEntry@64 != null
                  *    ...
                  *
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@60: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@61: {1}, {0}
                  */
        60      for (BlogEntry blogEntry : blogEntries) {
        61        for (Response response : blogEntry.getResponses()) {
        62          if (response.isApproved()) {
        63            approvedResponses.add(response.getGuid());
        64          } else if (response.isPending()) {
        65            pendingResponses.add(response.getGuid());
        66          } else if (response.isRejected()) {
        67            rejectedResponses.add(response.getGuid());
        68          }
        69        }
        70      }
        71
        72      Collections.sort(approvedResponses, new ReverseResponseIdComparator());
        73      Collections.sort(pendingResponses, new ReverseResponseIdComparator());
        74      Collections.sort(rejectedResponses, new ReverseResponseIdComparator());
        75      writeIndex(State.APPROVED);
        76      writeIndex(State.PENDING);
        77      writeIndex(State.REJECTED);
        78    }
        79
        80    /**
        81     * Indexes a single response.
        82     *
        83     * @param response    a Response instance
        84     */
        85    public synchronized void index(Response response) {
                 /*
        P/P       *  Method: void index(Response)
                  *
                  *  Preconditions:
                  *    response != null
                  *    response.state != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#1).name != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#2).name != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#3).name != null
                  *    (soft) response.blogEntry != null
                  *    (soft) response.date != null
                  *    (soft) init'ed(response.state.name)
                  *    (soft) this.approvedResponses != null
                  *    (soft) this.blog != null
                  *    ...
                  */
        86      if (response.isApproved()) {
        87        approvedResponses.add(response.getGuid());
        88        Collections.sort(approvedResponses, new ReverseResponseIdComparator());
        89        writeIndex(State.APPROVED);
        90      } else if (response.isPending()) {
        91        pendingResponses.add(response.getGuid());
        92        Collections.sort(pendingResponses, new ReverseResponseIdComparator());
        93        writeIndex(State.PENDING);
        94      } else if (response.isRejected()) {
        95        rejectedResponses.add(response.getGuid());
        96        Collections.sort(rejectedResponses, new ReverseResponseIdComparator());
        97        writeIndex(State.REJECTED);
        98      }
        99    }
       100
       101    /**
       102     * Unindexes a single response.
       103     *
       104     * @param response    a Response instance
       105     */
       106    public synchronized void unindex(Response response) {
                 /*
        P/P       *  Method: void unindex(Response)
                  *
                  *  Preconditions:
                  *    response != null
                  *    response.blogEntry != null
                  *    response.date != null
                  *    this.approvedResponses != null
                  *    (soft) this.blog != null
                  *    (soft) this.pendingResponses != null
                  *    (soft) this.rejectedResponses != null
                  *
                  *  Test Vectors:
                  *    java.util.List:contains(...)@107: {0}, {1}
                  *    java.util.List:contains(...)@110: {0}, {1}
                  *    java.util.List:contains(...)@113: {0}, {1}
                  */
       107      if (approvedResponses.contains(response.getGuid())) {
       108        approvedResponses.remove(response.getGuid());
       109        writeIndex(State.APPROVED);
       110      } else if (pendingResponses.contains(response.getGuid())) {
       111        pendingResponses.remove(response.getGuid());
       112        writeIndex(State.PENDING);
       113      } else if (rejectedResponses.contains(response.getGuid())) {
       114        rejectedResponses.remove(response.getGuid());
       115        writeIndex(State.REJECTED);
       116      }
       117    }
       118
       119    /**
       120     * Helper method to load the index.
       121     */
       122    private List<String> readIndex(State state) {
                 /*
        P/P       *  Method: List readIndex(State)
                  *
                  *  Preconditions:
                  *    this.blog != null
                  *
                  *  Presumptions:
                  *    org.apache.commons.logging.LogFactory:getLog(...)@24 != null
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(readIndex#1)
                  *    new ArrayList(readIndex#1) num objects == 1
                  *
                  *  Test Vectors:
                  *    java.io.File:exists(...)@134: {0}, {1}
                  */
       123      String filename = null;
       124      List<String> responses = new ArrayList<String>();
       125      if (state == State.APPROVED) {
       126          filename = "responses-approved.index";
       127      } else if (state == State.PENDING) {
       128        filename = "responses-pending.index";
       129      } else if (state == State.REJECTED) {
       130        filename = "responses-rejected.index";
       131      }
       132
       133      File indexFile = new File(blog.getIndexesDirectory(), filename);
       134      if (indexFile.exists()) {
       135        try {
       136          BufferedReader reader = new BufferedReader(new FileReader(indexFile));
       137          String response = reader.readLine();
       138          while (response != null) {
       139            responses.add(response);
       140            response = reader.readLine();
       141          }
       142
       143          reader.close();
       144        } catch (Exception e) {
       145          log.error("Error while reading index", e);
       146        }
       147      }
       148
       149      Collections.sort(responses, new ReverseResponseIdComparator());
       150
       151      return responses;
       152    }
       153
       154    /**
       155     * Helper method to write out the index to disk.
       156     */
       157    private void writeIndex(State state) {
                 /*
        P/P       *  Method: void writeIndex(State)
                  *
                  *  Preconditions:
                  *    (soft) this.approvedResponses != null
                  *    (soft) this.blog != null
                  *    (soft) this.pendingResponses != null
                  *    (soft) this.rejectedResponses != null
                  *
                  *  Presumptions:
                  *    org.apache.commons.logging.LogFactory:getLog(...)@24 != null
                  *
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@175: {1}, {0}
                  */
       158      String filename = null;
       159      List<String> responses = null;
       160      if (state == State.APPROVED) {
       161          filename = "responses-approved.index";
       162          responses = approvedResponses;
       163      } else if (state == State.PENDING) {
       164        filename = "responses-pending.index";
       165        responses = pendingResponses;
       166      } else if (state == State.REJECTED) {
       167        filename = "responses-rejected.index";
       168        responses = rejectedResponses;
       169      }
       170
       171      try {
       172        File indexFile = new File(blog.getIndexesDirectory(), filename);
       173        BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
       174
       175        for (String response : responses) {
       176          writer.write(response);
       177          writer.newLine();
       178        }
       179
       180        writer.flush();
       181        writer.close();
       182      } catch (Exception e) {
       183        log.error("Error while writing index", e);
       184      }
       185    }
       186
       187    /**
       188     * Gets the number of approved responses for this blog.
       189     *
       190     * @return  an int
       191     */
       192    public int getNumberOfApprovedResponses() {
                 /*
        P/P       *  Method: int getNumberOfApprovedResponses()
                  *
                  *  Preconditions:
                  *    this.approvedResponses != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       193      return approvedResponses.size();
       194    }
       195
       196    /**
       197     * Gets the number of pending responses for this blog.
       198     *
       199     * @return  an int
       200     */
       201    public int getNumberOfPendingResponses() {
                 /*
        P/P       *  Method: int getNumberOfPendingResponses()
                  *
                  *  Preconditions:
                  *    this.pendingResponses != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       202      return pendingResponses.size();
       203    }
       204
       205    /**
       206     * Gets the number of rejected responses for this blog.
       207     *
       208     * @return  an int
       209     */
       210    public int getNumberOfRejectedResponses() {
                 /*
        P/P       *  Method: int getNumberOfRejectedResponses()
                  *
                  *  Preconditions:
                  *    this.rejectedResponses != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       211      return rejectedResponses.size();
       212    }
       213
       214    /**
       215     * Gets the number of responses for this blog.
       216     *
       217     * @return  an int
       218     */
       219    public int getNumberOfResponses() {
                 /*
        P/P       *  Method: int getNumberOfResponses()
                  *
                  *  Preconditions:
                  *    this.approvedResponses != null
                  *    this.pendingResponses != null
                  *    this.rejectedResponses != null
                  *
                  *  Presumptions:
                  *    java.util.List:size(...)@202 + java.util.List:size(...)@193 in -232..6_442_450_943
                  *    java.util.List:size(...)@202 + java.util.List:size(...)@193 + java.util.List:size(...)@211 in -231..232-1
                  *
                  *  Postconditions:
                  *    (soft) init'ed(return_value)
                  */
       220      return getNumberOfApprovedResponses() + getNumberOfPendingResponses() + getNumberOfRejectedResponses();
       221    }
       222
       223    /**
       224     * Gets the most recent N approved responses.
       225     *
       226     * @return  a List of response IDs
       227     */
       228    public List<String> getRecentResponses(int number) {
                 /*
        P/P       *  Method: List getRecentResponses(int)
                  *
                  *  Preconditions:
                  *    this.approvedResponses != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       229      return getRecentApprovedResponses(number);
       230    }
       231
       232    /**
       233     * Gets the most recent N approved responses.
       234     *
       235     * @return  a List of response IDs
       236     */
       237    public List<String> getRecentApprovedResponses(int number) {
                 /*
        P/P       *  Method: List getRecentApprovedResponses(int)
                  *
                  *  Preconditions:
                  *    this.approvedResponses != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       238      if (approvedResponses.size() >= number) {
       239        return approvedResponses.subList(0, number);
       240      } else {
       241        return approvedResponses;
       242      }
       243    }
       244
       245    /**
       246     * Gets the list of approved responses.
       247     *
       248     * @return  a List of response IDs
       249     */
       250    public List<String> getApprovedResponses() {
                 /*
        P/P       *  Method: List getApprovedResponses()
                  *
                  *  Preconditions:
                  *    init'ed(this.approvedResponses)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getApprovedResponses#1)
                  *    new ArrayList(getApprovedResponses#1) num objects == 1
                  */
       251      return new ArrayList<String>(approvedResponses);
       252    }
       253
       254    /**
       255     * Gets the list of pending responses.
       256     *
       257     * @return  a List of response IDs
       258     */
       259    public List<String> getPendingResponses() {
                 /*
        P/P       *  Method: List getPendingResponses()
                  *
                  *  Preconditions:
                  *    init'ed(this.pendingResponses)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getPendingResponses#1)
                  *    new ArrayList(getPendingResponses#1) num objects == 1
                  */
       260      return new ArrayList<String>(pendingResponses);
       261    }
       262
       263    /**
       264     * Gets the list of rejected responses.
       265     *
       266     * @return  a List of response IDs
       267     */
       268    public List<String> getRejectedResponses() {
                 /*
        P/P       *  Method: List getRejectedResponses()
                  *
                  *  Preconditions:
                  *    init'ed(this.rejectedResponses)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getRejectedResponses#1)
                  *    new ArrayList(getRejectedResponses#1) num objects == 1
                  */
       269      return new ArrayList<String>(rejectedResponses);
       270    }
       271
       272  }

SofCheck Inspector Build Version : 2.22510

responseindex.java | 2010-Jun-25 19:40:32
responseindex.class | 2010-Jul-19 20:23:38