Skip to content

Commit

Permalink
This revision adds files involving ArrayListOption that should have
Browse files Browse the repository at this point in the history
been committed a year ago when I replaced VectorOption with
ArrayListOption.  Evidently, no one else attempted to build DrScala in the last
year until Nick Vrvilo noticed the omission.

The following files were modified, added, or deleted from the repository:

	new file:   src/edu/rice/cs/drjava/config/ArrayListOption.java
	new file:   src/edu/rice/cs/drjava/config/ArrayListOptionTest.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListAbsRelFileOptionComponent.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListClassnameOptionComponent.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListFileOptionComponent.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListFileOptionComponentTest.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListKeyStrokeOptionComponent.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListKeyStrokeOptionComponentTest.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListOptionComponent.java
	new file:   src/edu/rice/cs/drjava/ui/config/ArrayListStringOptionComponent.java
	new file:   src/edu/rice/cs/drjava/ui/icons/drscala256-opt.png
  • Loading branch information
ponderosaTX committed Aug 13, 2017
1 parent 7d4afa7 commit 95c2127
Show file tree
Hide file tree
Showing 11 changed files with 1,926 additions and 0 deletions.
201 changes: 201 additions & 0 deletions drjava/src/edu/rice/cs/drjava/config/ArrayListOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*BEGIN_COPYRIGHT_BLOCK
*
* Copyright (c) 2001-2015, JavaPLT group at Rice University (drjava@rice.edu)
* 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 names of DrJava, DrScala, the JavaPLT group, Rice University, 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.
*
* This software is Open Source Initiative approved Open Source Software.
* Open Source Initative Approved is a trademark of the Open Source Initiative.
*
* This file is part of DrScala. Download the current version of this project
* from http://www.drscala.org/.
*
* END_COPYRIGHT_BLOCK*/

package edu.rice.cs.drjava.config;

import java.util.ArrayList;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.io.IOException;

