Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 204   Methods: 5
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
VectorOption.java 100% 98.5% 100% 99%
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.config;
 38   
 39    import java.util.Vector;
 40    import java.io.StreamTokenizer;
 41    import java.io.StringReader;
 42    import java.io.IOException;
 43   
 44    /**
 45    * Abstract class defining behavior shared by all
 46    * configuration options with values of type
 47    * Vector<T>.
 48    * VectorOption<String> now allows empty strings, i.e. "[,]" is a vector of two empty strings.
 49    * "[]" will be interpreted as a vector of one empty string, and "" is an empty vector.
 50    * @version $Id: VectorOption.java 5175 2010-01-20 08:46:32Z mgricken $
 51    */
 52    public class VectorOption<T> extends Option<Vector<T>> {
 53   
 54    protected ParseStrategy<T> parser;
 55    protected FormatStrategy<T> formatter;
 56    public final String header;
 57    public final char delim;
 58    public final String footer;
 59   
 60    /** @param key The name of this option.
 61    * @param parser the parsing strategy for an element in this option
 62    * @param formatter the formatting strategy for an element in this option
 63    */
 64  16857 private VectorOption(String key, ParseStrategy<T> parser, FormatStrategy<T> formatter,
 65    String header, char delim, String footer, Vector<T> def) {
 66  16857 super(key,def);
 67  16857 this.parser = parser;
 68  16857 this.formatter = formatter;
 69  16857 this.header = header;
 70  16857 this.delim = delim;
 71  16857 this.footer = footer;
 72    }
 73   
 74  234 public VectorOption(String key, Option<T> strategy, String header,
 75    char delim, String footer, Vector<T> def) {
 76  234 this(key, strategy, strategy, header, delim, footer,def);
 77    }
 78   
 79    /** Defaults the "header", "footer", and "delim" fields
 80    * to open bracket, close bracket, and comma, repsectively.
 81    * @param key The name of this option.
 82    * @param option The object that knows how to parse and format
 83    * an element of type T.
 84    */
 85  16623 public VectorOption(String key, Option<T> option, Vector<T> def) {
 86  16623 this(key,option,option,"[",',',"]",def);
 87    }
 88   
 89    /** @param s The String to be parsed.
 90    * @return An instance of Vector<T> represented by "s".
 91    * @exception IllegalArgumentException if "s" is not formatted
 92    * according to the method Vector<T>.toString().
 93    */
 94  169222 public Vector<T> parse(String s) {
 95  169222 s= s.trim();
 96  169222 Vector<T> res = new Vector<T>();
 97  55222 if (s.equals("")) { return res; }
 98   
 99  114000 int startFirstElement = header.length();
 100  114000 int startFooter = s.length() - footer.length();
 101   
 102  114000 if (!s.startsWith(header) && !s.endsWith(footer)) {
 103    // not formatted as a vector, try parsing this as a singleton vector
 104  18 res.add(parser.parse(s));
 105  17 return res;
 106    }
 107  113982 if (startFooter < startFirstElement || !s.startsWith(header) || ! s.endsWith(footer)) {
 108  2 throw new OptionParseException(name, s, "Value must start with " + header + " and end " + "with " + footer +
 109    " to be a valid vector.");
 110    }
 111  113980 s = s.substring(startFirstElement, startFooter);
 112  113980 if (s.equals("")) {
 113  1 res.add(parser.parse(""));
 114  1 return res;
 115    }
 116   
 117    // String d = String.valueOf(delim);
 118   
 119  113979 StreamTokenizer st = new StreamTokenizer(new StringReader(s));
 120  113979 st.resetSyntax();
 121  113979 st.wordChars(0,255);
 122  113979 st.ordinaryChar('|');
 123  113979 st.ordinaryChar(delim);
 124  113979 try {
 125  113979 int tok = st.nextToken();
 126  113979 int prevtok = -4;
 127  113979 StringBuilder sb = new StringBuilder();
 128  113979 while (tok!=StreamTokenizer.TT_EOF) {
 129  114043 if (tok=='|') {
 130  9 if (prevtok=='|') {
 131    // second pipe in a row, append a pipe to string builder
 132  2 sb.append('|');
 133  2 prevtok = tok = -4;
 134    }
 135    else {
 136    // first pipe, next token decides
 137  7 prevtok = tok;
 138    }
 139    }
 140  114034 else if (tok==delim) {
 141  34 if (prevtok=='|') {
 142    // pipe followed by delimiter --> escaped delimiter
 143    // append delimiter to string builder
 144  4 sb.append(delim);
 145  4 prevtok = tok = -4;
 146    }
 147    else {
 148    // no preceding pipe --> real delimiter
 149  30 res.add(parser.parse(sb.toString()));
 150  30 sb.setLength(0); // clear string builder
 151  30 prevtok = tok;
 152    }
 153    }
 154    else {
 155    // not a pipe or delimiter
 156  114000 if (prevtok=='|') {
 157    // backslash followed by neither a backslash nor a delimiter
 158    // invalid
 159  1 throw new OptionParseException(name, s, "A pipe | was discovered before the token '" + st.sval +
 160    "'. A pipe is only allowed in front of another pipe " +
 161    "or the delimiter " + delim + ".");
 162    }
 163  113999 sb.append(st.sval);
 164  113999 prevtok = tok;
 165    }
 166   
 167  114042 tok = st.nextToken();
 168    }
 169   
 170  113978 res.add(parser.parse(sb.toString()));
 171    }
 172    catch(IOException ioe) {
 173  0 throw new OptionParseException(name, s, "An IOException occurred while parsing a vector.");
 174    }
 175   
 176  113976 return res;
 177    }
 178   
 179    /** Formats the Vector v. The overall String format is determined by the method Vector<T>.tString(), but each
 180    * element of the vector is formatted by calling formatElement().
 181    * @param v The Vector to be formatted.
 182    * @return A String representing "v".
 183    */
 184  16969 public String format(Vector<T> v) {
 185  5614 if (v.size() == 0) { return ""; }
 186   
 187    // String d = String.valueOf(delim);
 188  11355 final StringBuilder res = new StringBuilder(header);
 189   
 190  11355 int size = v.size();
 191  11355 int i = 0;
 192  11355 while (i < size) {
 193  11370 String str = formatter.format(v.get(i));
 194  11370 str = str.replaceAll("\\|","||");
 195  11370 str = str.replaceAll(",","|,");
 196  11370 res.append(str);
 197  11370 i++;
 198  15 if (i < size) res.append(delim);
 199    }
 200  11355 String str = res.append(footer).toString();
 201  11355 return str;
 202    }
 203    }
 204