Skip to content

Commit 0938465

Browse files
benfryomerjerk
authored andcommitted
Merge changes from main processing repo
Signed-off-by: Umair Khan <[email protected]>
1 parent 8aaecdc commit 0938465

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

core/src/processing/data/StringList.java

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ public StringList(String[] list) {
4444
}
4545

4646

47+
/**
48+
* Construct a StringList from a random pile of objects. Null values will
49+
* stay null, but all the others will be converted to String values.
50+
*/
51+
public StringList(Object... items) {
52+
count = items.length;
53+
data = new String[count];
54+
int index = 0;
55+
for (Object o : items) {
56+
// // Not gonna go with null values staying that way because perhaps
57+
// // the most common case here is to immediately call join() or similar.
58+
// data[index++] = String.valueOf(o);
59+
// Keep null values null (because join() will make non-null anyway)
60+
if (o != null) { // leave null values null
61+
data[index] = o.toString();
62+
}
63+
index++;
64+
}
65+
}
66+
67+
4768
/**
4869
* Create from something iterable, for instance:
4970
* StringList list = new StringList(hashMap.keySet());
@@ -113,6 +134,9 @@ public void clear() {
113134
* @brief Get an entry at a particular index
114135
*/
115136
public String get(int index) {
137+
if (index >= count) {
138+
throw new ArrayIndexOutOfBoundsException(index);
139+
}
116140
return data[index];
117141
}
118142

@@ -137,6 +161,22 @@ public void set(int index, String what) {
137161
}
138162

139163

164+
/** Just an alias for append(), but matches pop() */
165+
public void push(String value) {
166+
append(value);
167+
}
168+
169+
170+
public String pop() {
171+
if (count == 0) {
172+
throw new RuntimeException("Can't call pop() on an empty list");
173+
}
174+
String value = get(count-1);
175+
data[--count] = null; // avoid leak
176+
return value;
177+
}
178+
179+
140180
/**
141181
* Remove an element from the specified index.
142182
*
@@ -274,6 +314,14 @@ public void append(StringList list) {
274314
}
275315

276316

317+
/** Add this value, but only if it's not already in the list. */
318+
public void appendUnique(String value) {
319+
if (!hasValue(value)) {
320+
append(value);
321+
}
322+
}
323+
324+
277325
// public void insert(int index, int value) {
278326
// if (index+1 > count) {
279327
// if (index+1 < data.length) {
@@ -304,12 +352,17 @@ public void append(StringList list) {
304352
// }
305353

306354

355+
public void insert(int index, String value) {
356+
insert(index, new String[] { value });
357+
}
358+
359+
307360
// same as splice
308361
public void insert(int index, String[] values) {
309362
if (index < 0) {
310363
throw new IllegalArgumentException("insert() index cannot be negative: it was " + index);
311364
}
312-
if (index >= values.length) {
365+
if (index >= data.length) {
313366
throw new IllegalArgumentException("insert() index " + index + " is past the end of this list");
314367
}
315368

@@ -495,7 +548,7 @@ public void swap(int a, int b) {
495548

496549
/**
497550
* @webref stringlist:method
498-
* @brief To come...
551+
* @brief Reverse the order of the list elements
499552
*/
500553
public void reverse() {
501554
int ii = count - 1;
@@ -592,6 +645,7 @@ public String[] values() {
592645
}
593646

594647

648+
@Override
595649
public Iterator<String> iterator() {
596650
// return valueIterator();
597651
// }
@@ -629,7 +683,8 @@ public String[] array() {
629683

630684

631685
/**
632-
* Copy as many values as possible into the specified array.
686+
* Copy values into the specified array. If the specified array is null or
687+
* not the same size, a new array will be allocated.
633688
* @param array
634689
*/
635690
public String[] array(String[] array) {
@@ -679,14 +734,6 @@ public IntDict getOrder() {
679734
}
680735

681736

682-
// public void println() {
683-
// for (int i = 0; i < count; i++) {
684-
// System.out.println("[" + i + "] " + data[i]);
685-
// }
686-
// System.out.flush();
687-
// }
688-
689-
690737
public String join(String separator) {
691738
if (count == 0) {
692739
return "";
@@ -701,10 +748,11 @@ public String join(String separator) {
701748
}
702749

703750

704-
// static public StringList split(String value, char delim) {
705-
// String[] array = PApplet.split(value, delim);
706-
// return new StringList(array);
707-
// }
751+
public void print() {
752+
for (int i = 0; i < size(); i++) {
753+
System.out.format("[%d] %s%n", i, data[i]);
754+
}
755+
}
708756

709757

710758
@Override

0 commit comments

Comments
 (0)