diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index a5b4c939a..20b8ab97d 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6051,7 +6051,7 @@ public class PApplet extends Applet */ public XML loadXML(String filename, String options) { try { - return new XML(createInput(filename), options); + return new XML(createReader(filename), options); } catch (Exception e) { e.printStackTrace(); return null; @@ -6104,8 +6104,9 @@ public class PApplet extends Applet public JSONObject loadJSONObject(String filename) { - JSONTokener tokener = new JSONTokener(createReader(filename)); - return new JSONObject(tokener); + //JSONTokener tokener = new JSONTokener(createReader(filename)); + //return new JSONObject(tokener); + return new JSONObject(createReader(filename)); } @@ -6120,8 +6121,9 @@ public class PApplet extends Applet public JSONArray loadJSONArray(String filename) { - JSONTokener tokener = new JSONTokener(createReader(filename)); - return new JSONArray(tokener); +// JSONTokener tokener = new JSONTokener(createReader(filename)); +// return new JSONArray(tokener); + return new JSONArray(createReader(filename)); } diff --git a/core/src/processing/core/PGraphicsJava2D.java b/core/src/processing/core/PGraphicsJava2D.java index ec5ddc410..f41136dae 100644 --- a/core/src/processing/core/PGraphicsJava2D.java +++ b/core/src/processing/core/PGraphicsJava2D.java @@ -1463,7 +1463,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ { } else if (extension.equals("svgz")) { try { InputStream input = new GZIPInputStream(parent.createInput(filename)); - XML xml = new XML(input, options); + XML xml = new XML(PApplet.createReader(input), options); svg = new PShapeSVG(xml); } catch (Exception e) { e.printStackTrace(); diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index 5b7277126..d04b1f1f3 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -7,7 +7,7 @@ import java.util.Random; import processing.core.PApplet; -public class FloatList { +public class FloatList implements Iterable { int count; float[] data; @@ -550,9 +550,9 @@ public class FloatList { /** - * Returns the actual array being used to store the data. Suitable for - * iterating with a for() loop, but modifying the list could cause terrible - * things to happen. + * Returns the actual array being used to store the data. For advanced users, + * this is the fastest way to access a large list. Suitable for iterating + * with a for() loop, but modifying the list will have terrible consequences. */ public float[] values() { crop(); @@ -560,7 +560,13 @@ public class FloatList { } - public Iterator valueIterator() { + /** Implemented this way so that we can use a FloatList in a for loop. */ + @Override + public Iterator iterator() { +// } +// +// +// public Iterator valueIterator() { return new Iterator() { int index = -1; @@ -583,8 +589,8 @@ public class FloatList { * Create a new array with a copy of all the values. * @return an array sized by the length of the list with each of the values. */ - public int[] valueArray() { - return valueArray(null); + public int[] array() { + return array(null); } @@ -592,7 +598,7 @@ public class FloatList { * Copy as many values as possible into the specified array. * @param array */ - public int[] valueArray(int[] array) { + public int[] array(int[] array) { if (array == null || array.length != count) { array = new int[count]; } diff --git a/core/src/processing/data/IntHash.java b/core/src/processing/data/IntHash.java index 34d5991e4..16c8e4123 100644 --- a/core/src/processing/data/IntHash.java +++ b/core/src/processing/data/IntHash.java @@ -22,27 +22,27 @@ public class IntHash { private HashMap indices = new HashMap(); - /** - * Create a new object by counting the number of times each unique entry - * shows up in the specified String array. - */ - static public IntHash fromCount(String[] list) { - IntHash outgoing = new IntHash(); - for (String s : list) { - outgoing.inc(s); - } - outgoing.crop(); - return outgoing; - } - - - static public IntHash fromOrder(String[] list) { - IntHash outgoing = new IntHash(); - for (int i = 0; i < list.length; i++) { - outgoing.set(list[i], i); - } - return outgoing; - } +// /** +// * Create a new object by counting the number of times each unique entry +// * shows up in the specified String array. +// */ +// static public IntHash fromTally(String[] list) { +// IntHash outgoing = new IntHash(); +// for (String s : list) { +// outgoing.inc(s); +// } +// outgoing.crop(); +// return outgoing; +// } +// +// +// static public IntHash fromOrder(String[] list) { +// IntHash outgoing = new IntHash(); +// for (int i = 0; i < list.length; i++) { +// outgoing.set(list[i], i); +// } +// return outgoing; +// } public IntHash() { diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 5af43468f..e0f454452 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -17,7 +17,7 @@ import processing.core.PApplet; * sort() and shuffle() always act on the list itself. To get a sorted copy, * use list.copy().sort(). */ -public class IntList { +public class IntList implements Iterable { protected int count; protected int[] data; @@ -462,9 +462,9 @@ public class IntList { /** - * Returns the actual array being used to store the data. Suitable for - * iterating with a for() loop, but modifying the list could cause terrible - * things to happen. + * Returns the actual array being used to store the data. For advanced users, + * this is the fastest way to access a large list. Suitable for iterating + * with a for() loop, but modifying the list will have terrible consequences. */ public int[] values() { crop(); @@ -472,7 +472,9 @@ public class IntList { } - public Iterator valueIterator() { + @Override + public Iterator iterator() { +// public Iterator valueIterator() { return new Iterator() { int index = -1; @@ -495,8 +497,8 @@ public class IntList { * Create a new array with a copy of all the values. * @return an array sized by the length of the list with each of the values. */ - public int[] valueArray() { - return valueArray(null); + public int[] array() { + return array(null); } @@ -504,7 +506,7 @@ public class IntList { * Copy as many values as possible into the specified array. * @param array */ - public int[] valueArray(int[] array) { + public int[] array(int[] array) { if (array == null || array.length != count) { array = new int[count]; } diff --git a/core/src/processing/data/JSONArray.java b/core/src/processing/data/JSONArray.java index 8c633df36..5955bd4fd 100644 --- a/core/src/processing/data/JSONArray.java +++ b/core/src/processing/data/JSONArray.java @@ -38,6 +38,7 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Array; @@ -93,7 +94,6 @@ import processing.core.PApplet; */ public class JSONArray { - /** * The arrayList where the JSONArray's properties are kept. */ @@ -107,12 +107,18 @@ public class JSONArray { this.myArrayList = new ArrayList(); } + + public JSONArray(Reader reader) { + this(new JSONTokener(reader)); + } + + /** * Construct a JSONArray from a JSONTokener. * @param x A JSONTokener * @throws JSONException If there is a syntax error. */ - public JSONArray(JSONTokener x) { + protected JSONArray(JSONTokener x) { this(); if (x.nextClean() != '[') { throw new RuntimeException("A JSONArray text must start with '['"); diff --git a/core/src/processing/data/JSONObject.java b/core/src/processing/data/JSONObject.java index 92df70066..50803e53c 100644 --- a/core/src/processing/data/JSONObject.java +++ b/core/src/processing/data/JSONObject.java @@ -37,6 +37,7 @@ SOFTWARE. import java.io.File; import java.io.IOException; import java.io.PrintWriter; +import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Method; @@ -214,13 +215,18 @@ public class JSONObject { // } + public JSONObject(Reader reader) { + this(new JSONTokener(reader)); + } + + /** * Construct a JSONObject from a JSONTokener. * @param x A JSONTokener object containing the source string. * @throws JSONException If there is a syntax error in the source string * or a duplicated key. */ - public JSONObject(JSONTokener x) { + protected JSONObject(JSONTokener x) { this(); char c; String key; @@ -336,7 +342,7 @@ public class JSONObject { * @param bean An object that has getter methods that should be used * to make a JSONObject. */ - public JSONObject(Object bean) { + protected JSONObject(Object bean) { this(); this.populateMap(bean); } diff --git a/core/src/processing/data/JSONTokener.java b/core/src/processing/data/JSONTokener.java index 4bfa6fb1e..499f10a82 100644 --- a/core/src/processing/data/JSONTokener.java +++ b/core/src/processing/data/JSONTokener.java @@ -38,7 +38,7 @@ SOFTWARE. * @author JSON.org * @version 2012-02-16 */ -public class JSONTokener { +class JSONTokener { private long character; private boolean eof; diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index 8fb05a08a..3bee3f82b 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -7,7 +7,7 @@ import java.util.Random; import processing.core.PApplet; -public class StringList { +public class StringList implements Iterable { int count; String[] data; @@ -235,7 +235,7 @@ public class StringList { // same as splice - public void insert(int index, int[] values) { + public void insert(int index, String[] values) { if (index < 0) { throw new IllegalArgumentException("insert() index cannot be negative: it was " + index); } @@ -263,7 +263,7 @@ public class StringList { } - public void insert(int index, IntList list) { + public void insert(int index, StringList list) { insert(index, list.values()); } @@ -552,7 +552,13 @@ public class StringList { } - public Iterator valueIterator() { + @Override + public Iterator iterator() { +// return valueIterator(); +// } +// +// +// public Iterator valueIterator() { return new Iterator() { int index = -1; @@ -575,8 +581,8 @@ public class StringList { * Create a new array with a copy of all the values. * @return an array sized by the length of the list with each of the values. */ - public int[] valueArray() { - return valueArray(null); + public String[] array() { + return array(null); } @@ -584,11 +590,39 @@ public class StringList { * Copy as many values as possible into the specified array. * @param array */ - public int[] valueArray(int[] array) { + public String[] array(String[] array) { if (array == null || array.length != count) { - array = new int[count]; + array = new String[count]; } System.arraycopy(data, 0, array, 0, count); return array; } -} + + + /** Remove all non-unique entries. */ + public void unique() { + IntHash cheat = getTally(); + data = cheat.keyArray(); + count = cheat.size(); + } + + + /** Count the number of times each String entry is found in this list. */ + public IntHash getTally() { + IntHash outgoing = new IntHash(); + for (int i = 0; i < count; i++) { + outgoing.inc(data[i]); + } + return outgoing; + } + + + /** Create a dictionary associating each entry in this list to its index. */ + public IntHash getOrder() { + IntHash outgoing = new IntHash(); + for (int i = 0; i < count; i++) { + outgoing.set(data[i], i); + } + return outgoing; + } +} \ No newline at end of file diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 01ecd3cd0..e6960ffb2 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -2975,7 +2975,7 @@ public class Table { } } - void writeln(PrintWriter writer) throws IOException { + private void writeln(PrintWriter writer) throws IOException { for (String str : indexToData) { writer.println(str); } @@ -2998,63 +2998,67 @@ public class Table { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - class HashMapSucks extends HashMap { - - void increment(String what) { - Integer value = get(what); - if (value == null) { - put(what, 1); - } else { - put(what, value + 1); - } - } - - void check(String what) { - if (get(what) == null) { - put(what, 0); - } - } - } +// class HashMapSucks extends HashMap { +// +// void increment(String what) { +// Integer value = get(what); +// if (value == null) { +// put(what, 1); +// } else { +// put(what, value + 1); +// } +// } +// +// void check(String what) { +// if (get(what) == null) { +// put(what, 0); +// } +// } +// } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - /** Not to be documented yet, API not complete. */ - public String[] listUnique(String column) { - return listUnique(getColumnIndex(column)); - } + // replaced with StringList.fromUnique(String[] list) +// public String[] listUnique(String column) { +// return listUnique(getColumnIndex(column)); +// } - /** Not to be documented yet, API not complete. */ - public String[] listUnique(int column) { - HashMapSucks found = new HashMapSucks(); - for (int row = 0; row < getRowCount(); row++) { - found.check(getString(row, column)); - } - String[] outgoing = new String[found.size()]; - found.keySet().toArray(outgoing); - return outgoing; - } +// IntHash found = IntHash.fromTally(getStringColumn(column)); +// return found.keyArray(); + + // replaced with StringList.fromUnique(String[] list) +// public String[] listUnique(int column) { +// HashMapSucks found = new HashMapSucks(); +// +// for (int row = 0; row < getRowCount(); row++) { +// found.check(getString(row, column)); +// } +// String[] outgoing = new String[found.size()]; +// found.keySet().toArray(outgoing); +// return outgoing; +// } - /** Not to be documented yet, API not complete. */ - public HashMap tallyUnique(String columnName) { - return tallyUnique(getColumnIndex(columnName)); - } + // replaced with StringDict.fromTally(String[] list) +// public HashMap tallyUnique(String columnName) { +// return tallyUnique(getColumnIndex(columnName)); +// } - /** Not to be documented yet, API not complete. */ - public HashMap tallyUnique(int column) { - HashMapSucks outgoing = new HashMapSucks(); - for (int row = 0; row < rowCount; row++) { - String entry = getString(row, column); - if (entry != null) { - outgoing.increment(entry); - } - } - return outgoing; - } + // replaced with StringDict.fromTally(String[] list) +// public HashMap tallyUnique(int column) { +// HashMapSucks outgoing = new HashMapSucks(); +// for (int row = 0; row < rowCount; row++) { +// String entry = getString(row, column); +// if (entry != null) { +// outgoing.increment(entry); +// } +// } +// return outgoing; +// } /** diff --git a/core/src/processing/data/TableODS.java b/core/src/processing/data/TableODS.java index 660c02ee2..10231c17f 100644 --- a/core/src/processing/data/TableODS.java +++ b/core/src/processing/data/TableODS.java @@ -63,7 +63,7 @@ public class TableODS extends Table { // protected void read(BufferedReader reader, String worksheet, boolean actual) throws IOException, ParserConfigurationException, SAXException { // XML xml = new XML(reader); protected void read(InputStream input, String worksheet, boolean actual) throws IOException, ParserConfigurationException, SAXException { - XML xml = new XML(input); + XML xml = new XML(PApplet.createReader(input)); // XML x = new XML(reader); // PApplet.saveStrings(new File("/Users/fry/Desktop/namespacefix.xml"), new String[] { xml.toString() }); diff --git a/core/src/processing/data/XML.java b/core/src/processing/data/XML.java index 71711e8b5..53beb8018 100644 --- a/core/src/processing/data/XML.java +++ b/core/src/processing/data/XML.java @@ -95,20 +95,25 @@ public class XML implements Serializable { } - /** - * @param input description TBD - */ - public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException { - this(input, null); +// /** +// * @param input description TBD +// */ +// public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException { +// this(input, null); +// } + + +// public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException { +// this(PApplet.createReader(input), options); +// } + + + public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException { + this(reader, null); } - public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException { - this(PApplet.createReader(input), options); - } - - - protected XML(Reader reader, String options) throws IOException, ParserConfigurationException, SAXException { + public XML(Reader reader, String options) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Prevent 503 errors from www.w3.org @@ -191,17 +196,21 @@ public class XML implements Serializable { } - protected boolean save(OutputStream output) { - return save(PApplet.createWriter(output)); - } +// protected boolean save(OutputStream output) { +// return write(PApplet.createWriter(output)); +// } public boolean save(File file, String options) { - return save(PApplet.createWriter(file)); + PrintWriter writer = PApplet.createWriter(file); + boolean result = write(writer); + writer.flush(); + writer.close(); + return result; } - public boolean save(PrintWriter output) { + public boolean write(PrintWriter output) { output.print(format(2)); output.flush(); return true; diff --git a/core/src/processing/opengl/PGraphics2D.java b/core/src/processing/opengl/PGraphics2D.java index 21a516554..a1ad432d8 100644 --- a/core/src/processing/opengl/PGraphics2D.java +++ b/core/src/processing/opengl/PGraphics2D.java @@ -253,7 +253,7 @@ public class PGraphics2D extends PGraphicsOpenGL { try { InputStream input = new GZIPInputStream(pg.parent.createInput(filename)); - XML xml = new XML(input); + XML xml = new XML(PApplet.createReader(input)); svg = new PShapeSVG(xml); } catch (Exception e) { e.printStackTrace(); diff --git a/core/todo.txt b/core/todo.txt index 7e8e87924..a0f5250f7 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -24,6 +24,12 @@ _ rsort(), sortReverse(), sortKeysReverse, _ sortDescend, sortDescending, sortKeysDescending, _ sortHighLow, sortHigh, sortHighest, sortDown +it's going to be File or Reader (mostly BufferedReader) everywhere + though TableODS needs an InputStream... + +setMissingXxxx() -> should this live in PApplet? be static? + cons: static stinks, diff tables might use diff values + "hash.toJSONObject()" or "new JSONObject(hash)" hash/dict/etc