/** Abstract class defining behavior shared by all configuration options with values of type ArrayList<T>.
* ArrayListOption<String> now allows empty strings, i.e. "[,]" is a vector of two empty strings.
* "[]" will be interpreted as a vector of one empty string, and "" is an empty vector.
* @version $Id: ArrayListOption.java 5594 2012-06-21 11:23:40Z rcartwright $
*/
public class ArrayListOption<T> extends Option<ArrayList<T>> {

protected ParseStrategy<T> parser;
protected FormatStrategy<T> formatter;
public final String header;
public final char delim;
public final String footer;

/** @param key The name of this option.
* @param parser the parsing strategy for an element in this option
* @param formatter the formatting strategy for an element in this option
*/
private ArrayListOption(String key, ParseStrategy<T> parser, FormatStrategy<T> formatter,
String header, char delim, String footer, ArrayList<T> def) {
super(key,def);
this.parser = parser;
this.formatter = formatter;
this.header = header;
this.delim = delim;
this.footer = footer;
}

public ArrayListOption(String key, Option<T> strategy, String header,
char delim, String footer, ArrayList<T> def) {
this(key, strategy, strategy, header, delim, footer,def);
}

/** Defaults the "header", "footer", and "delim" fields
* to open bracket, close bracket, and comma, repsectively.
* @param key The name of this option.
* @param option The object that knows how to parse and format
* an element of type T.
*/
public ArrayListOption(String key, Option<T> option, ArrayList<T> def) {
this(key,option,option,"[",',',"]",def);
}

/** @param s The String to be parsed.
* @return An instance of ArrayList<T> represented by "s".
* @exception IllegalArgumentException if "s" is not formatted
* according to the method ArrayList<T>.toString().
*/
public ArrayList<T> parse(String s) {
s= s.trim();
ArrayList<T> res = new ArrayList<T>();
if (s.equals("")) { return res; }

int startFirstElement = header.length();
int startFooter = s.length() - footer.length();

if (!s.startsWith(header) && !s.endsWith(footer)) {
// not formatted as a vector, try parsing this as a singleton vector
res.add(parser.parse(s));
return res;
}
if (startFooter < startFirstElement || !s.startsWith(header) || ! s.endsWith(footer)) {
throw new OptionParseException(name, s, "Value must start with " + header + " and end " + "with " + footer +
" to be a valid vector.");
}
s = s.substring(startFirstElement, startFooter);
if (s.equals("")) {
res.add(parser.parse(""));
return res;
}

// String d = String.valueOf(delim);

StreamTokenizer st = new StreamTokenizer(new StringReader(s));
st.resetSyntax();
st.wordChars(0,255);
st.ordinaryChar('|');
st.ordinaryChar(delim);
try {
int tok = st.nextToken();
int prevtok = -4;
StringBuilder sb = new StringBuilder();
while (tok!=StreamTokenizer.TT_EOF) {
if (tok=='|') {
if (prevtok=='|') {
// second pipe in a row, append a pipe to string builder
sb.append('|');
prevtok = tok = -4;
}
else {
// first pipe, next token decides
prevtok = tok;
}
}
else if (tok==delim) {
if (prevtok=='|') {
// pipe followed by delimiter --> escaped delimiter
// append delimiter to string builder
sb.append(delim);
prevtok = tok = -4;
}
else {
// no preceding pipe --> real delimiter
res.add(parser.parse(sb.toString()));
sb.setLength(0); // clear string builder
prevtok = tok;
}
}
else {
// not a pipe or delimiter
if (prevtok=='|') {
// backslash followed by neither a backslash nor a delimiter
// invalid
throw new OptionParseException(name, s, "A pipe | was discovered before the token '" + st.sval +
"'. A pipe is only allowed in front of another pipe " +
"or the delimiter " + delim + ".");
}
sb.append(st.sval);
prevtok = tok;
}

tok = st.nextToken();
}

res.add(parser.parse(sb.toString()));
}
catch(IOException ioe) {
throw new OptionParseException(name, s, "An IOException occurred while parsing a vector.");
}

return res;
}

/** Formats the ArrayList v. The overall String format is determined by the method ArrayList<T>.tString(), but each
* element of the vector is formatted by calling formatElement().
* @param v The ArrayList to be formatted.
* @return A String representing "v".
*/
public String format(ArrayList<T> v) {
if (v.size() == 0) { return ""; }

// String d = String.valueOf(delim);
final StringBuilder res = new StringBuilder(header);

int size = v.size();
int i = 0;
while (i < size) {
String str = formatter.format(v.get(i));
str = str.replaceAll("\\|","||");
str = str.replaceAll(",","|,");
res.append(str);
i++;
if (i < size) res.append(delim);
}
String str = res.append(footer).toString();
return str;
}
}

185 changes: 185 additions & 0 deletions drjava/src/edu/rice/cs/drjava/config/ArrayListOptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*BEGIN_COPYRIGHT_BLOCK
*
* Copyright (c) 2001-2015, JavaPLT group at Rice University (drjava@rice.edu)
* 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 names of DrJava, DrScala, the JavaPLT group, Rice University, 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.
*
* This software is Open Source Initiative approved Open Source Software.
* Open Source Initative Approved is a trademark of the Open Source Initiative.
*
* This file is part of DrScala. Download the current version of this project
* from http://www.drscala.org/.
*
* END_COPYRIGHT_BLOCK*/

package edu.rice.cs.drjava.config;

import edu.rice.cs.drjava.DrScalaTestCase;

import java.util.ArrayList;


