//# 0 errors, 144 messages
//#
/*
    //#emailsubscriptionlist.java:1:1: class: net.sourceforge.pebble.index.EmailSubscriptionList
 * Copyright (c) 2003-2006, Simon Brown
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   - Neither the name of Pebble nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package net.sourceforge.pebble.index;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sourceforge.pebble.domain.Blog;
import net.sourceforge.pebble.domain.Tag;
import net.sourceforge.pebble.domain.BlogEntry;

import java.util.*;
import java.io.*;

/**
 * Represents the tag index for a blog.
 *
 * @author    Simon Brown
 */
public class EmailSubscriptionList {

  private static final Log log = LogFactory.getLog(EmailSubscriptionList.class);
    //#emailsubscriptionlist.java:50: method: net.sourceforge.pebble.index.EmailSubscriptionList.net.sourceforge.pebble.index.EmailSubscriptionList__static_init
    //#emailsubscriptionlist.java:50: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.index.EmailSubscriptionList
    //#    method: net.sourceforge.pebble.index.EmailSubscriptionList__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Descendant_Table[net/sourceforge/pebble/index/EmailSubscriptionList]
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.addEmailAddress(Ljava/lang/String;)V
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.clear()V
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.getEmailAddresses()Ljava/util/List;
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.readIndex()V
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.removeEmailAddress(Ljava/lang/String;)V
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.writeIndex()V
    //#output(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): log
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Descendant_Table[net/sourceforge/pebble/index/EmailSubscriptionList] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.addEmailAddress(Ljava/lang/String;)V == &addEmailAddress
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.clear()V == &clear
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.getEmailAddresses()Ljava/util/List; == &getEmailAddresses
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.readIndex()V == &readIndex
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.removeEmailAddress(Ljava/lang/String;)V == &removeEmailAddress
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): __Dispatch_Table.writeIndex()V == &writeIndex
    //#post(net.sourceforge.pebble.index.EmailSubscriptionList__static_init): init'ed(log)
    //#emailsubscriptionlist.java:50: end of method: net.sourceforge.pebble.index.EmailSubscriptionList.net.sourceforge.pebble.index.EmailSubscriptionList__static_init

  private Blog blog;

  /** the list of e-mail addresses */
  private List<String> emailAddresses = new LinkedList<String>();

  public EmailSubscriptionList(Blog blog) {
    //#emailsubscriptionlist.java:57: method: void net.sourceforge.pebble.index.EmailSubscriptionList.net.sourceforge.pebble.index.EmailSubscriptionList(Blog)
    //#input(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): blog
    //#input(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): log
    //#input(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): this
    //#output(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): new LinkedList(EmailSubscriptionList#1) num objects
    //#output(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): this.blog
    //#output(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): this.emailAddresses
    //#new obj(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): new LinkedList(EmailSubscriptionList#1)
    //#pre[1] (void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): blog != null
    //#post(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): this.blog == blog
    //#post(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): this.blog != null
    //#post(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): this.emailAddresses == &new LinkedList(EmailSubscriptionList#1)
    //#post(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): new LinkedList(EmailSubscriptionList#1) num objects == 1
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:net.sourceforge.pebble.domain.Blog:getIndexesDirectory
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.io.File
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.io.File:exists
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.io.FileReader
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.io.BufferedReader
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.io.BufferedReader:readLine
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.util.List:add
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:java.io.BufferedReader:close
    //#unanalyzed(void net.sourceforge.pebble.index.EmailSubscriptionList(Blog)): Effects-of-calling:org.apache.commons.logging.Log:error
    this.blog = blog;

    readIndex();
  }
    //#emailsubscriptionlist.java:61: end of method: void net.sourceforge.pebble.index.EmailSubscriptionList.net.sourceforge.pebble.index.EmailSubscriptionList(Blog)

  /**
   * Clears the index.
   */
  public void clear() {
    emailAddresses = new LinkedList<String>();
    //#emailsubscriptionlist.java:67: method: void net.sourceforge.pebble.index.EmailSubscriptionList.clear()
    //#input(void clear()): log
    //#input(void clear()): this
    //#input(void clear()): this.blog
    //#output(void clear()): new LinkedList(clear#1) num objects
    //#output(void clear()): this.emailAddresses
    //#new obj(void clear()): new LinkedList(clear#1)
    //#pre[2] (void clear()): (soft) this.blog != null
    //#post(void clear()): this.emailAddresses == &new LinkedList(clear#1)
    //#post(void clear()): new LinkedList(clear#1) num objects == 1
    //#unanalyzed(void clear()): Effects-of-calling:net.sourceforge.pebble.domain.Blog:getIndexesDirectory
    //#unanalyzed(void clear()): Effects-of-calling:java.io.File
    //#unanalyzed(void clear()): Effects-of-calling:org.apache.commons.logging.Log:error
    //#unanalyzed(void clear()): Effects-of-calling:java.io.FileWriter
    //#unanalyzed(void clear()): Effects-of-calling:java.io.BufferedWriter
    //#unanalyzed(void clear()): Effects-of-calling:java.util.List:iterator
    //#unanalyzed(void clear()): Effects-of-calling:java.util.Iterator:hasNext
    //#unanalyzed(void clear()): Effects-of-calling:java.util.Iterator:next
    //#unanalyzed(void clear()): Effects-of-calling:java.io.BufferedWriter:write
    //#unanalyzed(void clear()): Effects-of-calling:java.io.BufferedWriter:newLine
    //#unanalyzed(void clear()): Effects-of-calling:java.io.BufferedWriter:flush
    //#unanalyzed(void clear()): Effects-of-calling:java.io.BufferedWriter:close
    writeIndex();
  }
    //#emailsubscriptionlist.java:69: end of method: void net.sourceforge.pebble.index.EmailSubscriptionList.clear()

  /**
   * Adds an e-mail address.
   *
   * @param emailAddress    an e-mail address
   */
  public synchronized void addEmailAddress(String emailAddress) {
    if (!emailAddresses.contains(emailAddress)) {
    //#emailsubscriptionlist.java:77: method: void net.sourceforge.pebble.index.EmailSubscriptionList.addEmailAddress(String)
    //#input(void addEmailAddress(String)): emailAddress
    //#input(void addEmailAddress(String)): log
    //#input(void addEmailAddress(String)): this
    //#input(void addEmailAddress(String)): this.blog
    //#input(void addEmailAddress(String)): this.emailAddresses
    //#pre[4] (void addEmailAddress(String)): this.emailAddresses != null
    //#pre[3] (void addEmailAddress(String)): (soft) this.blog != null
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:net.sourceforge.pebble.domain.Blog:getIndexesDirectory
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.File
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:org.apache.commons.logging.Log:error
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.FileWriter
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.util.List:iterator
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.util.Iterator:hasNext
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.util.Iterator:next
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:write
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:newLine
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:flush
    //#unanalyzed(void addEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:close
    //#test_vector(void addEmailAddress(String)): java.util.List:contains(...)@77: {1}, {0}
      emailAddresses.add(emailAddress);
      writeIndex();
    }
  }
    //#emailsubscriptionlist.java:81: end of method: void net.sourceforge.pebble.index.EmailSubscriptionList.addEmailAddress(String)

  /**
   * Removes an e-mail address.
   *
   * @param emailAddress    an e-mail address
   */
  public synchronized void removeEmailAddress(String emailAddress) {
    emailAddresses.remove(emailAddress);
    //#emailsubscriptionlist.java:89: method: void net.sourceforge.pebble.index.EmailSubscriptionList.removeEmailAddress(String)
    //#input(void removeEmailAddress(String)): emailAddress
    //#input(void removeEmailAddress(String)): log
    //#input(void removeEmailAddress(String)): this
    //#input(void removeEmailAddress(String)): this.blog
    //#input(void removeEmailAddress(String)): this.emailAddresses
    //#pre[4] (void removeEmailAddress(String)): this.emailAddresses != null
    //#pre[3] (void removeEmailAddress(String)): (soft) this.blog != null
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:net.sourceforge.pebble.domain.Blog:getIndexesDirectory
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.File
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:org.apache.commons.logging.Log:error
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.FileWriter
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.util.List:iterator
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.util.Iterator:hasNext
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.util.Iterator:next
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:write
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:newLine
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:flush
    //#unanalyzed(void removeEmailAddress(String)): Effects-of-calling:java.io.BufferedWriter:close
    writeIndex();
  }
    //#emailsubscriptionlist.java:91: end of method: void net.sourceforge.pebble.index.EmailSubscriptionList.removeEmailAddress(String)

  /**
   * Helper method to load the index.
   */
  private void readIndex() {
    File indexFile = new File(blog.getIndexesDirectory(), "email-subscriptions.index");
    //#emailsubscriptionlist.java:97: method: void net.sourceforge.pebble.index.EmailSubscriptionList.readIndex()
    //#emailsubscriptionlist.java:97: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.Blog:getIndexesDirectory()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.index.EmailSubscriptionList
    //#    method: void readIndex()
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.Blog:getIndexesDirectory()
    //#input(void readIndex()): log
    //#input(void readIndex()): this
    //#input(void readIndex()): this.blog
    //#input(void readIndex()): this.emailAddresses
    //#pre[2] (void readIndex()): this.blog != null
    //#pre[3] (void readIndex()): (soft) this.emailAddresses != null
    //#presumption(void readIndex()): org.apache.commons.logging.LogFactory:getLog(...)@50 != null
    //#test_vector(void readIndex()): java.io.File:exists(...)@98: {0}, {1}
    if (indexFile.exists()) {
      try {
        BufferedReader reader = new BufferedReader(new FileReader(indexFile));
        String emailAddress = reader.readLine();
        while (emailAddress != null) {
          emailAddresses.add(emailAddress);
          emailAddress = reader.readLine();
        }

        reader.close();
      } catch (Exception e) {
        log.error("Error while reading index", e);
    //#emailsubscriptionlist.java:109: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:error(Object, Throwable)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.index.EmailSubscriptionList
    //#    method: void readIndex()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:error(Object, Throwable)
      }
    }
  }
    //#emailsubscriptionlist.java:112: end of method: void net.sourceforge.pebble.index.EmailSubscriptionList.readIndex()

  /**
   * Helper method to write out the index to disk.
   */
  private void writeIndex() {
    try {
      File indexFile = new File(blog.getIndexesDirectory(), "email-subscriptions.index");
    //#emailsubscriptionlist.java:119: method: void net.sourceforge.pebble.index.EmailSubscriptionList.writeIndex()
    //#emailsubscriptionlist.java:119: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.Blog:getIndexesDirectory()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.index.EmailSubscriptionList
    //#    method: void writeIndex()
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.Blog:getIndexesDirectory()
    //#input(void writeIndex()): log
    //#input(void writeIndex()): this
    //#input(void writeIndex()): this.blog
    //#input(void writeIndex()): this.emailAddresses
    //#pre[2] (void writeIndex()): (soft) this.blog != null
    //#pre[3] (void writeIndex()): (soft) this.emailAddresses != null
    //#presumption(void writeIndex()): org.apache.commons.logging.LogFactory:getLog(...)@50 != null
    //#test_vector(void writeIndex()): java.util.Iterator:hasNext(...)@122: {1}, {0}
      BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));

      for (String emailAddress : emailAddresses) {
        writer.write(emailAddress);
        writer.newLine();
      }

      writer.flush();
      writer.close();
    } catch (Exception e) {
      log.error("Error while writing index", e);
    //#emailsubscriptionlist.java:130: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:error(Object, Throwable)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.index.EmailSubscriptionList
    //#    method: void writeIndex()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:error(Object, Throwable)
    }
  }
    //#emailsubscriptionlist.java:132: end of method: void net.sourceforge.pebble.index.EmailSubscriptionList.writeIndex()

  /**
   * Gets the list of e-mail addresses.
   */
  public List<String> getEmailAddresses() {
    List list = new ArrayList<String>(emailAddresses);
    //#emailsubscriptionlist.java:138: method: List net.sourceforge.pebble.index.EmailSubscriptionList.getEmailAddresses()
    //#input(List getEmailAddresses()): this
    //#input(List getEmailAddresses()): this.emailAddresses
    //#output(List getEmailAddresses()): new ArrayList(getEmailAddresses#1) num objects
    //#output(List getEmailAddresses()): return_value
    //#new obj(List getEmailAddresses()): new ArrayList(getEmailAddresses#1)
    //#pre[2] (List getEmailAddresses()): init'ed(this.emailAddresses)
    //#post(List getEmailAddresses()): return_value == &new ArrayList(getEmailAddresses#1)
    //#post(List getEmailAddresses()): new ArrayList(getEmailAddresses#1) num objects == 1
    Collections.sort(list);
    return list;
    //#emailsubscriptionlist.java:140: end of method: List net.sourceforge.pebble.index.EmailSubscriptionList.getEmailAddresses()
  }

}
    //#emailsubscriptionlist.java:: end of class: net.sourceforge.pebble.index.EmailSubscriptionList
