Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 301   Methods: 25
NCLOC: 146   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
History.java 83.3% 96.8% 100% 93.5%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.model.repl;
 38   
 39    import java.util.ArrayList;
 40    import java.util.HashMap;
 41    import edu.rice.cs.util.FileOps;
 42    import edu.rice.cs.util.StringOps;
 43    import edu.rice.cs.drjava.config.*;
 44    import edu.rice.cs.drjava.DrJava;
 45    import edu.rice.cs.drjava.model.*;
 46    import edu.rice.cs.util.OperationCanceledException;
 47   
 48    import java.io.Serializable;
 49    import java.io.IOException;
 50    import java.io.File;
 51    import java.io.OutputStreamWriter;
 52    import java.io.OutputStream;
 53    import java.io.BufferedWriter;
 54   
 55    /** History class that records what has been typed in the interactions pane. This class is not thread safe;
 56    * it is only accessed from InteractionsDocument which takes responsibility for synchronization.
 57    * @version $Id: History.java 5236 2010-04-27 01:43:36Z mgricken $
 58    */
 59    public class History implements OptionConstants, Serializable {
 60   
 61    public static final String INTERACTION_SEPARATOR = "//End of Interaction//";
 62   
 63    // Not final because it may be updated by config
 64    private volatile int _maxSize;
 65   
 66    /** Version flag at the beginning of saved history file format
 67    * If this is not present in a saved history, it is assumed to be the original format.
 68    */
 69    public static final String HISTORY_FORMAT_VERSION_2 = "// DrJava saved history v2" + StringOps.EOL;
 70   
 71    private final ArrayList<String> _vector = new ArrayList<String>();
 72    private volatile int _cursor = -1;
 73   
 74    /** A hashmap for edited entries in the history. */
 75    private final HashMap<Integer, String> _editedEntries = new HashMap<Integer, String>();
 76   
 77    /** A placeholder for the current search string. */
 78    private volatile String _currentSearchString = "";
 79   
 80    /** The OptionListener for HISTORY_MAX_SIZE */
 81    public final OptionListener<Integer> historyOptionListener = new OptionListener<Integer>() {
 82  5 public void optionChanged (OptionEvent<Integer> oce) {
 83  5 int newSize = oce.value;
 84    // System.err.println("optionChanged called for historyOptionListener; newSize = " + newSize);
 85  5 setMaxSize(newSize);
 86    }
 87  1 public String toString() { return "HISTORY_MAX_SIZE OptionListener #" + hashCode(); }
 88    };
 89   
 90   
 91    /** Constructor, so we can add a listener to the Config item being used. */
 92  21 public History() {
 93  21 this(DrJava.getConfig().getSetting(HISTORY_MAX_SIZE));
 94    // the reference to historyOptionListener below is delicate
 95  21 DrJava.getConfig().addOptionListener(HISTORY_MAX_SIZE, historyOptionListener);
 96    }
 97   
 98    /** Creates a new History with the given size. An option listener is not added for the config framework.
 99    * @param maxSize Number of lines to remember in the history.
 100    */
 101  214 public History(int maxSize) {
 102  1 if (maxSize < 0) maxSize = 0; // Sanity check on _maxSize
 103  214 _maxSize = maxSize;
 104    }
 105   
 106    /* Getter for historyOptionListener. */
 107  157 public OptionListener<Integer> getHistoryOptionListener() { return historyOptionListener; }
 108   
 109    /** Sets the edited entry to the given value.
 110    * @param entry the string to set
 111    */
 112  529 public void setEditedEntry(String entry) {
 113  9 if (! entry.equals(getCurrent())) _editedEntries.put(Integer.valueOf(_cursor), entry);
 114    }
 115   
 116    /** Adds an item to the history and moves the cursor to point to the place after it.
 117    * Note: Items are not inserted if they are empty. (This is in accordance with
 118    * bug #522123, but in divergence from feature #522213 which originally excluded
 119    * sequential duplicate entries from ever being stored.)
 120    *
 121    * Thus, to access the newly inserted item, you must movePrevious first.
 122    */
 123  691 public void add(String item) {
 124    // for consistency in saved History files, WILL save sequential duplicate entries
 125  691 if (item.trim().length() > 0) {
 126  690 _vector.add(item);
 127    // If max size of _vector is exceeded, spill the oldest element out of the History.
 128  101 if (_vector.size() > _maxSize) _vector.remove(0);
 129   
 130  690 moveEnd();
 131  690 _editedEntries.clear();
 132    }
 133    }
 134   
 135    /** Returns the last element and removes it, or returns null if the history is empty.
 136    * @return last element before it was removed, or null if history is empty
 137    */
 138  3 public String removeLast() {
 139  1 if (_vector.size() == 0) { return null; }
 140  2 String last = _vector.remove(_vector.size()-1);
 141  2 if (_cursor > _vector.size()) { _cursor = _vector.size()-1; }
 142  2 return last;
 143    }
 144   
 145    /** Move the cursor to just past the end. Thus, to access the last element, you must movePrevious. */
 146  931 public void moveEnd() { _cursor = _vector.size(); }
 147   
 148    /** Moves cursor back 1, or throws exception if there is none.
 149    * @param entry the current entry (perhaps edited from what is in history)
 150    */
 151  518 public void movePrevious(String entry) {
 152  1 if (! hasPrevious()) throw new ArrayIndexOutOfBoundsException();
 153  517 setEditedEntry(entry);
 154  517 _cursor--;
 155    }
 156   
 157    /** Returns the last entry from the history. Throw array indexing exception if no such entry. */
 158  1 public String lastEntry() { return _vector.get(_cursor - 1); }
 159   
 160    /** Moves cursor forward 1, or throws exception if there is none.
 161    * @param entry the current entry (perhaps edited from what is in history)
 162    */
 163  7 public void moveNext(String entry) {
 164  0 if (! hasNext()) throw new ArrayIndexOutOfBoundsException();
 165  7 setEditedEntry(entry);
 166  6 _cursor++;
 167    }
 168   
 169    /** Returns whether moveNext() would succeed right now. */
 170  561 public boolean hasNext() { return _cursor < (_vector.size()); }
 171   
 172    /** Returns whether movePrevious() would succeed right now. */
 173  1028 public boolean hasPrevious() { return _cursor > 0; }
 174   
 175    /** Returns item in history at current position; returns "" if no current item exists. */
 176  564 public String getCurrent() {
 177  564 Integer cursor = Integer.valueOf(_cursor);
 178  13 if (_editedEntries.containsKey(cursor)) return _editedEntries.get(cursor);
 179   
 180  537 if (hasNext()) return _vector.get(_cursor);
 181  14 return "";
 182    }
 183   
 184    /** Returns the number of items in this History. */
 185  19 public int size() { return _vector.size(); }
 186   
 187    /** Clears the vector */
 188  1 public void clear() { _vector.clear(); }
 189   
 190    /** Returns the history as a string by concatenating each string in the vector separated by the delimiting
 191    * character. A semicolon is added to the end of every statement that didn't already end with one.
 192    */
 193  4 public String getHistoryAsStringWithSemicolons() {
 194  4 final StringBuilder s = new StringBuilder();
 195  4 final String delimiter = INTERACTION_SEPARATOR + StringOps.EOL;
 196  4 for (int i = 0; i < _vector.size(); i++) {
 197  9 String nextLine = _vector.get(i);
 198    // int nextLength = nextLine.length();
 199    // if ((nextLength > 0) && (nextLine.charAt(nextLength-1) != ';')) {
 200    // nextLine += ";";
 201    // }
 202    // s += nextLine + delimiter;
 203  9 s.append(nextLine);
 204  9 s.append(delimiter);
 205    }
 206  4 return s.toString();
 207    }
 208   
 209    /** Returns the history as a string by concatenating the lines in _vector with EOL as separator. */
 210  5 public String getHistoryAsString() {
 211  5 final StringBuilder sb = new StringBuilder();
 212  5 final String delimiter = StringOps.EOL;
 213  7 for (String s: _vector) sb.append(s).append(delimiter);
 214  5 return sb.toString();
 215    }
 216   
 217    /** Writes this (unedited) History to the file selected in the FileSaveSelector.
 218    * @param selector File to save to
 219    */
 220  3 public void writeToFile(FileSaveSelector selector) throws IOException {
 221  3 writeToFile(selector, getHistoryAsStringWithSemicolons());
 222    }
 223   
 224    /** Writes this History to the file selected in the FileSaveSelector. The saved file will still include
 225    * any tags or extensions needed to recognize it as a saved interactions file.
 226    * @param selector File to save to
 227    * @param editedVersion The edited version of the text to be saved (which already uses proper EOL string)
 228    */
 229  3 public static void writeToFile(FileSaveSelector selector, final String editedVersion) throws IOException {
 230  3 File c;
 231   
 232  3 try { c = selector.getFile(); }
 233  0 catch (OperationCanceledException oce) { return; }
 234   
 235    // Make sure we ask before overwriting
 236  3 if (c != null) {
 237  2 if (! c.exists() || selector.verifyOverwrite(c)) {
 238  2 FileOps.DefaultFileSaver saver = new FileOps.DefaultFileSaver(c) {
 239  2 public void saveTo(OutputStream os) throws IOException {
 240   
 241  2 OutputStreamWriter osw = new OutputStreamWriter(os);
 242  2 BufferedWriter bw = new BufferedWriter(osw);
 243  2 String file = HISTORY_FORMAT_VERSION_2 + editedVersion;
 244  2 bw.write(file, 0, file.length());
 245  2 bw.close();
 246    }
 247    };
 248  2 FileOps.saveFile(saver);
 249    }
 250    }
 251    }
 252   
 253    /** Changes the maximum number of interactions remembered by this History.
 254    * @param newSize New number of interactions to remember.
 255    */
 256  7 public void setMaxSize(int newSize) {
 257  1 if (newSize < 0) newSize = 0; // Sanity check
 258   
 259    // Remove old elements if the new size is less than current size
 260  7 if (size() > newSize) {
 261   
 262  2 int numToDelete = size() - newSize;
 263  20 for (int i = 0; i < numToDelete; i++) { _vector.remove(0); }
 264   
 265  2 moveEnd();
 266    }
 267  7 _maxSize = newSize;
 268    }
 269   
 270    /** Reverse-searches the history for the previous matching string.
 271    * @param currentInteraction the current interaction
 272    */
 273  4 public void reverseSearch(String currentInteraction) {
 274  4 if (_currentSearchString.equals("") || ! currentInteraction.startsWith(_currentSearchString))
 275  3 _currentSearchString = currentInteraction;
 276   
 277  4 setEditedEntry(currentInteraction);
 278  4 while (hasPrevious()) {
 279  6 movePrevious(getCurrent());
 280  3 if (getCurrent().startsWith(_currentSearchString, 0)) break;
 281    }
 282   
 283  1 if (! getCurrent().startsWith(_currentSearchString, 0)) moveEnd();
 284    }
 285   
 286    /** Forward-searches the history for the next matching string.
 287    * @param currentInteraction the current interaction
 288    */
 289  1 public void forwardSearch(String currentInteraction) {
 290  1 if (_currentSearchString.equals("") || ! currentInteraction.startsWith(_currentSearchString))
 291  1 _currentSearchString = currentInteraction;
 292   
 293  1 setEditedEntry(currentInteraction);
 294  1 while (hasNext()) {
 295  1 moveNext(getCurrent());
 296  1 if (getCurrent().startsWith(_currentSearchString, 0)) break;
 297    }
 298   
 299  0 if (! getCurrent().startsWith(_currentSearchString, 0)) moveEnd();
 300    }
 301    }