/** Class according to the JUnit protocol. Tests the proper functionality of the class ArrayListOption.
* @version $Id: ArrayListOptionTest.java 5594 2012-06-21 11:23:40Z rcartwright $
*/
public final class ArrayListOptionTest extends DrScalaTestCase {
private ArrayListOption<String> _svo;
private ArrayListOption<Integer> _ivo;
private ArrayListOption<Boolean> _bvo;

public void setUp() throws Exception {
super.setUp();
// name fields are irrelevant at this point.
_svo = new ArrayListOption<String>("whatever", new StringOption("", null), (ArrayList<String>) null);
_ivo = new ArrayListOption<Integer>("something", new IntegerOption("", null), (ArrayList<Integer>) null);
_bvo = new ArrayListOption<Boolean>("everwhat", new BooleanOption("", null), (ArrayList<Boolean>) null);
}

public void testGetName() {
assertEquals("whatever", _svo.getName());
assertEquals("everwhat", _bvo.getName());
}

public void testParse() {
assertTrue(_svo.parse("").isEmpty());
assertTrue(_bvo.parse("").isEmpty());

ArrayList<String> v = _svo.parse("[]");
assertEquals(1, v.size());
assertEquals("", v.get(0));

v = _svo.parse("[x]");
assertEquals(1, v.size());
assertEquals("x", v.get(0));

v = _svo.parse("[||]");
assertEquals(1, v.size());
assertEquals("|", v.get(0));

v = _svo.parse("[|,]");
assertEquals(1, v.size());
assertEquals(",", v.get(0));

v = _svo.parse("[|,,]");
assertEquals(2, v.size());
assertEquals(",", v.get(0));
assertEquals("", v.get(1));

v = _svo.parse("[,]");
assertEquals(2, v.size());
assertEquals("", v.get(0));
assertEquals("", v.get(1));

try { _svo.parse("[|x]"); fail("Pipe not in front of another pipe or delimiter."); }
catch (OptionParseException e) { }

try { _svo.parse("[11"); fail("Missing footer."); }
catch (OptionParseException e) { }

v = _svo.parse("[11,]");
assertEquals(2, v.size());
assertEquals("11", v.get(0));
assertEquals("", v.get(1));

try { _svo.parse("11]"); fail("Missing header."); }
catch (OptionParseException e) { }

v = _svo.parse("[11,,22]");
assertEquals(3, v.size());
assertEquals("11", v.get(0));
assertEquals("", v.get(1));
assertEquals("22", v.get(2));

v = _svo.parse("[11,|,,22]");
assertEquals(3, v.size());
assertEquals("11", v.get(0));
assertEquals(",", v.get(1));
assertEquals("22", v.get(2));

v = _svo.parse("[11,abc|,def,22]");
assertEquals(3, v.size());
assertEquals("11", v.get(0));
assertEquals("abc,def", v.get(1));
assertEquals("22", v.get(2));

v = _svo.parse("[11,||,22]");
assertEquals(3, v.size());
assertEquals("11", v.get(0));
assertEquals("|", v.get(1));
assertEquals("22", v.get(2));

// parsing this as a vector of strings is okay, because it will treat it
// as a singleton vector
v = _svo.parse("{11,22}");
assertEquals(1, v.size());
assertEquals("{11,22}", v.get(0));

// but parsing this as a vector of integers will fail
try { _ivo.parse("{11,22}"); fail("Should not have parsed this as singleton list."); }
catch (OptionParseException e) { }

ArrayList<Boolean> bv = _bvo.parse("[true]");

assertEquals(1, bv.size());
assertEquals(Boolean.TRUE, bv.get(0));

bv = _bvo.parse("[true,false,true,true]");

assertEquals(4, bv.size());
assertEquals(Boolean.TRUE, bv.get(0));
assertEquals(Boolean.FALSE, bv.get(1));
assertEquals(Boolean.TRUE, bv.get(2));
assertEquals(Boolean.TRUE, bv.get(3));

try { _bvo.parse("[11]"); fail("Number instead of boolean."); }
catch (OptionParseException e) { }

try { _bvo.parse("[true;false]"); fail("Illegal delimiter."); }
catch (OptionParseException e) { }
}

public void testFormat() {
ArrayList<String> sv = new ArrayList<String>();
assertEquals("", _svo.format(sv));

sv.add("");
assertEquals("[]", _svo.format(sv));

sv.add("-33");
assertEquals("[,-33]", _svo.format(sv));

sv.add("2");
assertEquals("[,-33,2]", _svo.format(sv));

sv.add("");
assertEquals("[,-33,2,]", _svo.format(sv));

sv.add(",");
assertEquals("[,-33,2,,|,]", _svo.format(sv));

sv.add("0");
assertEquals("[,-33,2,,|,,0]", _svo.format(sv));
}
}
Loading

0 comments on commit 95c2127

Please sign in to comment.