better constructors for Int/Float/StringList

This commit is contained in:
Ben Fry
2015-06-08 15:03:23 -04:00
parent a8a954cf0b
commit 39a1a4fda5
3 changed files with 124 additions and 37 deletions

View File

@@ -28,6 +28,7 @@ public class FloatList implements Iterable<Float> {
data = new float[10];
}
/**
* @nowebref
*/
@@ -35,6 +36,7 @@ public class FloatList implements Iterable<Float> {
data = new float[length];
}
/**
* @nowebref
*/
@@ -44,13 +46,49 @@ public class FloatList implements Iterable<Float> {
System.arraycopy(list, 0, data, 0, count);
}
/**
* Construct an FloatList from an iterable pile of objects.
* For instance, a float array, an array of strings, who knows).
* Un-parseable or null values will be set to NaN.
* @nowebref
*/
public FloatList(Iterable<Float> iter) {
public FloatList(Iterable<Object> iter) {
this(10);
for (float v : iter) {
append(v);
for (Object o : iter) {
if (o == null) {
append(Float.NaN);
} else if (o instanceof Number) {
append(((Number) o).floatValue());
} else {
append(PApplet.parseFloat(o.toString().trim()));
}
}
crop();
}
/**
* Construct an FloatList from a random pile of objects.
* Un-parseable or null values will be set to NaN.
*/
public FloatList(Object... items) {
// nuts, no good way to pass missingValue to this fn (varargs must be last)
final float missingValue = Float.NaN;
count = items.length;
data = new float[count];
int index = 0;
for (Object o : items) {
float value = missingValue;
if (o != null) {
if (o instanceof Number) {
value = ((Number) o).floatValue();
} else {
value = PApplet.parseFloat(o.toString().trim(), missingValue);
}
}
data[index++] = value;
}
}

View File

@@ -33,6 +33,7 @@ public class IntList implements Iterable<Integer> {
data = new int[10];
}
/**
* @nowebref
*/
@@ -40,6 +41,7 @@ public class IntList implements Iterable<Integer> {
data = new int[length];
}
/**
* @nowebref
*/
@@ -49,13 +51,48 @@ public class IntList implements Iterable<Integer> {
System.arraycopy(source, 0, data, 0, count);
}
/**
* Construct an IntList from an iterable pile of objects.
* For instance, a float array, an array of strings, who knows).
* Un-parseable or null values will be set to 0.
* @nowebref
*/
public IntList(Iterable<Integer> iter) {
public IntList(Iterable<Object> iter) {
this(10);
for (int v : iter) {
append(v);
for (Object o : iter) {
if (o == null) {
append(0); // missing value default
} else if (o instanceof Number) {
append(((Number) o).intValue());
} else {
append(PApplet.parseInt(o.toString().trim()));
}
}
crop();
}
/**
* Construct an IntList from a random pile of objects.
* Un-parseable or null values will be set to zero.
*/
public IntList(Object... items) {
final int missingValue = 0; // nuts, can't be last/final/second arg
count = items.length;
data = new int[count];
int index = 0;
for (Object o : items) {
int value = missingValue;
if (o != null) {
if (o instanceof Number) {
value = ((Number) o).intValue();
} else {
value = PApplet.parseInt(o.toString().trim(), missingValue);
}
}
data[index++] = value;
}
}