mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-20 03:04:08 +00:00
Moar
This commit is contained in:
parent
59f29eb6b4
commit
a97cb3df4f
@ -1,5 +1,6 @@
|
|||||||
package ninja.bytecode.iris;
|
package ninja.bytecode.iris;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -13,8 +14,15 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.generator.ChunkGenerator;
|
import org.bukkit.generator.ChunkGenerator;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import ninja.bytecode.iris.util.IO;
|
||||||
|
|
||||||
public class Iris extends JavaPlugin
|
public class Iris extends JavaPlugin
|
||||||
{
|
{
|
||||||
|
public Iris()
|
||||||
|
{
|
||||||
|
IO.delete(new File("iris"));
|
||||||
|
}
|
||||||
|
|
||||||
public void onEnable()
|
public void onEnable()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -7,18 +7,23 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.block.data.BlockData;
|
||||||
import org.bukkit.generator.BlockPopulator;
|
import org.bukkit.generator.BlockPopulator;
|
||||||
import org.bukkit.generator.ChunkGenerator;
|
import org.bukkit.generator.ChunkGenerator;
|
||||||
|
|
||||||
import ninja.bytecode.iris.util.ING;
|
import ninja.bytecode.iris.util.CNG;
|
||||||
|
import ninja.bytecode.iris.util.PolygonGenerator.EnumPolygonGenerator;
|
||||||
import ninja.bytecode.iris.util.RNG;
|
import ninja.bytecode.iris.util.RNG;
|
||||||
|
|
||||||
public class IrisGenerator extends ChunkGenerator
|
public class IrisGenerator extends ChunkGenerator
|
||||||
{
|
{
|
||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
private ING sng;
|
private CNG gen;
|
||||||
|
private EnumPolygonGenerator<BlockData> pog;
|
||||||
|
private BlockData[] d = {Material.RED_CONCRETE.createBlockData(), Material.GREEN_CONCRETE.createBlockData(), Material.BLUE_CONCRETE.createBlockData(),
|
||||||
|
};
|
||||||
|
|
||||||
public void onInit()
|
public void onInit(World world, RNG rng)
|
||||||
{
|
{
|
||||||
if(initialized)
|
if(initialized)
|
||||||
{
|
{
|
||||||
@ -26,8 +31,8 @@ public class IrisGenerator extends ChunkGenerator
|
|||||||
}
|
}
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
gen = CNG.signature(rng.nextParallelRNG(0));
|
||||||
sng = new ING(new RNG(), 2);
|
pog = new EnumPolygonGenerator<BlockData>(rng.nextParallelRNG(1), 0.1, 1, d, (c) -> c);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,9 +42,10 @@ public class IrisGenerator extends ChunkGenerator
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChunkData generateChunkData(World world, Random random, int x, int z, BiomeGrid biome)
|
public ChunkData generateChunkData(World world, Random no, int x, int z, BiomeGrid biome)
|
||||||
{
|
{
|
||||||
onInit();
|
RNG random = new RNG(world.getSeed());
|
||||||
|
onInit(world, random.nextParallelRNG(0));
|
||||||
ChunkData data = Bukkit.createChunkData(world);
|
ChunkData data = Bukkit.createChunkData(world);
|
||||||
|
|
||||||
for(int i = 0; i < 16; i++)
|
for(int i = 0; i < 16; i++)
|
||||||
@ -48,16 +54,8 @@ public class IrisGenerator extends ChunkGenerator
|
|||||||
{
|
{
|
||||||
double wx = (x * 16) + i;
|
double wx = (x * 16) + i;
|
||||||
double wz = (z * 16) + j;
|
double wz = (z * 16) + j;
|
||||||
int y = (int) Math.round(sng.noise(wx / 30D, wz / 30D) * 20);
|
|
||||||
for(int k = 0; k < 4; k++)
|
|
||||||
{
|
|
||||||
if(k < 0)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.setBlock(i, k + y, j, Material.STONE.createBlockData());
|
data.setBlock(i, 0, j, pog.getChoice(wx, wz));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
85
src/main/java/ninja/bytecode/iris/util/Average.java
Normal file
85
src/main/java/ninja/bytecode/iris/util/Average.java
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an incredibly fast averaging object. It swaps values from a sum
|
||||||
|
* using an array. Averages do not use any form of looping. An average of 10,000
|
||||||
|
* entries is the same speed as an average with 5 entries.
|
||||||
|
*
|
||||||
|
* @author cyberpwn
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Average {
|
||||||
|
protected double[] values;
|
||||||
|
private double average;
|
||||||
|
private double lastSum;
|
||||||
|
private boolean dirty;
|
||||||
|
protected int cursor;
|
||||||
|
private boolean brandNew;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an average holder
|
||||||
|
*
|
||||||
|
* @param size the size of entries to keep
|
||||||
|
*/
|
||||||
|
public Average(int size) {
|
||||||
|
values = new double[size];
|
||||||
|
DoubleArrayUtils.fill(values, 0);
|
||||||
|
brandNew = true;
|
||||||
|
average = 0;
|
||||||
|
cursor = 0;
|
||||||
|
lastSum = 0;
|
||||||
|
dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put a value into the average (rolls over if full)
|
||||||
|
*
|
||||||
|
* @param i the value
|
||||||
|
*/
|
||||||
|
public void put(double i) {
|
||||||
|
|
||||||
|
dirty = true;
|
||||||
|
|
||||||
|
if(brandNew)
|
||||||
|
{
|
||||||
|
DoubleArrayUtils.fill(values, i);
|
||||||
|
lastSum = size() * i;
|
||||||
|
brandNew = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double current = values[cursor];
|
||||||
|
lastSum = (lastSum - current) + i;
|
||||||
|
values[cursor] = i;
|
||||||
|
cursor = cursor + 1 < size() ? cursor + 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current average
|
||||||
|
*
|
||||||
|
* @return the average
|
||||||
|
*/
|
||||||
|
public double getAverage() {
|
||||||
|
if (dirty) {
|
||||||
|
calculateAverage();
|
||||||
|
return getAverage();
|
||||||
|
}
|
||||||
|
|
||||||
|
return average;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculateAverage() {
|
||||||
|
average = lastSum / (double) size();
|
||||||
|
dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size()
|
||||||
|
{
|
||||||
|
return values.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDirty()
|
||||||
|
{
|
||||||
|
return dirty;
|
||||||
|
}
|
||||||
|
}
|
155
src/main/java/ninja/bytecode/iris/util/CNG.java
Normal file
155
src/main/java/ninja/bytecode/iris/util/CNG.java
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class CNG
|
||||||
|
{
|
||||||
|
public static long hits = 0;
|
||||||
|
public static long creates = 0;
|
||||||
|
public static final NoiseInjector ADD = (s, v) -> new double[] {s + v, 1};
|
||||||
|
public static final NoiseInjector SRC_SUBTRACT = (s, v) -> new double[] {s - v < 0 ? 0 : s - v, -1};
|
||||||
|
public static final NoiseInjector DST_SUBTRACT = (s, v) -> new double[] {v - s < 0 ? 0 : s - v, -1};
|
||||||
|
public static final NoiseInjector MULTIPLY = (s, v) -> new double[] {s * v, 0};
|
||||||
|
public static final NoiseInjector MAX = (s, v) -> new double[] {Math.max(s, v), 0};
|
||||||
|
public static final NoiseInjector MIN = (s, v) -> new double[] {Math.min(s, v), 0};
|
||||||
|
public static final NoiseInjector SRC_MOD = (s, v) -> new double[] {s % v, 0};
|
||||||
|
public static final NoiseInjector SRC_POW = (s, v) -> new double[] {Math.pow(s, v), 0};
|
||||||
|
public static final NoiseInjector DST_MOD = (s, v) -> new double[] {v % s, 0};
|
||||||
|
public static final NoiseInjector DST_POW = (s, v) -> new double[] {Math.pow(v, s), 0};
|
||||||
|
private double freq;
|
||||||
|
private double amp;
|
||||||
|
private double scale;
|
||||||
|
private double fscale;
|
||||||
|
private KList<CNG> children;
|
||||||
|
private CNG fracture;
|
||||||
|
private SNG generator;
|
||||||
|
private final double opacity;
|
||||||
|
private NoiseInjector injector;
|
||||||
|
private RNG rng;
|
||||||
|
private int oct;
|
||||||
|
private double power;
|
||||||
|
|
||||||
|
public static CNG signature(RNG rng)
|
||||||
|
{
|
||||||
|
//@builder
|
||||||
|
return new CNG(rng.nextParallelRNG(17), 1D, 8)
|
||||||
|
.scale(0.012)
|
||||||
|
.amp(0.5)
|
||||||
|
.freq(1.1)
|
||||||
|
.fractureWith(new CNG(rng.nextParallelRNG(18), 1, 5)
|
||||||
|
.scale(0.018)
|
||||||
|
.child(new CNG(rng.nextParallelRNG(19), 0.745, 2)
|
||||||
|
.scale(0.1))
|
||||||
|
.fractureWith(new CNG(rng.nextParallelRNG(20), 1, 3)
|
||||||
|
.scale(0.15), 24), 44);
|
||||||
|
//@done
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG(RNG random)
|
||||||
|
{
|
||||||
|
this(random, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG(RNG random, int octaves)
|
||||||
|
{
|
||||||
|
this(random, 1D, octaves);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG(RNG random, double opacity, int octaves)
|
||||||
|
{
|
||||||
|
creates += octaves;
|
||||||
|
this.oct = octaves;
|
||||||
|
this.rng = random;
|
||||||
|
power = 1;
|
||||||
|
freq = 1;
|
||||||
|
amp = 1;
|
||||||
|
scale = 1;
|
||||||
|
fscale = 1;
|
||||||
|
fracture = null;
|
||||||
|
generator = new SNG(random);
|
||||||
|
this.opacity = opacity;
|
||||||
|
this.injector = ADD;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG child(CNG c)
|
||||||
|
{
|
||||||
|
if(children == null)
|
||||||
|
{
|
||||||
|
children = new KList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
children.add(c);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public RNG nextRNG()
|
||||||
|
{
|
||||||
|
return getRNG().nextRNG();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RNG getRNG()
|
||||||
|
{
|
||||||
|
return rng;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG fractureWith(CNG c, double scale)
|
||||||
|
{
|
||||||
|
fracture = c;
|
||||||
|
fscale = scale;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG scale(double c)
|
||||||
|
{
|
||||||
|
scale = c;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG freq(double c)
|
||||||
|
{
|
||||||
|
freq = c;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG amp(double c)
|
||||||
|
{
|
||||||
|
amp = c;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG injectWith(NoiseInjector i)
|
||||||
|
{
|
||||||
|
injector = i;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double noise(double... dim)
|
||||||
|
{
|
||||||
|
double f = fracture != null ? (fracture.noise(dim) - 0.5) * fscale : 0D;
|
||||||
|
double x = dim.length > 0 ? dim[0] + f : 0D;
|
||||||
|
double y = dim.length > 1 ? dim[1] - f : 0D;
|
||||||
|
double z = dim.length > 2 ? dim[2] + f : 0D;
|
||||||
|
double n = ((generator.noise(x * scale, y * scale, z * scale, oct, freq, amp, true) / 2D) + 0.5D) * opacity;
|
||||||
|
n = power != 1D ? Math.pow(n, power) : n;
|
||||||
|
double m = 1;
|
||||||
|
hits += oct;
|
||||||
|
if(children == null)
|
||||||
|
{
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(CNG i : children)
|
||||||
|
{
|
||||||
|
double[] r = injector.combine(n, i.noise(dim));
|
||||||
|
n = r[0];
|
||||||
|
m += r[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return n / m;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CNG pow(double power)
|
||||||
|
{
|
||||||
|
this.power = power;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
6
src/main/java/ninja/bytecode/iris/util/Callback.java
Normal file
6
src/main/java/ninja/bytecode/iris/util/Callback.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public interface Callback<T>
|
||||||
|
{
|
||||||
|
public void run(T t);
|
||||||
|
}
|
29
src/main/java/ninja/bytecode/iris/util/ChronoLatch.java
Normal file
29
src/main/java/ninja/bytecode/iris/util/ChronoLatch.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class ChronoLatch
|
||||||
|
{
|
||||||
|
private long interval;
|
||||||
|
private long since;
|
||||||
|
|
||||||
|
public ChronoLatch(long interval, boolean openedAtStart)
|
||||||
|
{
|
||||||
|
this.interval = interval;
|
||||||
|
since = System.currentTimeMillis() - (openedAtStart ? interval * 2 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChronoLatch(long interval)
|
||||||
|
{
|
||||||
|
this(interval, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean flip()
|
||||||
|
{
|
||||||
|
if(System.currentTimeMillis() - since > interval)
|
||||||
|
{
|
||||||
|
since = System.currentTimeMillis();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
57
src/main/java/ninja/bytecode/iris/util/Chunker.java
Normal file
57
src/main/java/ninja/bytecode/iris/util/Chunker.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class Chunker<T>
|
||||||
|
{
|
||||||
|
private ExecutorService executor;
|
||||||
|
private int threads;
|
||||||
|
private int workload;
|
||||||
|
private KList<T> q;
|
||||||
|
|
||||||
|
public Chunker(KList<T> q)
|
||||||
|
{
|
||||||
|
this.q = q;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chunker<T> threads(int threads)
|
||||||
|
{
|
||||||
|
this.threads = threads;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chunker<T> workload(int workload)
|
||||||
|
{
|
||||||
|
this.workload = workload;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(Consumer<T> consumer, Callback<Double> progress, int progressInterval)
|
||||||
|
{
|
||||||
|
ChronoLatch cl = new ChronoLatch(progressInterval);
|
||||||
|
Contained<Integer> consumed = new Contained<Integer>(0);
|
||||||
|
executor = Executors.newFixedThreadPool(threads);
|
||||||
|
int length = q.size();
|
||||||
|
int remaining = length;
|
||||||
|
|
||||||
|
while(remaining > 0)
|
||||||
|
{
|
||||||
|
int at = remaining;
|
||||||
|
remaining -= (remaining > workload ? workload : remaining);
|
||||||
|
int to = remaining;
|
||||||
|
|
||||||
|
executor.submit(() ->
|
||||||
|
{
|
||||||
|
J.dofor(at, (i) -> i >= to, -1, (i) -> J.attempt(() -> consumer.accept(q.get(i))));
|
||||||
|
consumed.mod((c) -> c += workload);
|
||||||
|
J.doif(() -> progress != null && cl.flip(), () -> progress.run((double) consumed.get() / (double) length));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.shutdown();
|
||||||
|
J.attempt(() -> executor.awaitTermination(100, TimeUnit.HOURS));
|
||||||
|
}
|
||||||
|
}
|
7
src/main/java/ninja/bytecode/iris/util/Consumer2.java
Normal file
7
src/main/java/ninja/bytecode/iris/util/Consumer2.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Consumer2<A, B>
|
||||||
|
{
|
||||||
|
public void accept(A a, B b);
|
||||||
|
}
|
7
src/main/java/ninja/bytecode/iris/util/Consumer3.java
Normal file
7
src/main/java/ninja/bytecode/iris/util/Consumer3.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Consumer3<A, B, C>
|
||||||
|
{
|
||||||
|
public void accept(A a, B b, C c);
|
||||||
|
}
|
33
src/main/java/ninja/bytecode/iris/util/Contained.java
Normal file
33
src/main/java/ninja/bytecode/iris/util/Contained.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class Contained<T>
|
||||||
|
{
|
||||||
|
private T t;
|
||||||
|
|
||||||
|
public Contained(T t)
|
||||||
|
{
|
||||||
|
set(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Contained()
|
||||||
|
{
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mod(Function<T, T> x)
|
||||||
|
{
|
||||||
|
set(x.apply(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get()
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(T t)
|
||||||
|
{
|
||||||
|
this.t = t;
|
||||||
|
}
|
||||||
|
}
|
29
src/main/java/ninja/bytecode/iris/util/DoubleArrayUtils.java
Normal file
29
src/main/java/ninja/bytecode/iris/util/DoubleArrayUtils.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
public class DoubleArrayUtils
|
||||||
|
{
|
||||||
|
public static void shiftRight(double[] values, double push)
|
||||||
|
{
|
||||||
|
for(int index = values.length - 2; index >= 0; index--)
|
||||||
|
{
|
||||||
|
values[index + 1] = values[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
values[0] = push;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void wrapRight(double[] values)
|
||||||
|
{
|
||||||
|
double last = values[values.length - 1];
|
||||||
|
shiftRight(values, last);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fill(double[] values, double value)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < values.length; i++)
|
||||||
|
{
|
||||||
|
values[i] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1564
src/main/java/ninja/bytecode/iris/util/Form.java
Normal file
1564
src/main/java/ninja/bytecode/iris/util/Form.java
Normal file
File diff suppressed because it is too large
Load Diff
50
src/main/java/ninja/bytecode/iris/util/Formula.java
Normal file
50
src/main/java/ninja/bytecode/iris/util/Formula.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates an expression using javascript engine and returns the double
|
||||||
|
* result. This can take variable parameters, so you need to define them.
|
||||||
|
* Parameters are defined as $[0-9]. For example evaluate("4$0/$1", 1, 2); This
|
||||||
|
* makes the expression (4x1)/2 == 2. Keep note that you must use 0-9, you
|
||||||
|
* cannot skip, or start at a number other than 0.
|
||||||
|
*
|
||||||
|
* @author cyberpwn
|
||||||
|
*/
|
||||||
|
public class Formula
|
||||||
|
{
|
||||||
|
private String expression;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates an expression using javascript engine and returns the double
|
||||||
|
* result. This can take variable parameters, so you need to define them.
|
||||||
|
* Parameters are defined as $[0-9]. For example evaluate("4$0/$1", 1, 2); This
|
||||||
|
* makes the expression (4x1)/2 == 2. Keep note that you must use 0-9, you
|
||||||
|
* cannot skip, or start at a number other than 0.
|
||||||
|
*
|
||||||
|
* @param expression
|
||||||
|
* the expression with variables
|
||||||
|
* @param args
|
||||||
|
* the arguments/variables
|
||||||
|
*/
|
||||||
|
public Formula(String expression)
|
||||||
|
{
|
||||||
|
this.expression = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates the given args
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
* the args
|
||||||
|
* @return the return result
|
||||||
|
* @throws IndexOutOfBoundsException
|
||||||
|
* invalid number of args
|
||||||
|
* @throws ScriptException
|
||||||
|
* syntax issue
|
||||||
|
*/
|
||||||
|
public double evaluate(Double... args) throws IndexOutOfBoundsException, ScriptException
|
||||||
|
{
|
||||||
|
return M.evaluate(expression, args);
|
||||||
|
}
|
||||||
|
}
|
7
src/main/java/ninja/bytecode/iris/util/Function2.java
Normal file
7
src/main/java/ninja/bytecode/iris/util/Function2.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Function2<A, B, R>
|
||||||
|
{
|
||||||
|
public R apply(A a, B b);
|
||||||
|
}
|
7
src/main/java/ninja/bytecode/iris/util/Function3.java
Normal file
7
src/main/java/ninja/bytecode/iris/util/Function3.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Function3<A, B, C, R>
|
||||||
|
{
|
||||||
|
public R apply(A a, B b, C c);
|
||||||
|
}
|
7
src/main/java/ninja/bytecode/iris/util/Function4.java
Normal file
7
src/main/java/ninja/bytecode/iris/util/Function4.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Function4<A, B, C, D, R>
|
||||||
|
{
|
||||||
|
public R apply(A a, B b, C c, D d);
|
||||||
|
}
|
201
src/main/java/ninja/bytecode/iris/util/HTTP.java
Normal file
201
src/main/java/ninja/bytecode/iris/util/HTTP.java
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2002 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an HTTP header to a JSONObject and back.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class HTTP
|
||||||
|
{
|
||||||
|
|
||||||
|
/** Carriage return/line feed. */
|
||||||
|
public static final String CRLF = "\r\n";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an HTTP header string into a JSONObject. It can be a request
|
||||||
|
* header or a response header. A request header will contain
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {
|
||||||
|
* Method: "POST" (for example),
|
||||||
|
* "Request-URI": "/" (for example),
|
||||||
|
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* A response header will contain
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {
|
||||||
|
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||||
|
* "Status-Code": "200" (for example),
|
||||||
|
* "Reason-Phrase": "OK" (for example)
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* In addition, the other parameters in the header will be captured, using
|
||||||
|
* the HTTP field names as JSON names, so that
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* Date: Sun, 26 May 2002 18:06:04 GMT
|
||||||
|
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
|
||||||
|
* Cache-Control: no-cache
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* become
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {...
|
||||||
|
* Date: "Sun, 26 May 2002 18:06:04 GMT",
|
||||||
|
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
|
||||||
|
* "Cache-Control": "no-cache",
|
||||||
|
* ...}
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* It does no further checking or conversion. It does not parse dates. It
|
||||||
|
* does not do '%' transforms on URLs.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* An HTTP header string.
|
||||||
|
* @return A JSONObject containing the elements and attributes of the XML
|
||||||
|
* string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static JSONObject toJSONObject(String string) throws JSONException
|
||||||
|
{
|
||||||
|
JSONObject jo = new JSONObject();
|
||||||
|
HTTPTokener x = new HTTPTokener(string);
|
||||||
|
String token;
|
||||||
|
|
||||||
|
token = x.nextToken();
|
||||||
|
if(token.toUpperCase().startsWith("HTTP"))
|
||||||
|
{
|
||||||
|
|
||||||
|
// Response
|
||||||
|
|
||||||
|
jo.put("HTTP-Version", token);
|
||||||
|
jo.put("Status-Code", x.nextToken());
|
||||||
|
jo.put("Reason-Phrase", x.nextTo('\0'));
|
||||||
|
x.next();
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
|
||||||
|
// Request
|
||||||
|
|
||||||
|
jo.put("Method", token);
|
||||||
|
jo.put("Request-URI", x.nextToken());
|
||||||
|
jo.put("HTTP-Version", x.nextToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
|
||||||
|
while(x.more())
|
||||||
|
{
|
||||||
|
String name = x.nextTo(':');
|
||||||
|
x.next(':');
|
||||||
|
jo.put(name, x.nextTo('\0'));
|
||||||
|
x.next();
|
||||||
|
}
|
||||||
|
return jo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a JSONObject into an HTTP header. A request header must contain
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {
|
||||||
|
* Method: "POST" (for example),
|
||||||
|
* "Request-URI": "/" (for example),
|
||||||
|
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* A response header must contain
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {
|
||||||
|
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||||
|
* "Status-Code": "200" (for example),
|
||||||
|
* "Reason-Phrase": "OK" (for example)
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* Any other members of the JSONObject will be output as HTTP fields. The
|
||||||
|
* result will end with two CRLF pairs.
|
||||||
|
*
|
||||||
|
* @param jo
|
||||||
|
* A JSONObject
|
||||||
|
* @return An HTTP header string.
|
||||||
|
* @throws JSONException
|
||||||
|
* if the object does not contain enough information.
|
||||||
|
*/
|
||||||
|
public static String toString(JSONObject jo) throws JSONException
|
||||||
|
{
|
||||||
|
Iterator<String> keys = jo.keys();
|
||||||
|
String string;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
if(jo.has("Status-Code") && jo.has("Reason-Phrase"))
|
||||||
|
{
|
||||||
|
sb.append(jo.getString("HTTP-Version"));
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(jo.getString("Status-Code"));
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(jo.getString("Reason-Phrase"));
|
||||||
|
} else if(jo.has("Method") && jo.has("Request-URI"))
|
||||||
|
{
|
||||||
|
sb.append(jo.getString("Method"));
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append('"');
|
||||||
|
sb.append(jo.getString("Request-URI"));
|
||||||
|
sb.append('"');
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(jo.getString("HTTP-Version"));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw new JSONException("Not enough material for an HTTP header.");
|
||||||
|
}
|
||||||
|
sb.append(CRLF);
|
||||||
|
while(keys.hasNext())
|
||||||
|
{
|
||||||
|
string = keys.next();
|
||||||
|
if(!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && !"Reason-Phrase".equals(string) && !"Method".equals(string) && !"Request-URI".equals(string) && !jo.isNull(string))
|
||||||
|
{
|
||||||
|
sb.append(string);
|
||||||
|
sb.append(": ");
|
||||||
|
sb.append(jo.getString(string));
|
||||||
|
sb.append(CRLF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append(CRLF);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
91
src/main/java/ninja/bytecode/iris/util/HTTPTokener.java
Normal file
91
src/main/java/ninja/bytecode/iris/util/HTTPTokener.java
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2002 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HTTPTokener extends the JSONTokener to provide additional methods for the
|
||||||
|
* parsing of HTTP headers.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class HTTPTokener extends JSONTokener
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an HTTPTokener from a string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* A source string.
|
||||||
|
*/
|
||||||
|
public HTTPTokener(String string)
|
||||||
|
{
|
||||||
|
super(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next token or string. This is used in parsing HTTP headers.
|
||||||
|
*
|
||||||
|
* @throws JSONException
|
||||||
|
* @return A String.
|
||||||
|
*/
|
||||||
|
public String nextToken() throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
char q;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
do
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
} while(Character.isWhitespace(c));
|
||||||
|
if(c == '"' || c == '\'')
|
||||||
|
{
|
||||||
|
q = c;
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
if(c < ' ')
|
||||||
|
{
|
||||||
|
throw syntaxError("Unterminated string.");
|
||||||
|
}
|
||||||
|
if(c == q)
|
||||||
|
{
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
if(c == 0 || Character.isWhitespace(c))
|
||||||
|
{
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
sb.append(c);
|
||||||
|
c = next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,37 +2,8 @@ package ninja.bytecode.iris.util;
|
|||||||
|
|
||||||
public class ING
|
public class ING
|
||||||
{
|
{
|
||||||
private SNG base;
|
public ING(RNG rng)
|
||||||
private SNG[] children;
|
|
||||||
|
|
||||||
public ING(RNG rng, int detail)
|
|
||||||
{
|
{
|
||||||
assert (detail >= 1);
|
|
||||||
this.children = new SNG[detail];
|
|
||||||
|
|
||||||
for(int i = 0; i < detail; i++)
|
|
||||||
{
|
|
||||||
children[i] = new SNG(rng.nextParallelRNG((i * 368989) % 13345));
|
|
||||||
}
|
|
||||||
base = new SNG(rng.nextParallelRNG(13));
|
|
||||||
}
|
|
||||||
|
|
||||||
public double noise(double x, double z)
|
|
||||||
{
|
|
||||||
double cx = x;
|
|
||||||
double cz = z;
|
|
||||||
int i;
|
|
||||||
double j;
|
|
||||||
double k;
|
|
||||||
|
|
||||||
for(i = 0; i < children.length; i++)
|
|
||||||
{
|
|
||||||
j = (i + 1) * 2;
|
|
||||||
k = (i + 1) * 1;
|
|
||||||
cx = cx + (children[i].noise((cx / j) + i, (cz / j) - i) * k);
|
|
||||||
cz = cz - (children[i].noise((cz / j) - i, (cx / j) + i) * k);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (base.noise(cx, cz) / 2D) + 0.5D;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1910
src/main/java/ninja/bytecode/iris/util/IO.java
Normal file
1910
src/main/java/ninja/bytecode/iris/util/IO.java
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
public enum InterpolationType
|
||||||
|
{
|
||||||
|
LINEAR,
|
||||||
|
PARAMETRIC_2,
|
||||||
|
PARAMETRIC_4,
|
||||||
|
BEZIER,
|
||||||
|
NONE;
|
||||||
|
}
|
224
src/main/java/ninja/bytecode/iris/util/IrisInterpolation.java
Normal file
224
src/main/java/ninja/bytecode/iris/util/IrisInterpolation.java
Normal file
@ -0,0 +1,224 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class IrisInterpolation
|
||||||
|
{
|
||||||
|
public static double bezier(double t)
|
||||||
|
{
|
||||||
|
return t * t * (3.0d - 2.0d * t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parametric(double t, double alpha)
|
||||||
|
{
|
||||||
|
double sqt = Math.pow(t, alpha);
|
||||||
|
return sqt / (alpha * (sqt - Math.pow(t, alpha - 1)) + 1.0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double lerp(double a, double b, double f)
|
||||||
|
{
|
||||||
|
return a + (f * (b - a));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double lerpBezier(double a, double b, double f)
|
||||||
|
{
|
||||||
|
return a + (bezier(f) * (b - a));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double sinCenter(double f)
|
||||||
|
{
|
||||||
|
return Math.sin(f * Math.PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double lerpCenterSinBezier(double a, double b, double f)
|
||||||
|
{
|
||||||
|
return lerpBezier(a, b, sinCenter(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double lerpCenterSin(double a, double b, double f)
|
||||||
|
{
|
||||||
|
return lerpBezier(a, b, sinCenter(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double lerpParametric(double a, double b, double f, double v)
|
||||||
|
{
|
||||||
|
return a + (parametric(f, v) * (b - a));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double blerp(double a, double b, double c, double d, double tx, double ty)
|
||||||
|
{
|
||||||
|
return lerp(lerp(a, b, tx), lerp(c, d, tx), ty);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double blerp(double a, double b, double c, double d, double tx, double ty, InterpolationType type)
|
||||||
|
{
|
||||||
|
if(type.equals(InterpolationType.LINEAR))
|
||||||
|
{
|
||||||
|
return blerp(a, b, c, d, tx, ty);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(type.equals(InterpolationType.BEZIER))
|
||||||
|
{
|
||||||
|
return blerpBezier(a, b, c, d, tx, ty);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(type.equals(InterpolationType.PARAMETRIC_2))
|
||||||
|
{
|
||||||
|
return blerpParametric(a, b, c, d, tx, ty, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(type.equals(InterpolationType.PARAMETRIC_4))
|
||||||
|
{
|
||||||
|
return blerpParametric(a, b, c, d, tx, ty, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double blerpBezier(double a, double b, double c, double d, double tx, double ty)
|
||||||
|
{
|
||||||
|
return lerpBezier(lerpBezier(a, b, tx), lerpBezier(c, d, tx), ty);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double blerpParametric(double a, double b, double c, double d, double tx, double ty, double v)
|
||||||
|
{
|
||||||
|
return lerpParametric(lerpParametric(a, b, tx, v), lerpParametric(c, d, tx, v), ty, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double hermite(double p0, double p1, double p2, double p3, double mu, double tension, double bias)
|
||||||
|
{
|
||||||
|
double m0, m1, mu2, mu3;
|
||||||
|
double a0, a1, a2, a3;
|
||||||
|
|
||||||
|
mu2 = mu * mu;
|
||||||
|
mu3 = mu2 * mu;
|
||||||
|
m0 = (p1 - p0) * (1 + bias) * (1 - tension) / 2;
|
||||||
|
m0 += (p2 - p1) * (1 - bias) * (1 - tension) / 2;
|
||||||
|
m1 = (p2 - p1) * (1 + bias) * (1 - tension) / 2;
|
||||||
|
m1 += (p3 - p2) * (1 - bias) * (1 - tension) / 2;
|
||||||
|
a0 = 2 * mu3 - 3 * mu2 + 1;
|
||||||
|
a1 = mu3 - 2 * mu2 + mu;
|
||||||
|
a2 = mu3 - mu2;
|
||||||
|
a3 = -2 * mu3 + 3 * mu2;
|
||||||
|
|
||||||
|
return (a0 * p1 + a1 * m0 + a2 * m1 + a3 * p2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double bihermite(double p00, double p01, double p02, double p03, double p10, double p11, double p12, double p13, double p20, double p21, double p22, double p23, double p30, double p31, double p32, double p33, double mux, double muy, double tension, double bias)
|
||||||
|
{
|
||||||
|
return hermite(hermite(p00, p01, p02, p03, muy, tension, bias), hermite(p10, p11, p12, p13, muy, tension, bias), hermite(p20, p21, p22, p23, muy, tension, bias), hermite(p30, p31, p32, p33, muy, tension, bias), mux, tension, bias);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double cubic(double p0, double p1, double p2, double p3, double mu)
|
||||||
|
{
|
||||||
|
double a0, a1, a2, a3, mu2;
|
||||||
|
|
||||||
|
mu2 = mu * mu;
|
||||||
|
a0 = p3 - p2 - p0 + p1;
|
||||||
|
a1 = p0 - p1 - a0;
|
||||||
|
a2 = p2 - p0;
|
||||||
|
a3 = p1;
|
||||||
|
|
||||||
|
return a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double bicubic(double p00, double p01, double p02, double p03, double p10, double p11, double p12, double p13, double p20, double p21, double p22, double p23, double p30, double p31, double p32, double p33, double mux, double muy)
|
||||||
|
{
|
||||||
|
return cubic(cubic(p00, p01, p02, p03, muy), cubic(p10, p11, p12, p13, muy), cubic(p20, p21, p22, p23, muy), cubic(p30, p31, p32, p33, muy), mux);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getBilinearNoise(int x, int z, double rad, NoiseProvider n)
|
||||||
|
{
|
||||||
|
int fx = (int) Math.floor(x / rad);
|
||||||
|
int fz = (int) Math.floor(z / rad);
|
||||||
|
int x1 = (int) Math.round(fx * rad);
|
||||||
|
int z1 = (int) Math.round(fz * rad);
|
||||||
|
int x2 = (int) Math.round((fx + 1) * rad);
|
||||||
|
int z2 = (int) Math.round((fz + 1) * rad);
|
||||||
|
double px = rangeScale(0, 1, x1, x2, x);
|
||||||
|
double pz = rangeScale(0, 1, z1, z2, z);
|
||||||
|
//@builder
|
||||||
|
return blerpBezier(
|
||||||
|
n.noise(x1, z1),
|
||||||
|
n.noise(x2, z1),
|
||||||
|
n.noise(x1, z2),
|
||||||
|
n.noise(x2, z2),
|
||||||
|
px, pz);
|
||||||
|
//@done
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getBicubicNoise(int x, int z, double rad, NoiseProvider n)
|
||||||
|
{
|
||||||
|
int fx = (int) Math.floor(x / rad);
|
||||||
|
int fz = (int) Math.floor(z / rad);
|
||||||
|
int x0 = (int) Math.round((fx - 1) * rad);
|
||||||
|
int z0 = (int) Math.round((fz - 1) * rad);
|
||||||
|
int x1 = (int) Math.round(fx * rad);
|
||||||
|
int z1 = (int) Math.round(fz * rad);
|
||||||
|
int x2 = (int) Math.round((fx + 1) * rad);
|
||||||
|
int z2 = (int) Math.round((fz + 1) * rad);
|
||||||
|
int x3 = (int) Math.round((fx + 2) * rad);
|
||||||
|
int z3 = (int) Math.round((fz + 2) * rad);
|
||||||
|
double px = rangeScale(0, 1, x1, x2, x);
|
||||||
|
double pz = rangeScale(0, 1, z1, z2, z);
|
||||||
|
//@builder
|
||||||
|
return bicubic(
|
||||||
|
n.noise(x0, z0),
|
||||||
|
n.noise(x0, z1),
|
||||||
|
n.noise(x0, z2),
|
||||||
|
n.noise(x0, z3),
|
||||||
|
n.noise(x1, z0),
|
||||||
|
n.noise(x1, z1),
|
||||||
|
n.noise(x1, z2),
|
||||||
|
n.noise(x1, z3),
|
||||||
|
n.noise(x2, z0),
|
||||||
|
n.noise(x2, z1),
|
||||||
|
n.noise(x2, z2),
|
||||||
|
n.noise(x2, z3),
|
||||||
|
n.noise(x3, z0),
|
||||||
|
n.noise(x3, z1),
|
||||||
|
n.noise(x3, z2),
|
||||||
|
n.noise(x3, z3),
|
||||||
|
px, pz);
|
||||||
|
//@done
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getHermiteNoise(int x, int z, double rad, NoiseProvider n)
|
||||||
|
{
|
||||||
|
int fx = (int) Math.floor(x / rad);
|
||||||
|
int fz = (int) Math.floor(z / rad);
|
||||||
|
int x0 = (int) Math.round((fx - 1) * rad);
|
||||||
|
int z0 = (int) Math.round((fz - 1) * rad);
|
||||||
|
int x1 = (int) Math.round(fx * rad);
|
||||||
|
int z1 = (int) Math.round(fz * rad);
|
||||||
|
int x2 = (int) Math.round((fx + 1) * rad);
|
||||||
|
int z2 = (int) Math.round((fz + 1) * rad);
|
||||||
|
int x3 = (int) Math.round((fx + 2) * rad);
|
||||||
|
int z3 = (int) Math.round((fz + 2) * rad);
|
||||||
|
double px = rangeScale(0, 1, x1, x2, x);
|
||||||
|
double pz = rangeScale(0, 1, z1, z2, z);
|
||||||
|
//@builder
|
||||||
|
return bihermite(
|
||||||
|
n.noise(x0, z0),
|
||||||
|
n.noise(x0, z1),
|
||||||
|
n.noise(x0, z2),
|
||||||
|
n.noise(x0, z3),
|
||||||
|
n.noise(x1, z0),
|
||||||
|
n.noise(x1, z1),
|
||||||
|
n.noise(x1, z2),
|
||||||
|
n.noise(x1, z3),
|
||||||
|
n.noise(x2, z0),
|
||||||
|
n.noise(x2, z1),
|
||||||
|
n.noise(x2, z2),
|
||||||
|
n.noise(x2, z3),
|
||||||
|
n.noise(x3, z0),
|
||||||
|
n.noise(x3, z1),
|
||||||
|
n.noise(x3, z2),
|
||||||
|
n.noise(x3, z3),
|
||||||
|
px, pz, 0.00001, 0.5);
|
||||||
|
//@done
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double rangeScale(double amin, double amax, double bmin, double bmax, double b)
|
||||||
|
{
|
||||||
|
return amin + ((amax - amin) * ((b - bmin) / (bmax - bmin)));
|
||||||
|
}
|
||||||
|
}
|
133
src/main/java/ninja/bytecode/iris/util/J.java
Normal file
133
src/main/java/ninja/bytecode/iris/util/J.java
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class J
|
||||||
|
{
|
||||||
|
private static int tid = 0;
|
||||||
|
private static final ExecutorService e = Executors.newCachedThreadPool(new ThreadFactory()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r)
|
||||||
|
{
|
||||||
|
tid++;
|
||||||
|
Thread t = new Thread(r);
|
||||||
|
t.setName("Actuator " + tid);
|
||||||
|
t.setPriority(Thread.MIN_PRIORITY);
|
||||||
|
t.setUncaughtExceptionHandler((et, e) -> {
|
||||||
|
e.printStackTrace();
|
||||||
|
});
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
public static void dofor(int a, Function<Integer, Boolean> c, int ch, Consumer<Integer> d)
|
||||||
|
{
|
||||||
|
for(int i = a; c.apply(i); i+=ch)
|
||||||
|
{
|
||||||
|
c.apply(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean doif(Supplier<Boolean> c, Runnable g)
|
||||||
|
{
|
||||||
|
if(c.get())
|
||||||
|
{
|
||||||
|
g.run();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void a(Runnable a)
|
||||||
|
{
|
||||||
|
e.submit(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Future<T> a(Callable<T> a)
|
||||||
|
{
|
||||||
|
return e.submit(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void attemptAsync(NastyRunnable r)
|
||||||
|
{
|
||||||
|
J.a(() -> J.attempt(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <R> R attemptResult(NastyFuture<R> r, R onError)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return r.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return onError;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T, R> R attemptFunction(NastyFunction<T, R> r, T param, R onError)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return r.run(param);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return onError;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean sleep(long ms)
|
||||||
|
{
|
||||||
|
return J.attempt(() -> Thread.sleep(ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean attempt(NastyRunnable r)
|
||||||
|
{
|
||||||
|
return attemptCatch(r) == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Throwable attemptCatch(NastyRunnable r)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
r.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T attempt(Supplier<T> t, T i)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return t.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1252
src/main/java/ninja/bytecode/iris/util/JSONArray.java
Normal file
1252
src/main/java/ninja/bytecode/iris/util/JSONArray.java
Normal file
File diff suppressed because it is too large
Load Diff
50
src/main/java/ninja/bytecode/iris/util/JSONException.java
Normal file
50
src/main/java/ninja/bytecode/iris/util/JSONException.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class JSONException extends RuntimeException
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 0;
|
||||||
|
private Throwable cause;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a JSONException with an explanatory message.
|
||||||
|
*
|
||||||
|
* @param message
|
||||||
|
* Detail about the reason for the exception.
|
||||||
|
*/
|
||||||
|
public JSONException(String message)
|
||||||
|
{
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new JSONException with the specified cause.
|
||||||
|
*
|
||||||
|
* @param cause
|
||||||
|
* The cause.
|
||||||
|
*/
|
||||||
|
public JSONException(Throwable cause)
|
||||||
|
{
|
||||||
|
super(cause.getMessage());
|
||||||
|
this.cause = cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the cause of this exception or null if the cause is nonexistent
|
||||||
|
* or unknown.
|
||||||
|
*
|
||||||
|
* @return the cause of this exception or null if the cause is nonexistent
|
||||||
|
* or unknown.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Throwable getCause()
|
||||||
|
{
|
||||||
|
return this.cause;
|
||||||
|
}
|
||||||
|
}
|
555
src/main/java/ninja/bytecode/iris/util/JSONML.java
Normal file
555
src/main/java/ninja/bytecode/iris/util/JSONML.java
Normal file
@ -0,0 +1,555 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2008 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This provides static methods to convert an XML text into a JSONArray or
|
||||||
|
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
|
||||||
|
* the JsonML transform.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class JSONML
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse XML values and store them in a JSONArray.
|
||||||
|
*
|
||||||
|
* @param x
|
||||||
|
* The XMLTokener containing the source string.
|
||||||
|
* @param arrayForm
|
||||||
|
* true if array form, false if object form.
|
||||||
|
* @param ja
|
||||||
|
* The JSONArray that is containing the current tag or null if we
|
||||||
|
* are at the outermost level.
|
||||||
|
* @return A JSONArray if the value is the outermost tag, otherwise null.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja) throws JSONException
|
||||||
|
{
|
||||||
|
String attribute;
|
||||||
|
char c;
|
||||||
|
String closeTag = null;
|
||||||
|
int i;
|
||||||
|
JSONArray newja = null;
|
||||||
|
JSONObject newjo = null;
|
||||||
|
Object token;
|
||||||
|
String tagName = null;
|
||||||
|
|
||||||
|
// Test for and skip past these forms:
|
||||||
|
// <!-- ... -->
|
||||||
|
// <![ ... ]]>
|
||||||
|
// <! ... >
|
||||||
|
// <? ... ?>
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
if(!x.more())
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Bad XML");
|
||||||
|
}
|
||||||
|
token = x.nextContent();
|
||||||
|
if(token == XML.LT)
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
if(token instanceof Character)
|
||||||
|
{
|
||||||
|
if(token == XML.SLASH)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Close tag </
|
||||||
|
|
||||||
|
token = x.nextToken();
|
||||||
|
if(!(token instanceof String))
|
||||||
|
{
|
||||||
|
throw new JSONException("Expected a closing name instead of '" + token + "'.");
|
||||||
|
}
|
||||||
|
if(x.nextToken() != XML.GT)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped close tag");
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
} else if(token == XML.BANG)
|
||||||
|
{
|
||||||
|
|
||||||
|
// <!
|
||||||
|
|
||||||
|
c = x.next();
|
||||||
|
if(c == '-')
|
||||||
|
{
|
||||||
|
if(x.next() == '-')
|
||||||
|
{
|
||||||
|
x.skipPast("-->");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
x.back();
|
||||||
|
}
|
||||||
|
} else if(c == '[')
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
if(token.equals("CDATA") && x.next() == '[')
|
||||||
|
{
|
||||||
|
if(ja != null)
|
||||||
|
{
|
||||||
|
ja.put(x.nextCDATA());
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Expected 'CDATA['");
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
token = x.nextMeta();
|
||||||
|
if(token == null)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Missing '>' after '<!'.");
|
||||||
|
} else if(token == XML.LT)
|
||||||
|
{
|
||||||
|
i += 1;
|
||||||
|
} else if(token == XML.GT)
|
||||||
|
{
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
} while(i > 0);
|
||||||
|
}
|
||||||
|
} else if(token == XML.QUEST)
|
||||||
|
{
|
||||||
|
|
||||||
|
// <?
|
||||||
|
|
||||||
|
x.skipPast("?>");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open tag <
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if(!(token instanceof String))
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Bad tagName '" + token + "'.");
|
||||||
|
}
|
||||||
|
tagName = (String) token;
|
||||||
|
newja = new JSONArray();
|
||||||
|
newjo = new JSONObject();
|
||||||
|
if(arrayForm)
|
||||||
|
{
|
||||||
|
newja.put(tagName);
|
||||||
|
if(ja != null)
|
||||||
|
{
|
||||||
|
ja.put(newja);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
newjo.put("tagName", tagName);
|
||||||
|
if(ja != null)
|
||||||
|
{
|
||||||
|
ja.put(newjo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token = null;
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
if(token == null)
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
}
|
||||||
|
if(token == null)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
}
|
||||||
|
if(!(token instanceof String))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// attribute = value
|
||||||
|
|
||||||
|
attribute = (String) token;
|
||||||
|
if(!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute)))
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Reserved attribute.");
|
||||||
|
}
|
||||||
|
token = x.nextToken();
|
||||||
|
if(token == XML.EQ)
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
if(!(token instanceof String))
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Missing value");
|
||||||
|
}
|
||||||
|
newjo.accumulate(attribute, XML.stringToValue((String) token));
|
||||||
|
token = null;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
newjo.accumulate(attribute, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(arrayForm && newjo.length() > 0)
|
||||||
|
{
|
||||||
|
newja.put(newjo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty tag <.../>
|
||||||
|
|
||||||
|
if(token == XML.SLASH)
|
||||||
|
{
|
||||||
|
if(x.nextToken() != XML.GT)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
}
|
||||||
|
if(ja == null)
|
||||||
|
{
|
||||||
|
if(arrayForm)
|
||||||
|
{
|
||||||
|
return newja;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
return newjo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content, between <...> and </...>
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if(token != XML.GT)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
}
|
||||||
|
closeTag = (String) parse(x, arrayForm, newja);
|
||||||
|
if(closeTag != null)
|
||||||
|
{
|
||||||
|
if(!closeTag.equals(tagName))
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Mismatched '" + tagName + "' and '" + closeTag + "'");
|
||||||
|
}
|
||||||
|
tagName = null;
|
||||||
|
if(!arrayForm && newja.length() > 0)
|
||||||
|
{
|
||||||
|
newjo.put("childNodes", newja);
|
||||||
|
}
|
||||||
|
if(ja == null)
|
||||||
|
{
|
||||||
|
if(arrayForm)
|
||||||
|
{
|
||||||
|
return newja;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
return newjo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if(ja != null)
|
||||||
|
{
|
||||||
|
ja.put(token instanceof String ? XML.stringToValue((String) token) : token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||||
|
* JSONArray using the JsonML transform. Each XML tag is represented as a
|
||||||
|
* JSONArray in which the first element is the tag name. If the tag has
|
||||||
|
* attributes, then the second element will be JSONObject containing the
|
||||||
|
* name/value pairs. If the tag contains children, then strings and
|
||||||
|
* JSONArrays will represent the child tags. Comments, prologs, DTDs, and
|
||||||
|
* <code><[ [ ]]></code> are ignored.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* The source string.
|
||||||
|
* @return A JSONArray containing the structured data from the XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static JSONArray toJSONArray(String string) throws JSONException
|
||||||
|
{
|
||||||
|
return toJSONArray(new XMLTokener(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||||
|
* JSONArray using the JsonML transform. Each XML tag is represented as a
|
||||||
|
* JSONArray in which the first element is the tag name. If the tag has
|
||||||
|
* attributes, then the second element will be JSONObject containing the
|
||||||
|
* name/value pairs. If the tag contains children, then strings and
|
||||||
|
* JSONArrays will represent the child content and tags. Comments, prologs,
|
||||||
|
* DTDs, and <code><[ [ ]]></code> are ignored.
|
||||||
|
*
|
||||||
|
* @param x
|
||||||
|
* An XMLTokener.
|
||||||
|
* @return A JSONArray containing the structured data from the XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static JSONArray toJSONArray(XMLTokener x) throws JSONException
|
||||||
|
{
|
||||||
|
return (JSONArray) parse(x, true, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||||
|
* JSONObject using the JsonML transform. Each XML tag is represented as a
|
||||||
|
* JSONObject with a "tagName" property. If the tag has attributes, then the
|
||||||
|
* attributes will be in the JSONObject as properties. If the tag contains
|
||||||
|
* children, the object will have a "childNodes" property which will be an
|
||||||
|
* array of strings and JsonML JSONObjects.
|
||||||
|
*
|
||||||
|
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||||
|
*
|
||||||
|
* @param x
|
||||||
|
* An XMLTokener of the XML source text.
|
||||||
|
* @return A JSONObject containing the structured data from the XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static JSONObject toJSONObject(XMLTokener x) throws JSONException
|
||||||
|
{
|
||||||
|
return (JSONObject) parse(x, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||||
|
* JSONObject using the JsonML transform. Each XML tag is represented as a
|
||||||
|
* JSONObject with a "tagName" property. If the tag has attributes, then the
|
||||||
|
* attributes will be in the JSONObject as properties. If the tag contains
|
||||||
|
* children, the object will have a "childNodes" property which will be an
|
||||||
|
* array of strings and JsonML JSONObjects.
|
||||||
|
*
|
||||||
|
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* The XML source text.
|
||||||
|
* @return A JSONObject containing the structured data from the XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static JSONObject toJSONObject(String string) throws JSONException
|
||||||
|
{
|
||||||
|
return toJSONObject(new XMLTokener(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the JSONML transformation, making an XML text from a JSONArray.
|
||||||
|
*
|
||||||
|
* @param ja
|
||||||
|
* A JSONArray.
|
||||||
|
* @return An XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static String toString(JSONArray ja) throws JSONException
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
JSONObject jo;
|
||||||
|
String key;
|
||||||
|
Iterator<String> keys;
|
||||||
|
int length;
|
||||||
|
Object object;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String tagName;
|
||||||
|
String value;
|
||||||
|
|
||||||
|
// Emit <tagName
|
||||||
|
|
||||||
|
tagName = ja.getString(0);
|
||||||
|
XML.noSpace(tagName);
|
||||||
|
tagName = XML.escape(tagName);
|
||||||
|
sb.append('<');
|
||||||
|
sb.append(tagName);
|
||||||
|
|
||||||
|
object = ja.opt(1);
|
||||||
|
if(object instanceof JSONObject)
|
||||||
|
{
|
||||||
|
i = 2;
|
||||||
|
jo = (JSONObject) object;
|
||||||
|
|
||||||
|
// Emit the attributes
|
||||||
|
|
||||||
|
keys = jo.keys();
|
||||||
|
while(keys.hasNext())
|
||||||
|
{
|
||||||
|
key = keys.next();
|
||||||
|
XML.noSpace(key);
|
||||||
|
value = jo.optString(key);
|
||||||
|
if(value != null)
|
||||||
|
{
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(XML.escape(key));
|
||||||
|
sb.append('=');
|
||||||
|
sb.append('"');
|
||||||
|
sb.append(XML.escape(value));
|
||||||
|
sb.append('"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit content in body
|
||||||
|
|
||||||
|
length = ja.length();
|
||||||
|
if(i >= length)
|
||||||
|
{
|
||||||
|
sb.append('/');
|
||||||
|
sb.append('>');
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append('>');
|
||||||
|
do
|
||||||
|
{
|
||||||
|
object = ja.get(i);
|
||||||
|
i += 1;
|
||||||
|
if(object != null)
|
||||||
|
{
|
||||||
|
if(object instanceof String)
|
||||||
|
{
|
||||||
|
sb.append(XML.escape(object.toString()));
|
||||||
|
} else if(object instanceof JSONObject)
|
||||||
|
{
|
||||||
|
sb.append(toString((JSONObject) object));
|
||||||
|
} else if(object instanceof JSONArray)
|
||||||
|
{
|
||||||
|
sb.append(toString((JSONArray) object));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(object.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while(i < length);
|
||||||
|
sb.append('<');
|
||||||
|
sb.append('/');
|
||||||
|
sb.append(tagName);
|
||||||
|
sb.append('>');
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the JSONML transformation, making an XML text from a JSONObject.
|
||||||
|
* The JSONObject must contain a "tagName" property. If it has children,
|
||||||
|
* then it must have a "childNodes" property containing an array of objects.
|
||||||
|
* The other properties are attributes with string values.
|
||||||
|
*
|
||||||
|
* @param jo
|
||||||
|
* A JSONObject.
|
||||||
|
* @return An XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static String toString(JSONObject jo) throws JSONException
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int i;
|
||||||
|
JSONArray ja;
|
||||||
|
String key;
|
||||||
|
Iterator<String> keys;
|
||||||
|
int length;
|
||||||
|
Object object;
|
||||||
|
String tagName;
|
||||||
|
String value;
|
||||||
|
|
||||||
|
// Emit <tagName
|
||||||
|
|
||||||
|
tagName = jo.optString("tagName");
|
||||||
|
if(tagName == null)
|
||||||
|
{
|
||||||
|
return XML.escape(jo.toString());
|
||||||
|
}
|
||||||
|
XML.noSpace(tagName);
|
||||||
|
tagName = XML.escape(tagName);
|
||||||
|
sb.append('<');
|
||||||
|
sb.append(tagName);
|
||||||
|
|
||||||
|
// Emit the attributes
|
||||||
|
|
||||||
|
keys = jo.keys();
|
||||||
|
while(keys.hasNext())
|
||||||
|
{
|
||||||
|
key = keys.next();
|
||||||
|
if(!"tagName".equals(key) && !"childNodes".equals(key))
|
||||||
|
{
|
||||||
|
XML.noSpace(key);
|
||||||
|
value = jo.optString(key);
|
||||||
|
if(value != null)
|
||||||
|
{
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(XML.escape(key));
|
||||||
|
sb.append('=');
|
||||||
|
sb.append('"');
|
||||||
|
sb.append(XML.escape(value));
|
||||||
|
sb.append('"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit content in body
|
||||||
|
|
||||||
|
ja = jo.optJSONArray("childNodes");
|
||||||
|
if(ja == null)
|
||||||
|
{
|
||||||
|
sb.append('/');
|
||||||
|
sb.append('>');
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append('>');
|
||||||
|
length = ja.length();
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
object = ja.get(i);
|
||||||
|
if(object != null)
|
||||||
|
{
|
||||||
|
if(object instanceof String)
|
||||||
|
{
|
||||||
|
sb.append(XML.escape(object.toString()));
|
||||||
|
} else if(object instanceof JSONObject)
|
||||||
|
{
|
||||||
|
sb.append(toString((JSONObject) object));
|
||||||
|
} else if(object instanceof JSONArray)
|
||||||
|
{
|
||||||
|
sb.append(toString((JSONArray) object));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(object.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append('<');
|
||||||
|
sb.append('/');
|
||||||
|
sb.append(tagName);
|
||||||
|
sb.append('>');
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
2072
src/main/java/ninja/bytecode/iris/util/JSONObject.java
Normal file
2072
src/main/java/ninja/bytecode/iris/util/JSONObject.java
Normal file
File diff suppressed because it is too large
Load Diff
21
src/main/java/ninja/bytecode/iris/util/JSONString.java
Normal file
21
src/main/java/ninja/bytecode/iris/util/JSONString.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
|
||||||
|
* method so that a class can change the behavior of
|
||||||
|
* <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>, and
|
||||||
|
* <code>JSONWriter.value(</code>Object<code>)</code>. The
|
||||||
|
* <code>toJSONString</code> method will be used instead of the default behavior
|
||||||
|
* of using the Object's <code>toString()</code> method and quoting the result.
|
||||||
|
*/
|
||||||
|
public interface JSONString
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The <code>toJSONString</code> method allows a class to produce its own
|
||||||
|
* JSON serialization.
|
||||||
|
*
|
||||||
|
* @return A strictly syntactically correct JSON text.
|
||||||
|
*/
|
||||||
|
public String toJSONString();
|
||||||
|
}
|
86
src/main/java/ninja/bytecode/iris/util/JSONStringer.java
Normal file
86
src/main/java/ninja/bytecode/iris/util/JSONStringer.java
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2006 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSONStringer provides a quick and convenient way of producing JSON text. The
|
||||||
|
* texts produced strictly conform to JSON syntax rules. No whitespace is added,
|
||||||
|
* so the results are ready for transmission or storage. Each instance of
|
||||||
|
* JSONStringer can produce one JSON text.
|
||||||
|
* <p>
|
||||||
|
* A JSONStringer instance provides a <code>value</code> method for appending
|
||||||
|
* values to the text, and a <code>key</code> method for adding keys before
|
||||||
|
* values in objects. There are <code>array</code> and <code>endArray</code>
|
||||||
|
* methods that make and bound array values, and <code>object</code> and
|
||||||
|
* <code>endObject</code> methods which make and bound object values. All of
|
||||||
|
* these methods return the JSONWriter instance, permitting cascade style. For
|
||||||
|
* example,
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* myString = new JSONStringer().object().key("JSON").value("Hello, World!").endObject().toString();
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* which produces the string
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {"JSON":"Hello, World!"}
|
||||||
|
* </pre>
|
||||||
|
* <p>
|
||||||
|
* The first method called must be <code>array</code> or <code>object</code>.
|
||||||
|
* There are no methods for adding commas or colons. JSONStringer adds them for
|
||||||
|
* you. Objects and arrays can be nested up to 20 levels deep.
|
||||||
|
* <p>
|
||||||
|
* This can sometimes be easier than using a JSONObject to build a string.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2008-09-18
|
||||||
|
*/
|
||||||
|
public class JSONStringer extends JSONWriter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Make a fresh JSONStringer. It can be used to build one JSON text.
|
||||||
|
*/
|
||||||
|
public JSONStringer()
|
||||||
|
{
|
||||||
|
super(new StringWriter());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the JSON text. This method is used to obtain the product of the
|
||||||
|
* JSONStringer instance. It will return <code>null</code> if there was a
|
||||||
|
* problem in the construction of the JSON text (such as the calls to
|
||||||
|
* <code>array</code> were not properly balanced with calls to
|
||||||
|
* <code>endArray</code>).
|
||||||
|
*
|
||||||
|
* @return The JSON text.
|
||||||
|
*/
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return this.mode == 'd' ? this.writer.toString() : null;
|
||||||
|
}
|
||||||
|
}
|
504
src/main/java/ninja/bytecode/iris/util/JSONTokener.java
Normal file
504
src/main/java/ninja/bytecode/iris/util/JSONTokener.java
Normal file
@ -0,0 +1,504 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2002 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A JSONTokener takes a source string and extracts characters and tokens from
|
||||||
|
* it. It is used by the JSONObject and JSONArray constructors to parse JSON
|
||||||
|
* source strings.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class JSONTokener
|
||||||
|
{
|
||||||
|
|
||||||
|
private long character;
|
||||||
|
private boolean eof;
|
||||||
|
private long index;
|
||||||
|
private long line;
|
||||||
|
private char previous;
|
||||||
|
private Reader reader;
|
||||||
|
private boolean usePrevious;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a JSONTokener from a Reader.
|
||||||
|
*
|
||||||
|
* @param reader
|
||||||
|
* A reader.
|
||||||
|
*/
|
||||||
|
public JSONTokener(Reader reader)
|
||||||
|
{
|
||||||
|
this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
|
||||||
|
this.eof = false;
|
||||||
|
this.usePrevious = false;
|
||||||
|
this.previous = 0;
|
||||||
|
this.index = 0;
|
||||||
|
this.character = 1;
|
||||||
|
this.line = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a JSONTokener from an InputStream.
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* The source.
|
||||||
|
*/
|
||||||
|
public JSONTokener(InputStream inputStream) throws JSONException
|
||||||
|
{
|
||||||
|
this(new InputStreamReader(inputStream));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a JSONTokener from a string.
|
||||||
|
*
|
||||||
|
* @param s
|
||||||
|
* A source string.
|
||||||
|
*/
|
||||||
|
public JSONTokener(String s)
|
||||||
|
{
|
||||||
|
this(new StringReader(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Back up one character. This provides a sort of lookahead capability, so
|
||||||
|
* that you can test for a digit or letter before attempting to parse the
|
||||||
|
* next number or identifier.
|
||||||
|
*/
|
||||||
|
public void back() throws JSONException
|
||||||
|
{
|
||||||
|
if(this.usePrevious || this.index <= 0)
|
||||||
|
{
|
||||||
|
throw new JSONException("Stepping back two steps is not supported");
|
||||||
|
}
|
||||||
|
this.index -= 1;
|
||||||
|
this.character -= 1;
|
||||||
|
this.usePrevious = true;
|
||||||
|
this.eof = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the hex value of a character (base16).
|
||||||
|
*
|
||||||
|
* @param c
|
||||||
|
* A character between '0' and '9' or between 'A' and 'F' or
|
||||||
|
* between 'a' and 'f'.
|
||||||
|
* @return An int between 0 and 15, or -1 if c was not a hex digit.
|
||||||
|
*/
|
||||||
|
public static int dehexchar(char c)
|
||||||
|
{
|
||||||
|
if(c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
return c - '0';
|
||||||
|
}
|
||||||
|
if(c >= 'A' && c <= 'F')
|
||||||
|
{
|
||||||
|
return c - ('A' - 10);
|
||||||
|
}
|
||||||
|
if(c >= 'a' && c <= 'f')
|
||||||
|
{
|
||||||
|
return c - ('a' - 10);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean end()
|
||||||
|
{
|
||||||
|
return this.eof && !this.usePrevious;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the source string still contains characters that next() can
|
||||||
|
* consume.
|
||||||
|
*
|
||||||
|
* @return true if not yet at the end of the source.
|
||||||
|
*/
|
||||||
|
public boolean more() throws JSONException
|
||||||
|
{
|
||||||
|
this.next();
|
||||||
|
if(this.end())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.back();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next character in the source string.
|
||||||
|
*
|
||||||
|
* @return The next character, or 0 if past the end of the source string.
|
||||||
|
*/
|
||||||
|
public char next() throws JSONException
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
if(this.usePrevious)
|
||||||
|
{
|
||||||
|
this.usePrevious = false;
|
||||||
|
c = this.previous;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = this.reader.read();
|
||||||
|
} catch(IOException exception)
|
||||||
|
{
|
||||||
|
throw new JSONException(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(c <= 0)
|
||||||
|
{ // End of stream
|
||||||
|
this.eof = true;
|
||||||
|
c = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.index += 1;
|
||||||
|
if(this.previous == '\r')
|
||||||
|
{
|
||||||
|
this.line += 1;
|
||||||
|
this.character = c == '\n' ? 0 : 1;
|
||||||
|
} else if(c == '\n')
|
||||||
|
{
|
||||||
|
this.line += 1;
|
||||||
|
this.character = 0;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
this.character += 1;
|
||||||
|
}
|
||||||
|
this.previous = (char) c;
|
||||||
|
return this.previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consume the next character, and check that it matches a specified
|
||||||
|
* character.
|
||||||
|
*
|
||||||
|
* @param c
|
||||||
|
* The character to match.
|
||||||
|
* @return The character.
|
||||||
|
* @throws JSONException
|
||||||
|
* if the character does not match.
|
||||||
|
*/
|
||||||
|
public char next(char c) throws JSONException
|
||||||
|
{
|
||||||
|
char n = this.next();
|
||||||
|
if(n != c)
|
||||||
|
{
|
||||||
|
throw this.syntaxError("Expected '" + c + "' and instead saw '" + n + "'");
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next n characters.
|
||||||
|
*
|
||||||
|
* @param n
|
||||||
|
* The number of characters to take.
|
||||||
|
* @return A string of n characters.
|
||||||
|
* @throws JSONException
|
||||||
|
* Substring bounds error if there are not n characters
|
||||||
|
* remaining in the source string.
|
||||||
|
*/
|
||||||
|
public String next(int n) throws JSONException
|
||||||
|
{
|
||||||
|
if(n == 0)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
char[] chars = new char[n];
|
||||||
|
int pos = 0;
|
||||||
|
|
||||||
|
while(pos < n)
|
||||||
|
{
|
||||||
|
chars[pos] = this.next();
|
||||||
|
if(this.end())
|
||||||
|
{
|
||||||
|
throw this.syntaxError("Substring bounds error");
|
||||||
|
}
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
return new String(chars);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next char in the string, skipping whitespace.
|
||||||
|
*
|
||||||
|
* @throws JSONException
|
||||||
|
* @return A character, or 0 if there are no more characters.
|
||||||
|
*/
|
||||||
|
public char nextClean() throws JSONException
|
||||||
|
{
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
char c = this.next();
|
||||||
|
if(c == 0 || c > ' ')
|
||||||
|
{
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the characters up to the next close quote character. Backslash
|
||||||
|
* processing is done. The formal JSON format does not allow strings in
|
||||||
|
* single quotes, but an implementation is allowed to accept them.
|
||||||
|
*
|
||||||
|
* @param quote
|
||||||
|
* The quoting character, either <code>"</code>
|
||||||
|
* <small>(double quote)</small> or <code>'</code>
|
||||||
|
* <small>(single quote)</small>.
|
||||||
|
* @return A String.
|
||||||
|
* @throws JSONException
|
||||||
|
* Unterminated string.
|
||||||
|
*/
|
||||||
|
public String nextString(char quote) throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = this.next();
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
case '\n':
|
||||||
|
case '\r':
|
||||||
|
throw this.syntaxError("Unterminated string");
|
||||||
|
case '\\':
|
||||||
|
c = this.next();
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case 'b':
|
||||||
|
sb.append('\b');
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
sb.append('\t');
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
sb.append('\n');
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
sb.append('\f');
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
sb.append('\r');
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
sb.append((char) Integer.parseInt(this.next(4), 16));
|
||||||
|
break;
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
case '\\':
|
||||||
|
case '/':
|
||||||
|
sb.append(c);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw this.syntaxError("Illegal escape.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if(c == quote)
|
||||||
|
{
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the text up but not including the specified character or the end of
|
||||||
|
* line, whichever comes first.
|
||||||
|
*
|
||||||
|
* @param delimiter
|
||||||
|
* A delimiter character.
|
||||||
|
* @return A string.
|
||||||
|
*/
|
||||||
|
public String nextTo(char delimiter) throws JSONException
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
char c = this.next();
|
||||||
|
if(c == delimiter || c == 0 || c == '\n' || c == '\r')
|
||||||
|
{
|
||||||
|
if(c != 0)
|
||||||
|
{
|
||||||
|
this.back();
|
||||||
|
}
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the text up but not including one of the specified delimiter
|
||||||
|
* characters or the end of line, whichever comes first.
|
||||||
|
*
|
||||||
|
* @param delimiters
|
||||||
|
* A set of delimiter characters.
|
||||||
|
* @return A string, trimmed.
|
||||||
|
*/
|
||||||
|
public String nextTo(String delimiters) throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = this.next();
|
||||||
|
if(delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r')
|
||||||
|
{
|
||||||
|
if(c != 0)
|
||||||
|
{
|
||||||
|
this.back();
|
||||||
|
}
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next value. The value can be a Boolean, Double, Integer,
|
||||||
|
* JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
|
||||||
|
*
|
||||||
|
* @throws JSONException
|
||||||
|
* If syntax error.
|
||||||
|
*
|
||||||
|
* @return An object.
|
||||||
|
*/
|
||||||
|
public Object nextValue() throws JSONException
|
||||||
|
{
|
||||||
|
char c = this.nextClean();
|
||||||
|
String string;
|
||||||
|
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
return this.nextString(c);
|
||||||
|
case '{':
|
||||||
|
this.back();
|
||||||
|
return new JSONObject(this);
|
||||||
|
case '[':
|
||||||
|
this.back();
|
||||||
|
return new JSONArray(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Handle unquoted text. This could be the values true, false, or null,
|
||||||
|
* or it can be a number. An implementation (such as this one) is
|
||||||
|
* allowed to also accept non-standard forms.
|
||||||
|
*
|
||||||
|
* Accumulate characters until we reach the end of the text or a
|
||||||
|
* formatting character.
|
||||||
|
*/
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
while(c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0)
|
||||||
|
{
|
||||||
|
sb.append(c);
|
||||||
|
c = this.next();
|
||||||
|
}
|
||||||
|
this.back();
|
||||||
|
|
||||||
|
string = sb.toString().trim();
|
||||||
|
if("".equals(string))
|
||||||
|
{
|
||||||
|
throw this.syntaxError("Missing value");
|
||||||
|
}
|
||||||
|
return JSONObject.stringToValue(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skip characters until the next character is the requested character. If
|
||||||
|
* the requested character is not found, no characters are skipped.
|
||||||
|
*
|
||||||
|
* @param to
|
||||||
|
* A character to skip to.
|
||||||
|
* @return The requested character, or zero if the requested character is
|
||||||
|
* not found.
|
||||||
|
*/
|
||||||
|
public char skipTo(char to) throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
long startIndex = this.index;
|
||||||
|
long startCharacter = this.character;
|
||||||
|
long startLine = this.line;
|
||||||
|
this.reader.mark(1000000);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
c = this.next();
|
||||||
|
if(c == 0)
|
||||||
|
{
|
||||||
|
this.reader.reset();
|
||||||
|
this.index = startIndex;
|
||||||
|
this.character = startCharacter;
|
||||||
|
this.line = startLine;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
} while(c != to);
|
||||||
|
} catch(IOException exception)
|
||||||
|
{
|
||||||
|
throw new JSONException(exception);
|
||||||
|
}
|
||||||
|
this.back();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a JSONException to signal a syntax error.
|
||||||
|
*
|
||||||
|
* @param message
|
||||||
|
* The error message.
|
||||||
|
* @return A JSONException object, suitable for throwing
|
||||||
|
*/
|
||||||
|
public JSONException syntaxError(String message)
|
||||||
|
{
|
||||||
|
return new JSONException(message + this.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a printable string of this JSONTokener.
|
||||||
|
*
|
||||||
|
* @return " at {index} [character {character} line {line}]"
|
||||||
|
*/
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return " at " + this.index + " [character " + this.character + " line " + this.line + "]";
|
||||||
|
}
|
||||||
|
}
|
388
src/main/java/ninja/bytecode/iris/util/JSONWriter.java
Normal file
388
src/main/java/ninja/bytecode/iris/util/JSONWriter.java
Normal file
@ -0,0 +1,388 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2006 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSONWriter provides a quick and convenient way of producing JSON text. The
|
||||||
|
* texts produced strictly conform to JSON syntax rules. No whitespace is added,
|
||||||
|
* so the results are ready for transmission or storage. Each instance of
|
||||||
|
* JSONWriter can produce one JSON text.
|
||||||
|
* <p>
|
||||||
|
* A JSONWriter instance provides a <code>value</code> method for appending
|
||||||
|
* values to the text, and a <code>key</code> method for adding keys before
|
||||||
|
* values in objects. There are <code>array</code> and <code>endArray</code>
|
||||||
|
* methods that make and bound array values, and <code>object</code> and
|
||||||
|
* <code>endObject</code> methods which make and bound object values. All of
|
||||||
|
* these methods return the JSONWriter instance, permitting a cascade style. For
|
||||||
|
* example,
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* new JSONWriter(myWriter).object().key("JSON").value("Hello, World!").endObject();
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* which writes
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* {"JSON":"Hello, World!"}
|
||||||
|
* </pre>
|
||||||
|
* <p>
|
||||||
|
* The first method called must be <code>array</code> or <code>object</code>.
|
||||||
|
* There are no methods for adding commas or colons. JSONWriter adds them for
|
||||||
|
* you. Objects and arrays can be nested up to 20 levels deep.
|
||||||
|
* <p>
|
||||||
|
* This can sometimes be easier than using a JSONObject to build a string.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2011-11-24
|
||||||
|
*/
|
||||||
|
public class JSONWriter
|
||||||
|
{
|
||||||
|
private static final int maxdepth = 200;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The comma flag determines if a comma should be output before the next
|
||||||
|
* value.
|
||||||
|
*/
|
||||||
|
private boolean comma;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current mode. Values: 'a' (array), 'd' (done), 'i' (initial), 'k'
|
||||||
|
* (key), 'o' (object).
|
||||||
|
*/
|
||||||
|
protected char mode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The object/array stack.
|
||||||
|
*/
|
||||||
|
private final JSONObject stack[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The stack top index. A value of 0 indicates that the stack is empty.
|
||||||
|
*/
|
||||||
|
private int top;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The writer that will receive the output.
|
||||||
|
*/
|
||||||
|
protected Writer writer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a fresh JSONWriter. It can be used to build one JSON text.
|
||||||
|
*/
|
||||||
|
public JSONWriter(Writer w)
|
||||||
|
{
|
||||||
|
this.comma = false;
|
||||||
|
this.mode = 'i';
|
||||||
|
this.stack = new JSONObject[maxdepth];
|
||||||
|
this.top = 0;
|
||||||
|
this.writer = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a value.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* A string value.
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If the value is out of sequence.
|
||||||
|
*/
|
||||||
|
private JSONWriter append(String string) throws JSONException
|
||||||
|
{
|
||||||
|
if(string == null)
|
||||||
|
{
|
||||||
|
throw new JSONException("Null pointer");
|
||||||
|
}
|
||||||
|
if(this.mode == 'o' || this.mode == 'a')
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if(this.comma && this.mode == 'a')
|
||||||
|
{
|
||||||
|
this.writer.write(',');
|
||||||
|
}
|
||||||
|
this.writer.write(string);
|
||||||
|
} catch(IOException e)
|
||||||
|
{
|
||||||
|
throw new JSONException(e);
|
||||||
|
}
|
||||||
|
if(this.mode == 'o')
|
||||||
|
{
|
||||||
|
this.mode = 'k';
|
||||||
|
}
|
||||||
|
this.comma = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
throw new JSONException("Value out of sequence.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begin appending a new array. All values until the balancing
|
||||||
|
* <code>endArray</code> will be appended to this array. The
|
||||||
|
* <code>endArray</code> method must be called to mark the array's end.
|
||||||
|
*
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If the nesting is too deep, or if the object is started in
|
||||||
|
* the wrong place (for example as a key or after the end of the
|
||||||
|
* outermost array or object).
|
||||||
|
*/
|
||||||
|
public JSONWriter array() throws JSONException
|
||||||
|
{
|
||||||
|
if(this.mode == 'i' || this.mode == 'o' || this.mode == 'a')
|
||||||
|
{
|
||||||
|
this.push(null);
|
||||||
|
this.append("[");
|
||||||
|
this.comma = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
throw new JSONException("Misplaced array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End something.
|
||||||
|
*
|
||||||
|
* @param mode
|
||||||
|
* Mode
|
||||||
|
* @param c
|
||||||
|
* Closing character
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If unbalanced.
|
||||||
|
*/
|
||||||
|
private JSONWriter end(char mode, char c) throws JSONException
|
||||||
|
{
|
||||||
|
if(this.mode != mode)
|
||||||
|
{
|
||||||
|
throw new JSONException(mode == 'a' ? "Misplaced endArray." : "Misplaced endObject.");
|
||||||
|
}
|
||||||
|
this.pop(mode);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.writer.write(c);
|
||||||
|
} catch(IOException e)
|
||||||
|
{
|
||||||
|
throw new JSONException(e);
|
||||||
|
}
|
||||||
|
this.comma = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End an array. This method most be called to balance calls to
|
||||||
|
* <code>array</code>.
|
||||||
|
*
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If incorrectly nested.
|
||||||
|
*/
|
||||||
|
public JSONWriter endArray() throws JSONException
|
||||||
|
{
|
||||||
|
return this.end('a', ']');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End an object. This method most be called to balance calls to
|
||||||
|
* <code>object</code>.
|
||||||
|
*
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If incorrectly nested.
|
||||||
|
*/
|
||||||
|
public JSONWriter endObject() throws JSONException
|
||||||
|
{
|
||||||
|
return this.end('k', '}');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a key. The key will be associated with the next value. In an
|
||||||
|
* object, every value must be preceded by a key.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* A key string.
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If the key is out of place. For example, keys do not belong
|
||||||
|
* in arrays or if the key is null.
|
||||||
|
*/
|
||||||
|
public JSONWriter key(String string) throws JSONException
|
||||||
|
{
|
||||||
|
if(string == null)
|
||||||
|
{
|
||||||
|
throw new JSONException("Null key.");
|
||||||
|
}
|
||||||
|
if(this.mode == 'k')
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.stack[this.top - 1].putOnce(string, Boolean.TRUE);
|
||||||
|
if(this.comma)
|
||||||
|
{
|
||||||
|
this.writer.write(',');
|
||||||
|
}
|
||||||
|
this.writer.write(JSONObject.quote(string));
|
||||||
|
this.writer.write(':');
|
||||||
|
this.comma = false;
|
||||||
|
this.mode = 'o';
|
||||||
|
return this;
|
||||||
|
} catch(IOException e)
|
||||||
|
{
|
||||||
|
throw new JSONException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new JSONException("Misplaced key.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begin appending a new object. All keys and values until the balancing
|
||||||
|
* <code>endObject</code> will be appended to this object. The
|
||||||
|
* <code>endObject</code> method must be called to mark the object's end.
|
||||||
|
*
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If the nesting is too deep, or if the object is started in
|
||||||
|
* the wrong place (for example as a key or after the end of the
|
||||||
|
* outermost array or object).
|
||||||
|
*/
|
||||||
|
public JSONWriter object() throws JSONException
|
||||||
|
{
|
||||||
|
if(this.mode == 'i')
|
||||||
|
{
|
||||||
|
this.mode = 'o';
|
||||||
|
}
|
||||||
|
if(this.mode == 'o' || this.mode == 'a')
|
||||||
|
{
|
||||||
|
this.append("{");
|
||||||
|
this.push(new JSONObject());
|
||||||
|
this.comma = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
throw new JSONException("Misplaced object.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop an array or object scope.
|
||||||
|
*
|
||||||
|
* @param c
|
||||||
|
* The scope to close.
|
||||||
|
* @throws JSONException
|
||||||
|
* If nesting is wrong.
|
||||||
|
*/
|
||||||
|
private void pop(char c) throws JSONException
|
||||||
|
{
|
||||||
|
if(this.top <= 0)
|
||||||
|
{
|
||||||
|
throw new JSONException("Nesting error.");
|
||||||
|
}
|
||||||
|
char m = this.stack[this.top - 1] == null ? 'a' : 'k';
|
||||||
|
if(m != c)
|
||||||
|
{
|
||||||
|
throw new JSONException("Nesting error.");
|
||||||
|
}
|
||||||
|
this.top -= 1;
|
||||||
|
this.mode = this.top == 0 ? 'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push an array or object scope.
|
||||||
|
*
|
||||||
|
* @param jo
|
||||||
|
* The scope to open.
|
||||||
|
* @throws JSONException
|
||||||
|
* If nesting is too deep.
|
||||||
|
*/
|
||||||
|
private void push(JSONObject jo) throws JSONException
|
||||||
|
{
|
||||||
|
if(this.top >= maxdepth)
|
||||||
|
{
|
||||||
|
throw new JSONException("Nesting too deep.");
|
||||||
|
}
|
||||||
|
this.stack[this.top] = jo;
|
||||||
|
this.mode = jo == null ? 'a' : 'k';
|
||||||
|
this.top += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append either the value <code>true</code> or the value <code>false</code>
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @param b
|
||||||
|
* A boolean.
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public JSONWriter value(boolean b) throws JSONException
|
||||||
|
{
|
||||||
|
return this.append(b ? "true" : "false");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a double value.
|
||||||
|
*
|
||||||
|
* @param d
|
||||||
|
* A double.
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If the number is not finite.
|
||||||
|
*/
|
||||||
|
public JSONWriter value(double d) throws JSONException
|
||||||
|
{
|
||||||
|
return this.value(new Double(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a long value.
|
||||||
|
*
|
||||||
|
* @param l
|
||||||
|
* A long.
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public JSONWriter value(long l) throws JSONException
|
||||||
|
{
|
||||||
|
return this.append(Long.toString(l));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append an object value.
|
||||||
|
*
|
||||||
|
* @param object
|
||||||
|
* The object to append. It can be null, or a Boolean, Number,
|
||||||
|
* String, JSONObject, or JSONArray, or an object that implements
|
||||||
|
* JSONString.
|
||||||
|
* @return this
|
||||||
|
* @throws JSONException
|
||||||
|
* If the value is out of sequence.
|
||||||
|
*/
|
||||||
|
public JSONWriter value(Object object) throws JSONException
|
||||||
|
{
|
||||||
|
return this.append(JSONObject.valueToString(object));
|
||||||
|
}
|
||||||
|
}
|
642
src/main/java/ninja/bytecode/iris/util/KList.java
Normal file
642
src/main/java/ninja/bytecode/iris/util/KList.java
Normal file
@ -0,0 +1,642 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class KList<T> extends ArrayList<T> implements List<T>
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = -2892550695744823337L;
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public KList(T... ts)
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
add(ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList(Collection<T> values)
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
add(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList(Enumeration<T> e)
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
add(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chunker<T> chunk()
|
||||||
|
{
|
||||||
|
return new Chunker<T>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the last element
|
||||||
|
*/
|
||||||
|
public KList<T> removeLast()
|
||||||
|
{
|
||||||
|
remove(last());
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Queue<T> enqueue()
|
||||||
|
{
|
||||||
|
return Queue.create(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private KList<T> add(Enumeration<T> e)
|
||||||
|
{
|
||||||
|
while(e.hasMoreElements())
|
||||||
|
{
|
||||||
|
add(e.nextElement());
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList<T> add(Collection<T> values)
|
||||||
|
{
|
||||||
|
addAll(values);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Map out of this list where this list becomes the values of the
|
||||||
|
* returned map. You must specify each key for each value in this list. In the
|
||||||
|
* function, returning null will not add the keyval pair.
|
||||||
|
*
|
||||||
|
* @param <K>
|
||||||
|
* the inferred key type
|
||||||
|
* @param f
|
||||||
|
* the function
|
||||||
|
* @return the new map
|
||||||
|
*/
|
||||||
|
public <K> KMap<K, T> asValues(Function<T, K> f)
|
||||||
|
{
|
||||||
|
KMap<K, T> m = new KMap<K, T>();
|
||||||
|
forEach((i) -> m.putNonNull(f.apply(i), i));
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Map out of this list where this list becomes the keys of the
|
||||||
|
* returned map. You must specify each value for each key in this list. In the
|
||||||
|
* function, returning null will not add the keyval pair.
|
||||||
|
*
|
||||||
|
* @param <V>
|
||||||
|
* the inferred value type
|
||||||
|
* @param f
|
||||||
|
* the function
|
||||||
|
* @return the new map
|
||||||
|
*/
|
||||||
|
public <V> KMap<T, V> asKeys(Function<T, V> f)
|
||||||
|
{
|
||||||
|
KMap<T, V> m = new KMap<T, V>();
|
||||||
|
forEach((i) -> m.putNonNull(i, f.apply(i)));
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cut this list into targetCount sublists
|
||||||
|
*
|
||||||
|
* @param targetCount
|
||||||
|
* the target count of sublists
|
||||||
|
* @return the list of sublists
|
||||||
|
*/
|
||||||
|
public KList<KList<T>> divide(int targetCount)
|
||||||
|
{
|
||||||
|
return split(size() / targetCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split this list into a list of sublists with roughly targetSize elements of T
|
||||||
|
* per sublist
|
||||||
|
*
|
||||||
|
* @param targetSize
|
||||||
|
* the target size
|
||||||
|
* @return the list of sublists
|
||||||
|
*/
|
||||||
|
public KList<KList<T>> split(int targetSize)
|
||||||
|
{
|
||||||
|
targetSize = targetSize < 1 ? 1 : targetSize;
|
||||||
|
KList<KList<T>> gg = new KList<>();
|
||||||
|
KList<T> b = new KList<>();
|
||||||
|
|
||||||
|
for(T i : this)
|
||||||
|
{
|
||||||
|
if(b.size() >= targetSize)
|
||||||
|
{
|
||||||
|
gg.add(b.copy());
|
||||||
|
b.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
b.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!b.isEmpty())
|
||||||
|
{
|
||||||
|
gg.add(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrite this list by checking each value and changing the value (or not).
|
||||||
|
* Return null to remove the element in the function
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* the function
|
||||||
|
* @return the same list (not a copy)
|
||||||
|
*/
|
||||||
|
public KList<T> rewrite(Function<T, T> t)
|
||||||
|
{
|
||||||
|
KList<T> m = copy();
|
||||||
|
clear();
|
||||||
|
|
||||||
|
for(T i : m)
|
||||||
|
{
|
||||||
|
addNonNull(t.apply(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To array
|
||||||
|
*
|
||||||
|
* @return the array
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public T[] array()
|
||||||
|
{
|
||||||
|
return (T[]) toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a copy of this list
|
||||||
|
*
|
||||||
|
* @return the copy
|
||||||
|
*/
|
||||||
|
public KList<T> copy()
|
||||||
|
{
|
||||||
|
return new KList<T>().add(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shuffle the list
|
||||||
|
*
|
||||||
|
* @return the same list
|
||||||
|
*/
|
||||||
|
public KList<T> shuffle()
|
||||||
|
{
|
||||||
|
Collections.shuffle(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sort the list (based on toString comparison)
|
||||||
|
*
|
||||||
|
* @return the same list
|
||||||
|
*/
|
||||||
|
public KList<T> sort()
|
||||||
|
{
|
||||||
|
Collections.sort(this, (a, b) -> a.toString().compareTo(b.toString()));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse this list
|
||||||
|
*
|
||||||
|
* @return the same list
|
||||||
|
*/
|
||||||
|
public KList<T> reverse()
|
||||||
|
{
|
||||||
|
Collections.reverse(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return "[" + toString(", ") + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tostring with a seperator for each item in the list
|
||||||
|
*
|
||||||
|
* @param split
|
||||||
|
* the seperator
|
||||||
|
* @return the string representing this object
|
||||||
|
*/
|
||||||
|
public String toString(String split)
|
||||||
|
{
|
||||||
|
if(isEmpty())
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if(size() == 1)
|
||||||
|
{
|
||||||
|
return get(0).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
|
||||||
|
for(String i : toStringList())
|
||||||
|
{
|
||||||
|
b.append(split + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.toString().substring(split.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke tostring on each value in the list into a string list
|
||||||
|
*
|
||||||
|
* @return the string list
|
||||||
|
*/
|
||||||
|
public KList<String> toStringList()
|
||||||
|
{
|
||||||
|
return convert((t) -> t.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a list into another list type. Such as GList<Integer> to
|
||||||
|
* GList<String>. list.convert((i) -> "" + i);
|
||||||
|
*
|
||||||
|
* @param <V>
|
||||||
|
* @param converter
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public <V> KList<V> convert(Function<T, V> converter)
|
||||||
|
{
|
||||||
|
KList<V> v = new KList<V>();
|
||||||
|
forEach((t) -> v.addNonNull(converter.apply(t)));
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds T to the list, ignores if null
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* the value to add
|
||||||
|
* @return the same list
|
||||||
|
*/
|
||||||
|
public KList<T> addNonNull(T t)
|
||||||
|
{
|
||||||
|
if(t != null)
|
||||||
|
{
|
||||||
|
super.add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Swaps the values of index a and b. For example "hello", "world", "!" swap(1,
|
||||||
|
* 2) would change the list to "hello", "!", "world"
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the first index
|
||||||
|
* @param b
|
||||||
|
* the second index
|
||||||
|
* @return the same list (builder), not a copy
|
||||||
|
*/
|
||||||
|
public KList<T> swapIndexes(int a, int b)
|
||||||
|
{
|
||||||
|
T aa = remove(a);
|
||||||
|
T bb = get(b);
|
||||||
|
add(a, bb);
|
||||||
|
remove(b);
|
||||||
|
add(b, aa);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a number of elements from the list
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* the elements
|
||||||
|
* @return this list
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> remove(T... t)
|
||||||
|
{
|
||||||
|
for(T i : t)
|
||||||
|
{
|
||||||
|
super.remove(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add another glist's contents to this one (addall builder)
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* the list
|
||||||
|
* @return the same list
|
||||||
|
*/
|
||||||
|
public KList<T> add(KList<T> t)
|
||||||
|
{
|
||||||
|
super.addAll(t);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a number of values to this list
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* the list
|
||||||
|
* @return this list
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> add(T... t)
|
||||||
|
{
|
||||||
|
for(T i : t)
|
||||||
|
{
|
||||||
|
super.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this list has an index at the given index
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* the given index
|
||||||
|
* @return true if size > index
|
||||||
|
*/
|
||||||
|
public boolean hasIndex(int index)
|
||||||
|
{
|
||||||
|
return size() > index;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the last index of this list (size - 1)
|
||||||
|
*
|
||||||
|
* @return the last index of this list
|
||||||
|
*/
|
||||||
|
public int last()
|
||||||
|
{
|
||||||
|
return size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deduplicate this list by converting to linked hash set and back
|
||||||
|
*
|
||||||
|
* @return the deduplicated list
|
||||||
|
*/
|
||||||
|
public KList<T> dedupe()
|
||||||
|
{
|
||||||
|
return qclear().add(new LinkedHashSet<T>(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear this list (and return it)
|
||||||
|
*
|
||||||
|
* @return the same list
|
||||||
|
*/
|
||||||
|
public KList<T> qclear()
|
||||||
|
{
|
||||||
|
super.clear();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simply !isEmpty()
|
||||||
|
*
|
||||||
|
* @return true if this list has 1 or more element(s)
|
||||||
|
*/
|
||||||
|
public boolean hasElements()
|
||||||
|
{
|
||||||
|
return !isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop the first item off this list and return it
|
||||||
|
*
|
||||||
|
* @return the popped off item or null if the list is empty
|
||||||
|
*/
|
||||||
|
public T pop()
|
||||||
|
{
|
||||||
|
if(isEmpty())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop the last item off this list and return it
|
||||||
|
*
|
||||||
|
* @return the popped off item or null if the list is empty
|
||||||
|
*/
|
||||||
|
public T popLast()
|
||||||
|
{
|
||||||
|
if(isEmpty())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return remove(last());
|
||||||
|
}
|
||||||
|
|
||||||
|
public T popRandom()
|
||||||
|
{
|
||||||
|
if(isEmpty())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(size() == 1)
|
||||||
|
{
|
||||||
|
return pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
return remove(M.irand(0, last()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static KList<String> fromJSONAny(JSONArray oo)
|
||||||
|
{
|
||||||
|
KList<String> s = new KList<String>();
|
||||||
|
|
||||||
|
for(int i = 0; i < oo.length(); i++)
|
||||||
|
{
|
||||||
|
s.add(oo.get(i).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList<T> sub(int f, int t)
|
||||||
|
{
|
||||||
|
KList<T> g = new KList<>();
|
||||||
|
|
||||||
|
for(int i = f; i < M.min(size(), t); i++)
|
||||||
|
{
|
||||||
|
g.add(get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONArray toJSONStringArray()
|
||||||
|
{
|
||||||
|
JSONArray j = new JSONArray();
|
||||||
|
|
||||||
|
for(Object i : this)
|
||||||
|
{
|
||||||
|
j.put(i.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static KList<String> asStringList(List<?> oo)
|
||||||
|
{
|
||||||
|
KList<String> s = new KList<String>();
|
||||||
|
|
||||||
|
for(Object i : oo)
|
||||||
|
{
|
||||||
|
s.add(i.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(Object[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(int[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(double[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(float[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(byte[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(short[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(long[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public KList<T> forceAdd(boolean[] values)
|
||||||
|
{
|
||||||
|
for(Object i : values)
|
||||||
|
{
|
||||||
|
add((T) i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T middleValue()
|
||||||
|
{
|
||||||
|
return get(middleIndex());
|
||||||
|
}
|
||||||
|
|
||||||
|
private int middleIndex()
|
||||||
|
{
|
||||||
|
return size() % 2 == 0 ? (size() / 2) : ((size() / 2) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getRandom()
|
||||||
|
{
|
||||||
|
if(isEmpty())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(size() == 1)
|
||||||
|
{
|
||||||
|
return get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return get(M.irand(0, last()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList<T> qdel(T t)
|
||||||
|
{
|
||||||
|
remove(t);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
424
src/main/java/ninja/bytecode/iris/util/KMap.java
Normal file
424
src/main/java/ninja/bytecode/iris/util/KMap.java
Normal file
@ -0,0 +1,424 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class KMap<K, V> extends ConcurrentHashMap<K, V>
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 7288942695300448163L;
|
||||||
|
|
||||||
|
public KMap()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public KMap(KMap<K, V> gMap)
|
||||||
|
{
|
||||||
|
this();
|
||||||
|
put(gMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts a value into a map-value-list based on the key such that if GMap<K,
|
||||||
|
* GList<S>> where V is GList<S>
|
||||||
|
*
|
||||||
|
* @param <S>
|
||||||
|
* the list type in the value type
|
||||||
|
* @param k
|
||||||
|
* the key to look for
|
||||||
|
* @param vs
|
||||||
|
* the values to put into the list of the given key
|
||||||
|
* @return the same list (builder)
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <S> KMap<K, V> putValueList(K k, S... vs)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
KMap<K, KList<S>> s = (KMap<K, KList<S>>) this;
|
||||||
|
|
||||||
|
if(!s.containsKey(k))
|
||||||
|
{
|
||||||
|
s.put(k, new KList<S>());
|
||||||
|
}
|
||||||
|
|
||||||
|
s.get(k).add(vs);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a sorted list of keys from this map, based on the sorting order of
|
||||||
|
* the values.
|
||||||
|
*
|
||||||
|
* @return the value-sorted key list
|
||||||
|
*/
|
||||||
|
public KList<K> sortK()
|
||||||
|
{
|
||||||
|
KList<K> k = new KList<K>();
|
||||||
|
KList<V> v = v();
|
||||||
|
|
||||||
|
Collections.sort(v, new Comparator<V>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int compare(V v, V t1)
|
||||||
|
{
|
||||||
|
return v.toString().compareTo(t1.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for(V i : v)
|
||||||
|
{
|
||||||
|
for(K j : k())
|
||||||
|
{
|
||||||
|
if(get(j).equals(i))
|
||||||
|
{
|
||||||
|
k.add(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
k.dedupe();
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a sorted list of keys from this map, based on the sorting order of
|
||||||
|
* the values. Sorting is based on numerical values
|
||||||
|
*
|
||||||
|
* @return the value-sorted key list
|
||||||
|
*/
|
||||||
|
public KList<K> sortKNumber()
|
||||||
|
{
|
||||||
|
KList<K> k = new KList<K>();
|
||||||
|
KList<V> v = v();
|
||||||
|
|
||||||
|
Collections.sort(v, new Comparator<V>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int compare(V v, V t1)
|
||||||
|
{
|
||||||
|
Number n1 = (Number) v;
|
||||||
|
Number n2 = (Number) t1;
|
||||||
|
|
||||||
|
return (int) ((n1.doubleValue() - n2.doubleValue()) * 1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for(V i : v)
|
||||||
|
{
|
||||||
|
for(K j : k())
|
||||||
|
{
|
||||||
|
if(get(j).equals(i))
|
||||||
|
{
|
||||||
|
k.add(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
k.dedupe();
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put another map's values into this map
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* the map to insert
|
||||||
|
* @return this map (builder)
|
||||||
|
*/
|
||||||
|
public KMap<K, V> put(Map<K, V> m)
|
||||||
|
{
|
||||||
|
putAll(m);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a copy of this map
|
||||||
|
*
|
||||||
|
* @return the copied map
|
||||||
|
*/
|
||||||
|
public KMap<K, V> copy()
|
||||||
|
{
|
||||||
|
return new KMap<K, V>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loop through each keyvalue set (copy of it) with the map parameter
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* the function
|
||||||
|
* @return the same gmap
|
||||||
|
*/
|
||||||
|
public KMap<K, V> rewrite(Consumer3<K, V, KMap<K, V>> f)
|
||||||
|
{
|
||||||
|
KMap<K, V> m = copy();
|
||||||
|
|
||||||
|
for(K i : m.k())
|
||||||
|
{
|
||||||
|
f.accept(i, get(i), this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loop through each keyvalue set (copy of it)
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* the function
|
||||||
|
* @return the same gmap
|
||||||
|
*/
|
||||||
|
public KMap<K, V> each(Consumer2<K, V> f)
|
||||||
|
{
|
||||||
|
for(K i : k())
|
||||||
|
{
|
||||||
|
f.accept(i, get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flip the hashmap and flatten the value list even if there are multiple keys
|
||||||
|
*
|
||||||
|
* @return the flipped and flattened hashmap
|
||||||
|
*/
|
||||||
|
public KMap<V, K> flipFlatten()
|
||||||
|
{
|
||||||
|
KMap<V, KList<K>> f = flip();
|
||||||
|
KMap<V, K> m = new KMap<>();
|
||||||
|
|
||||||
|
for(V i : f.k())
|
||||||
|
{
|
||||||
|
m.putNonNull(i, m.isEmpty() ? null : m.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flip the hashmap so keys are now list-keys in the value position
|
||||||
|
*
|
||||||
|
* @return the flipped hashmap
|
||||||
|
*/
|
||||||
|
public KMap<V, KList<K>> flip()
|
||||||
|
{
|
||||||
|
KMap<V, KList<K>> flipped = new KMap<V, KList<K>>();
|
||||||
|
|
||||||
|
for(K i : keySet())
|
||||||
|
{
|
||||||
|
if(i == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!flipped.containsKey(get(i)))
|
||||||
|
{
|
||||||
|
flipped.put(get(i), new KList<K>());
|
||||||
|
}
|
||||||
|
|
||||||
|
flipped.get(get(i)).add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return flipped;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sort values based on the keys sorting order
|
||||||
|
*
|
||||||
|
* @return the values (sorted)
|
||||||
|
*/
|
||||||
|
public KList<V> sortV()
|
||||||
|
{
|
||||||
|
KList<V> v = new KList<V>();
|
||||||
|
KList<K> k = k();
|
||||||
|
|
||||||
|
Collections.sort(k, new Comparator<K>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int compare(K v, K t1)
|
||||||
|
{
|
||||||
|
return v.toString().compareTo(t1.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for(K i : k)
|
||||||
|
{
|
||||||
|
for(V j : v())
|
||||||
|
{
|
||||||
|
if(get(i).equals(j))
|
||||||
|
{
|
||||||
|
v.add(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
v.dedupe();
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public KList<V> sortVNoDedupe()
|
||||||
|
{
|
||||||
|
KList<V> v = new KList<V>();
|
||||||
|
KList<K> k = k();
|
||||||
|
|
||||||
|
Collections.sort(k, new Comparator<K>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int compare(K v, K t1)
|
||||||
|
{
|
||||||
|
return v.toString().compareTo(t1.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for(K i : k)
|
||||||
|
{
|
||||||
|
for(V j : v())
|
||||||
|
{
|
||||||
|
if(get(i).equals(j))
|
||||||
|
{
|
||||||
|
v.add(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a copy of this maps keys
|
||||||
|
*
|
||||||
|
* @return the keys
|
||||||
|
*/
|
||||||
|
public KList<K> k()
|
||||||
|
{
|
||||||
|
KList<K> k = new KList<K>();
|
||||||
|
Enumeration<K> kk = keys();
|
||||||
|
|
||||||
|
while(kk.hasMoreElements())
|
||||||
|
{
|
||||||
|
K kkk = kk.nextElement();
|
||||||
|
k.add(kkk);
|
||||||
|
}
|
||||||
|
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a copy of this maps values
|
||||||
|
*
|
||||||
|
* @return the values
|
||||||
|
*/
|
||||||
|
public KList<V> v()
|
||||||
|
{
|
||||||
|
return new KList<V>(values());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Still works as it normally should except it returns itself (builder)
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* the key
|
||||||
|
* @param value
|
||||||
|
* the value (single only supported)
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public KMap<K, V> qput(K key, V value)
|
||||||
|
{
|
||||||
|
super.put(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Works just like put, except it wont put anything unless the key and value are
|
||||||
|
* nonnull
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* the nonnull key
|
||||||
|
* @param value
|
||||||
|
* the nonnull value
|
||||||
|
* @return the same map
|
||||||
|
*/
|
||||||
|
public KMap<K, V> putNonNull(K key, V value)
|
||||||
|
{
|
||||||
|
if(key != null || value != null)
|
||||||
|
{
|
||||||
|
put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V putThen(K key, V valueIfKeyNotPresent)
|
||||||
|
{
|
||||||
|
if(!containsKey(key))
|
||||||
|
{
|
||||||
|
put(key, valueIfKeyNotPresent);
|
||||||
|
}
|
||||||
|
|
||||||
|
return get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear this map and return it
|
||||||
|
*
|
||||||
|
* @return the cleared map
|
||||||
|
*/
|
||||||
|
public KMap<K, V> qclear()
|
||||||
|
{
|
||||||
|
super.clear();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert this map to keypairs
|
||||||
|
*
|
||||||
|
* @return the keypair list
|
||||||
|
*/
|
||||||
|
public KList<KeyPair<K, V>> keypair()
|
||||||
|
{
|
||||||
|
KList<KeyPair<K, V>> g = new KList<>();
|
||||||
|
each((k, v) -> g.add(new KeyPair<K, V>(k, v)));
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a keypair queue
|
||||||
|
*
|
||||||
|
* @return the queue
|
||||||
|
*/
|
||||||
|
public Queue<KeyPair<K, V>> enqueue()
|
||||||
|
{
|
||||||
|
return Queue.create(keypair());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a key queue
|
||||||
|
*
|
||||||
|
* @return the queue
|
||||||
|
*/
|
||||||
|
public Queue<K> enqueueKeys()
|
||||||
|
{
|
||||||
|
return Queue.create(k());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a value queue
|
||||||
|
*
|
||||||
|
* @return the queue
|
||||||
|
*/
|
||||||
|
public Queue<V> enqueueValues()
|
||||||
|
{
|
||||||
|
return Queue.create(v());
|
||||||
|
}
|
||||||
|
}
|
29
src/main/java/ninja/bytecode/iris/util/KSet.java
Normal file
29
src/main/java/ninja/bytecode/iris/util/KSet.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
public class KSet<T> extends HashSet<T>
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public KSet()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public KSet(Collection<? extends T> c)
|
||||||
|
{
|
||||||
|
super(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KSet(int initialCapacity, float loadFactor)
|
||||||
|
{
|
||||||
|
super(initialCapacity, loadFactor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KSet(int initialCapacity)
|
||||||
|
{
|
||||||
|
super(initialCapacity);
|
||||||
|
}
|
||||||
|
}
|
45
src/main/java/ninja/bytecode/iris/util/KeyPair.java
Normal file
45
src/main/java/ninja/bytecode/iris/util/KeyPair.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a keypair
|
||||||
|
* @author cyberpwn
|
||||||
|
*
|
||||||
|
* @param <K> the key type
|
||||||
|
* @param <V> the value type
|
||||||
|
*/
|
||||||
|
public class KeyPair<K, V>
|
||||||
|
{
|
||||||
|
private K k;
|
||||||
|
private V v;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a keypair
|
||||||
|
* @param k the key
|
||||||
|
* @param v the value
|
||||||
|
*/
|
||||||
|
public KeyPair(K k, V v)
|
||||||
|
{
|
||||||
|
this.k = k;
|
||||||
|
this.v = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public K getK()
|
||||||
|
{
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setK(K k)
|
||||||
|
{
|
||||||
|
this.k = k;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getV()
|
||||||
|
{
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setV(V v)
|
||||||
|
{
|
||||||
|
this.v = v;
|
||||||
|
}
|
||||||
|
}
|
34
src/main/java/ninja/bytecode/iris/util/Looper.java
Normal file
34
src/main/java/ninja/bytecode/iris/util/Looper.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public abstract class Looper extends Thread
|
||||||
|
{
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
while(!interrupted())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
long m = loop();
|
||||||
|
|
||||||
|
if(m < 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(InterruptedException e)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract long loop();
|
||||||
|
}
|
443
src/main/java/ninja/bytecode/iris/util/M.java
Normal file
443
src/main/java/ninja/bytecode/iris/util/M.java
Normal file
@ -0,0 +1,443 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Math
|
||||||
|
*
|
||||||
|
* @author cyberpwn
|
||||||
|
*/
|
||||||
|
public class M
|
||||||
|
{
|
||||||
|
private static final int precision = 128;
|
||||||
|
private static final int modulus = 360 * precision;
|
||||||
|
private static final float[] sin = new float[modulus];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scales B by an external range change so that <br/>
|
||||||
|
* <br/>
|
||||||
|
* BMIN < B < BMAX <br/>
|
||||||
|
* AMIN < RESULT < AMAX <br/>
|
||||||
|
* <br/>
|
||||||
|
* So Given rangeScale(0, 20, 0, 10, 5) -> 10 <br/>
|
||||||
|
* 0 < 5 < 10 <br/>
|
||||||
|
* 0 < ? < 20 <br/>
|
||||||
|
* <br/>
|
||||||
|
* would return 10
|
||||||
|
*
|
||||||
|
* @param amin
|
||||||
|
* the resulting minimum
|
||||||
|
* @param amax
|
||||||
|
* the resulting maximum
|
||||||
|
* @param bmin
|
||||||
|
* the initial minimum
|
||||||
|
* @param bmax
|
||||||
|
* the initial maximum
|
||||||
|
* @param b
|
||||||
|
* the initial value
|
||||||
|
* @return the resulting value
|
||||||
|
*/
|
||||||
|
public static double rangeScale(double amin, double amax, double bmin, double bmax, double b)
|
||||||
|
{
|
||||||
|
return amin + ((amax - amin) * ((b - bmin) / (bmax - bmin)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the percent (inverse lerp) from "from" to "to" where "at".
|
||||||
|
*
|
||||||
|
* If from = 0 and to = 100 and at = 25 then it would return 0.25
|
||||||
|
*
|
||||||
|
* @param from
|
||||||
|
* the from
|
||||||
|
* @param to
|
||||||
|
* the to
|
||||||
|
* @param at
|
||||||
|
* the at
|
||||||
|
* @return the percent
|
||||||
|
*/
|
||||||
|
public static double lerpInverse(double from, double to, double at)
|
||||||
|
{
|
||||||
|
return M.rangeScale(0, 1, from, to, at);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linear interpolation from a to b where f is the percent across
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the first pos (0)
|
||||||
|
* @param b
|
||||||
|
* the second pos (1)
|
||||||
|
* @param f
|
||||||
|
* the percent
|
||||||
|
* @return the value
|
||||||
|
*/
|
||||||
|
public static double lerp(double a, double b, double f)
|
||||||
|
{
|
||||||
|
return a + (f * (b - a));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bilinear interpolation
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the first point (0, 0)
|
||||||
|
* @param b
|
||||||
|
* the second point (1, 0)
|
||||||
|
* @param c
|
||||||
|
* the third point (0, 1)
|
||||||
|
* @param d
|
||||||
|
* the fourth point (1, 1)
|
||||||
|
* @param tx
|
||||||
|
* the x
|
||||||
|
* @param ty
|
||||||
|
* the y
|
||||||
|
* @return the bilerped value
|
||||||
|
*/
|
||||||
|
public static double bilerp(double a, double b, double c, double d, double x, double y)
|
||||||
|
{
|
||||||
|
return lerp(lerp(a, b, x), lerp(c, d, x), y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trilinear interpolation
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the first point (0, 0, 0)
|
||||||
|
* @param b
|
||||||
|
* the second point (1, 0, 0)
|
||||||
|
* @param c
|
||||||
|
* the third point (0, 0, 1)
|
||||||
|
* @param d
|
||||||
|
* the fourth point (1, 0, 1)
|
||||||
|
* @param e
|
||||||
|
* the fifth point (0, 1, 0)
|
||||||
|
* @param f
|
||||||
|
* the sixth point (1, 1, 0)
|
||||||
|
* @param g
|
||||||
|
* the seventh point (0, 1, 1)
|
||||||
|
* @param h
|
||||||
|
* the eighth point (1, 1, 1)
|
||||||
|
* @param x
|
||||||
|
* the x
|
||||||
|
* @param y
|
||||||
|
* the y
|
||||||
|
* @param z
|
||||||
|
* the z
|
||||||
|
* @return the trilerped value
|
||||||
|
*/
|
||||||
|
public static double trilerp(double a, double b, double c, double d, double e, double f, double g, double h, double x, double y, double z)
|
||||||
|
{
|
||||||
|
return lerp(bilerp(a, b, c, d, x, y), bilerp(e, f, g, h, x, y), z);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clip a value
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* the value
|
||||||
|
* @param min
|
||||||
|
* the min
|
||||||
|
* @param max
|
||||||
|
* the max
|
||||||
|
* @return the clipped value
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T extends Number> T clip(T value, T min, T max)
|
||||||
|
{
|
||||||
|
return (T) Double.valueOf(Math.min(max.doubleValue(), Math.max(min.doubleValue(), value.doubleValue())));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get true or false based on random percent
|
||||||
|
*
|
||||||
|
* @param d
|
||||||
|
* between 0 and 1
|
||||||
|
* @return true if true
|
||||||
|
*/
|
||||||
|
public static boolean r(Double d)
|
||||||
|
{
|
||||||
|
if(d == null)
|
||||||
|
{
|
||||||
|
return Math.random() < 0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.random() < d;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the ticks per second from a time in nanoseconds, the rad can be used for
|
||||||
|
* multiple ticks
|
||||||
|
*
|
||||||
|
* @param ns
|
||||||
|
* the time in nanoseconds
|
||||||
|
* @param rad
|
||||||
|
* the radius of the time
|
||||||
|
* @return the ticks per second in double form
|
||||||
|
*/
|
||||||
|
public static double tps(long ns, int rad)
|
||||||
|
{
|
||||||
|
return (20.0 * (ns / 50000000.0)) / rad;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of ticks from a time in nanoseconds
|
||||||
|
*
|
||||||
|
* @param ns
|
||||||
|
* the nanoseconds
|
||||||
|
* @return the amount of ticks
|
||||||
|
*/
|
||||||
|
public static double ticksFromNS(long ns)
|
||||||
|
{
|
||||||
|
return (ns / 50000000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a random int from to (inclusive)
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* the from
|
||||||
|
* @param t
|
||||||
|
* the to
|
||||||
|
* @return the value
|
||||||
|
*/
|
||||||
|
public static int irand(int f, int t)
|
||||||
|
{
|
||||||
|
return f + (int) (Math.random() * ((t - f) + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a random float from to (inclusive)
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* the from
|
||||||
|
* @param t
|
||||||
|
* the to
|
||||||
|
* @return the value
|
||||||
|
*/
|
||||||
|
public static float frand(float f, float t)
|
||||||
|
{
|
||||||
|
return f + (float) (Math.random() * ((t - f) + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a random double from to (inclusive)
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* the from
|
||||||
|
* @param t
|
||||||
|
* the to
|
||||||
|
* @return the value
|
||||||
|
*/
|
||||||
|
public static double drand(double f, double t)
|
||||||
|
{
|
||||||
|
return f + (Math.random() * ((t - f) + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get system Nanoseconds
|
||||||
|
*
|
||||||
|
* @return nanoseconds (current)
|
||||||
|
*/
|
||||||
|
public static long ns()
|
||||||
|
{
|
||||||
|
return System.nanoTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current millisecond time
|
||||||
|
*
|
||||||
|
* @return milliseconds
|
||||||
|
*/
|
||||||
|
public static long ms()
|
||||||
|
{
|
||||||
|
return System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fast sin function
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the number
|
||||||
|
* @return the sin
|
||||||
|
*/
|
||||||
|
public static float sin(float a)
|
||||||
|
{
|
||||||
|
return sinLookup((int) (a * precision + 0.5f));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fast cos function
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the number
|
||||||
|
* @return the cos
|
||||||
|
*/
|
||||||
|
public static float cos(float a)
|
||||||
|
{
|
||||||
|
return sinLookup((int) ((a + 90f) * precision + 0.5f));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fast tan function
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* the number
|
||||||
|
* @return the tan
|
||||||
|
*/
|
||||||
|
public static float tan(float a)
|
||||||
|
{
|
||||||
|
float c = cos(a);
|
||||||
|
return sin(a) / (c == 0 ? 0.0000001f : c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Biggest number
|
||||||
|
*
|
||||||
|
* @param numbers
|
||||||
|
* the numbers
|
||||||
|
* @return the biggest one
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T extends Number> T max(T... doubles)
|
||||||
|
{
|
||||||
|
double max = Double.MIN_VALUE;
|
||||||
|
|
||||||
|
for(T i : doubles)
|
||||||
|
{
|
||||||
|
if(i.doubleValue() > max)
|
||||||
|
{
|
||||||
|
max = i.doubleValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (T) Double.valueOf(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Smallest number
|
||||||
|
*
|
||||||
|
* @param doubles
|
||||||
|
* the numbers
|
||||||
|
* @return the smallest one
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T extends Number> T min(T... doubles)
|
||||||
|
{
|
||||||
|
double min = Double.MAX_VALUE;
|
||||||
|
|
||||||
|
for(T i : doubles)
|
||||||
|
{
|
||||||
|
if(i.doubleValue() < min)
|
||||||
|
{
|
||||||
|
min = i.doubleValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (T) Double.valueOf(min);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates an expression using javascript engine and returns the double
|
||||||
|
* result. This can take variable parameters, so you need to define them.
|
||||||
|
* Parameters are defined as $[0-9]. For example evaluate("4$0/$1", 1, 2); This
|
||||||
|
* makes the expression (4x1)/2 == 2. Keep note that you must use 0-9, you
|
||||||
|
* cannot skip, or start at a number other than 0.
|
||||||
|
*
|
||||||
|
* @param expression
|
||||||
|
* the expression with variables
|
||||||
|
* @param args
|
||||||
|
* the arguments/variables
|
||||||
|
* @return the resulting double value
|
||||||
|
* @throws ScriptException
|
||||||
|
* ... gg
|
||||||
|
* @throws IndexOutOfBoundsException
|
||||||
|
* learn to count
|
||||||
|
*/
|
||||||
|
public static double evaluate(String expression, Double... args) throws ScriptException, IndexOutOfBoundsException
|
||||||
|
{
|
||||||
|
for(int i = 0; i < args.length; i++)
|
||||||
|
{
|
||||||
|
String current = "$" + i;
|
||||||
|
|
||||||
|
if(expression.contains(current))
|
||||||
|
{
|
||||||
|
expression = expression.replaceAll(Matcher.quoteReplacement(current), args[i] + "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return evaluate(expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates an expression using javascript engine and returns the double
|
||||||
|
*
|
||||||
|
* @param expression
|
||||||
|
* the mathimatical expression
|
||||||
|
* @return the double result
|
||||||
|
* @throws ScriptException
|
||||||
|
* ... gg
|
||||||
|
*/
|
||||||
|
public static double evaluate(String expression) throws ScriptException
|
||||||
|
{
|
||||||
|
ScriptEngineManager mgr = new ScriptEngineManager();
|
||||||
|
ScriptEngine scriptEngine = mgr.getEngineByName("JavaScript");
|
||||||
|
|
||||||
|
return Double.valueOf(scriptEngine.eval(expression).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* is the number "is" within from-to
|
||||||
|
*
|
||||||
|
* @param from
|
||||||
|
* the lower end
|
||||||
|
* @param to
|
||||||
|
* the upper end
|
||||||
|
* @param is
|
||||||
|
* the check
|
||||||
|
* @return true if its within
|
||||||
|
*/
|
||||||
|
public static boolean within(int from, int to, int is)
|
||||||
|
{
|
||||||
|
return is >= from && is <= to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount of days past since the epoch time (1970 jan 1 utc)
|
||||||
|
*
|
||||||
|
* @return the epoch days
|
||||||
|
*/
|
||||||
|
public static long epochDays()
|
||||||
|
{
|
||||||
|
return epochDays(M.ms());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount of days past since the epoch time (1970 jan 1 utc)
|
||||||
|
*
|
||||||
|
* @param ms
|
||||||
|
* the time in milliseconds
|
||||||
|
* @return the epoch days
|
||||||
|
*/
|
||||||
|
private static long epochDays(long ms)
|
||||||
|
{
|
||||||
|
return ms / 1000 / 60 / 60 / 24;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
for(int i = 0; i < sin.length; i++)
|
||||||
|
{
|
||||||
|
sin[i] = (float) Math.sin((i * Math.PI) / (precision * 180));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float sinLookup(int a)
|
||||||
|
{
|
||||||
|
return a >= 0 ? sin[a % (modulus)] : -sin[-a % (modulus)];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public interface NastyFunction<T, R>
|
||||||
|
{
|
||||||
|
public R run(T t) throws Throwable;
|
||||||
|
}
|
6
src/main/java/ninja/bytecode/iris/util/NastyFuture.java
Normal file
6
src/main/java/ninja/bytecode/iris/util/NastyFuture.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public interface NastyFuture<R>
|
||||||
|
{
|
||||||
|
public R run() throws Throwable;
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public interface NastyRunnable
|
||||||
|
{
|
||||||
|
public void run() throws Throwable;
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface NoiseInjector
|
||||||
|
{
|
||||||
|
public double[] combine(double src, double value);
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface NoiseProvider
|
||||||
|
{
|
||||||
|
public double noise(double x, double z);
|
||||||
|
}
|
214
src/main/java/ninja/bytecode/iris/util/PolygonGenerator.java
Normal file
214
src/main/java/ninja/bytecode/iris/util/PolygonGenerator.java
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class PolygonGenerator
|
||||||
|
{
|
||||||
|
private double[] rarity;
|
||||||
|
private CNG[] gen;
|
||||||
|
private int bits;
|
||||||
|
private int possibilities;
|
||||||
|
private boolean useRarity;
|
||||||
|
|
||||||
|
public PolygonGenerator(RNG rng, int possibilities, double scale, int octaves, Function<CNG, CNG> factory)
|
||||||
|
{
|
||||||
|
useRarity = false;
|
||||||
|
bits = 1;
|
||||||
|
this.possibilities = possibilities;
|
||||||
|
|
||||||
|
while(Math.pow(2, bits) <= possibilities)
|
||||||
|
{
|
||||||
|
bits++;
|
||||||
|
}
|
||||||
|
|
||||||
|
bits++;
|
||||||
|
bits = bits > 32 ? 32 : bits;
|
||||||
|
rarity = new double[possibilities];
|
||||||
|
gen = new CNG[bits];
|
||||||
|
|
||||||
|
for(int i = 0; i < bits; i++)
|
||||||
|
{
|
||||||
|
gen[i] = new CNG(rng.nextParallelRNG(2118 + (i * 3305)), 1D, 1).scale(scale / possibilities);
|
||||||
|
gen[i] = factory.apply(gen[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PolygonGenerator useRarity()
|
||||||
|
{
|
||||||
|
useRarity = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRarity(int index, double r)
|
||||||
|
{
|
||||||
|
rarity[index] = 1D - Math.pow(0.5, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasBorder(int checks, double distance, double... dims)
|
||||||
|
{
|
||||||
|
int current = getIndex(dims);
|
||||||
|
double ajump = 360D / (double) checks;
|
||||||
|
|
||||||
|
if(dims.length == 2)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < checks; i++)
|
||||||
|
{
|
||||||
|
double dx = M.sin((float) Math.toRadians(ajump * i));
|
||||||
|
double dz = M.cos((float) Math.toRadians(ajump * i));
|
||||||
|
if(current != getIndex((dx * distance) + dims[0], (dz * distance) + dims[1]))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dims.length == 3)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < checks; i++)
|
||||||
|
{
|
||||||
|
double dx = M.sin((float) Math.toRadians(ajump * i));
|
||||||
|
double dz = M.cos((float) Math.toRadians(ajump * i));
|
||||||
|
double dy = Math.tan(Math.toRadians(ajump * i));
|
||||||
|
if(current != getIndex((dx * distance) + dims[0], (dz * distance) + dims[1], (dy * distance) + dims[2]))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasBorder3D(int checks, double distance, double... dims)
|
||||||
|
{
|
||||||
|
int current = getIndex(dims);
|
||||||
|
double ajump = 360D / (double) checks;
|
||||||
|
int hit = -1;
|
||||||
|
|
||||||
|
if(dims.length == 3)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < checks; i++)
|
||||||
|
{
|
||||||
|
double dx = M.sin((float) Math.toRadians(ajump * i));
|
||||||
|
double dz = M.cos((float) Math.toRadians(ajump * i));
|
||||||
|
double dy = Math.tan(Math.toRadians(ajump * i));
|
||||||
|
int d = getIndex((dx * distance) + dims[0], (dz * distance) + dims[1], (dy * distance) + dims[2]);
|
||||||
|
if(current != d)
|
||||||
|
{
|
||||||
|
if(hit >= 0 && hit != current && hit != d)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(hit < 0)
|
||||||
|
{
|
||||||
|
hit = d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns 0.0 to 1.0 where 0.0 is directly on the border of another region and
|
||||||
|
* 1.0 is perfectly in the center of a region
|
||||||
|
*
|
||||||
|
* @param x
|
||||||
|
* the x
|
||||||
|
* @param z
|
||||||
|
* the z
|
||||||
|
* @return the closest neighbor threshold.
|
||||||
|
*/
|
||||||
|
public double getClosestNeighbor(double... dim)
|
||||||
|
{
|
||||||
|
double closest = 0.5;
|
||||||
|
|
||||||
|
for(int i = 0; i < gen.length; i++)
|
||||||
|
{
|
||||||
|
double distance = Math.abs(gen[i].noise(dim) - 0.5);
|
||||||
|
|
||||||
|
if(distance < closest)
|
||||||
|
{
|
||||||
|
closest = distance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (closest * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIndex(double... dim)
|
||||||
|
{
|
||||||
|
int data = 0;
|
||||||
|
int adjusted = 0;
|
||||||
|
double[] noise = new double[gen.length];
|
||||||
|
|
||||||
|
for(int i = 0; i < gen.length; i++)
|
||||||
|
{
|
||||||
|
data |= (noise[i] = gen[i].noise(dim)) > 0.5 ? i == 0 ? 1 : 1 << i : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!useRarity)
|
||||||
|
{
|
||||||
|
return data % possibilities;
|
||||||
|
}
|
||||||
|
|
||||||
|
double r = rarity[data % possibilities];
|
||||||
|
|
||||||
|
for(int i = 0; i < gen.length; i++)
|
||||||
|
{
|
||||||
|
adjusted |= noise[i] > r ? i == 0 ? 1 : 1 << i : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return adjusted % possibilities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class EnumPolygonGenerator<T> extends PolygonGenerator
|
||||||
|
{
|
||||||
|
private T[] choices;
|
||||||
|
|
||||||
|
|
||||||
|
public EnumPolygonGenerator(RNG rng, double scale, int octaves, T[] choices, Function<CNG, CNG> factory)
|
||||||
|
{
|
||||||
|
super(rng, choices.length, scale / (double) choices.length, octaves, factory);
|
||||||
|
this.choices = choices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnumPolygonGenerator<T> useRarity()
|
||||||
|
{
|
||||||
|
super.useRarity();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public EnumPolygonGenerator(RNG rng, double scale, int octaves, KList<T> c, KMap<T, Double> choiceRarities, Function<CNG, CNG> factory)
|
||||||
|
{
|
||||||
|
super(rng, choiceRarities.size(), scale / (double) choiceRarities.size(), octaves, factory);
|
||||||
|
this.choices = (T[]) c.toArray();
|
||||||
|
int m = 0;
|
||||||
|
|
||||||
|
for(T i : c)
|
||||||
|
{
|
||||||
|
setRarity(m++, choiceRarities.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRarity(T t, double rarity)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < choices.length; i++)
|
||||||
|
{
|
||||||
|
if(choices[i].equals(t))
|
||||||
|
{
|
||||||
|
setRarity(i, rarity);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getChoice(double... dim)
|
||||||
|
{
|
||||||
|
return choices[getIndex(dim)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
101
src/main/java/ninja/bytecode/iris/util/PrecisionStopwatch.java
Normal file
101
src/main/java/ninja/bytecode/iris/util/PrecisionStopwatch.java
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class PrecisionStopwatch {
|
||||||
|
private long nanos;
|
||||||
|
private long startNano;
|
||||||
|
private long millis;
|
||||||
|
private long startMillis;
|
||||||
|
private double time;
|
||||||
|
private boolean profiling;
|
||||||
|
|
||||||
|
public static PrecisionStopwatch start() {
|
||||||
|
PrecisionStopwatch p = new PrecisionStopwatch();
|
||||||
|
p.begin();
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrecisionStopwatch() {
|
||||||
|
reset();
|
||||||
|
profiling = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void begin() {
|
||||||
|
profiling = true;
|
||||||
|
startNano = System.nanoTime();
|
||||||
|
startMillis = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void end() {
|
||||||
|
if (!profiling) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
profiling = false;
|
||||||
|
nanos = System.nanoTime() - startNano;
|
||||||
|
millis = System.currentTimeMillis() - startMillis;
|
||||||
|
time = (double) nanos / 1000000.0;
|
||||||
|
time = (double) millis - time > 1.01 ? millis : time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
nanos = -1;
|
||||||
|
millis = -1;
|
||||||
|
startNano = -1;
|
||||||
|
startMillis = -1;
|
||||||
|
time = -0;
|
||||||
|
profiling = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTicks() {
|
||||||
|
return getMilliseconds() / 50.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getSeconds() {
|
||||||
|
return getMilliseconds() / 1000.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMinutes() {
|
||||||
|
return getSeconds() / 60.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getHours() {
|
||||||
|
return getMinutes() / 60.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMilliseconds() {
|
||||||
|
nanos = System.nanoTime() - startNano;
|
||||||
|
millis = System.currentTimeMillis() - startMillis;
|
||||||
|
time = (double) nanos / 1000000.0;
|
||||||
|
time = (double) millis - time > 1.01 ? millis : time;
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getNanoseconds() {
|
||||||
|
return (long) (time * 1000000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getNanos() {
|
||||||
|
return nanos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getStartNano() {
|
||||||
|
return startNano;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getMillis() {
|
||||||
|
return millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getStartMillis() {
|
||||||
|
return startMillis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTime() {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isProfiling() {
|
||||||
|
return profiling;
|
||||||
|
}
|
||||||
|
}
|
31
src/main/java/ninja/bytecode/iris/util/Queue.java
Normal file
31
src/main/java/ninja/bytecode/iris/util/Queue.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public interface Queue<T>
|
||||||
|
{
|
||||||
|
public Queue<T> queue(T t);
|
||||||
|
|
||||||
|
public Queue<T> queue(KList<T> t);
|
||||||
|
|
||||||
|
public boolean hasNext(int amt);
|
||||||
|
|
||||||
|
public boolean hasNext();
|
||||||
|
|
||||||
|
public T next();
|
||||||
|
|
||||||
|
public KList<T> next(int amt);
|
||||||
|
|
||||||
|
public Queue<T> clear();
|
||||||
|
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
public static <T> Queue<T> create(KList<T> t)
|
||||||
|
{
|
||||||
|
return new ShurikenQueue<T>().queue(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> Queue<T> create(T... t)
|
||||||
|
{
|
||||||
|
return new ShurikenQueue<T>().queue(new KList<T>().add(t));
|
||||||
|
}
|
||||||
|
}
|
54
src/main/java/ninja/bytecode/iris/util/QueueExecutor.java
Normal file
54
src/main/java/ninja/bytecode/iris/util/QueueExecutor.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class QueueExecutor extends Looper
|
||||||
|
{
|
||||||
|
private Queue<Runnable> queue;
|
||||||
|
private boolean shutdown;
|
||||||
|
|
||||||
|
public QueueExecutor()
|
||||||
|
{
|
||||||
|
queue = new ShurikenQueue<Runnable>();
|
||||||
|
shutdown = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Queue<Runnable> queue()
|
||||||
|
{
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected long loop()
|
||||||
|
{
|
||||||
|
|
||||||
|
while(queue.hasNext())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
queue.next().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(Throwable e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shutdown && !queue.hasNext())
|
||||||
|
{
|
||||||
|
interrupt();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.max(500, (long) getRunTime() * 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getRunTime()
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown()
|
||||||
|
{
|
||||||
|
shutdown = true;
|
||||||
|
}
|
||||||
|
}
|
101
src/main/java/ninja/bytecode/iris/util/RollingSequence.java
Normal file
101
src/main/java/ninja/bytecode/iris/util/RollingSequence.java
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class RollingSequence extends Average
|
||||||
|
{
|
||||||
|
private double median;
|
||||||
|
private double max;
|
||||||
|
private double min;
|
||||||
|
private boolean dirtyMedian;
|
||||||
|
private int dirtyExtremes;
|
||||||
|
private boolean precision;
|
||||||
|
|
||||||
|
public RollingSequence(int size)
|
||||||
|
{
|
||||||
|
super(size);
|
||||||
|
median = 0;
|
||||||
|
min = 0;
|
||||||
|
max = 0;
|
||||||
|
setPrecision(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double addLast(int amt)
|
||||||
|
{
|
||||||
|
double f = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < Math.min(values.length, amt); i++)
|
||||||
|
{
|
||||||
|
f += values[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrecision(boolean p)
|
||||||
|
{
|
||||||
|
this.precision = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPrecision()
|
||||||
|
{
|
||||||
|
return precision;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMin()
|
||||||
|
{
|
||||||
|
if(dirtyExtremes > (isPrecision() ? 0 : values.length))
|
||||||
|
{
|
||||||
|
resetExtremes();
|
||||||
|
}
|
||||||
|
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMax()
|
||||||
|
{
|
||||||
|
if(dirtyExtremes > (isPrecision() ? 0 : values.length))
|
||||||
|
{
|
||||||
|
resetExtremes();
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMedian()
|
||||||
|
{
|
||||||
|
if(dirtyMedian)
|
||||||
|
{
|
||||||
|
recalculateMedian();
|
||||||
|
}
|
||||||
|
|
||||||
|
return median;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recalculateMedian()
|
||||||
|
{
|
||||||
|
median = new KList<Double>().forceAdd(values).sort().middleValue();
|
||||||
|
dirtyMedian = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetExtremes()
|
||||||
|
{
|
||||||
|
max = Integer.MIN_VALUE;
|
||||||
|
min = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
for(double i : values)
|
||||||
|
{
|
||||||
|
max = M.max(max, i);
|
||||||
|
min = M.min(min, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
dirtyExtremes = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(double i)
|
||||||
|
{
|
||||||
|
super.put(i);
|
||||||
|
dirtyMedian = true;
|
||||||
|
dirtyExtremes++;
|
||||||
|
max = M.max(max, i);
|
||||||
|
min = M.min(min, i);
|
||||||
|
}
|
||||||
|
}
|
@ -363,7 +363,6 @@ public class SNG extends PerlinNoise
|
|||||||
// (array access is a lot slower than member access)
|
// (array access is a lot slower than member access)
|
||||||
private static class Grad
|
private static class Grad
|
||||||
{
|
{
|
||||||
|
|
||||||
public double x;
|
public double x;
|
||||||
public double y;
|
public double y;
|
||||||
public double z;
|
public double z;
|
||||||
|
26
src/main/java/ninja/bytecode/iris/util/Shrinkwrap.java
Normal file
26
src/main/java/ninja/bytecode/iris/util/Shrinkwrap.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class Shrinkwrap<T>
|
||||||
|
{
|
||||||
|
private T t;
|
||||||
|
|
||||||
|
public Shrinkwrap(T t)
|
||||||
|
{
|
||||||
|
set(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Shrinkwrap()
|
||||||
|
{
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get()
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(T t)
|
||||||
|
{
|
||||||
|
this.t = t;
|
||||||
|
}
|
||||||
|
}
|
88
src/main/java/ninja/bytecode/iris/util/ShurikenQueue.java
Normal file
88
src/main/java/ninja/bytecode/iris/util/ShurikenQueue.java
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class ShurikenQueue<T> implements Queue<T>
|
||||||
|
{
|
||||||
|
private KList<T> queue;
|
||||||
|
private boolean randomPop;
|
||||||
|
private boolean reversePop;
|
||||||
|
|
||||||
|
public ShurikenQueue()
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShurikenQueue<T> responsiveMode()
|
||||||
|
{
|
||||||
|
reversePop = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShurikenQueue<T> randomMode()
|
||||||
|
{
|
||||||
|
randomPop = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ShurikenQueue<T> queue(T t)
|
||||||
|
{
|
||||||
|
queue.add(t);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ShurikenQueue<T> queue(KList<T> t)
|
||||||
|
{
|
||||||
|
queue.add(t);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext(int amt)
|
||||||
|
{
|
||||||
|
return queue.size() >= amt;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext()
|
||||||
|
{
|
||||||
|
return !queue.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T next()
|
||||||
|
{
|
||||||
|
return reversePop ? queue.popLast() : randomPop ? queue.popRandom() : queue.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KList<T> next(int amt)
|
||||||
|
{
|
||||||
|
KList<T> t = new KList<>();
|
||||||
|
|
||||||
|
for(int i = 0; i < amt; i++)
|
||||||
|
{
|
||||||
|
if(!hasNext())
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
t.add(next());
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ShurikenQueue<T> clear()
|
||||||
|
{
|
||||||
|
queue = new KList<T>();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size()
|
||||||
|
{
|
||||||
|
return queue.size();
|
||||||
|
}
|
||||||
|
}
|
51
src/main/java/ninja/bytecode/iris/util/WeightMap.java
Normal file
51
src/main/java/ninja/bytecode/iris/util/WeightMap.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
public class WeightMap<T> extends KMap<T, Double>
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 87558033900969389L;
|
||||||
|
private boolean modified = false;
|
||||||
|
private double lastWeight = 0;
|
||||||
|
|
||||||
|
public double getPercentChance(T t)
|
||||||
|
{
|
||||||
|
if(totalWeight() <= 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getWeight(t) / totalWeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear()
|
||||||
|
{
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WeightMap<T> setWeight(T t, double weight)
|
||||||
|
{
|
||||||
|
modified = true;
|
||||||
|
put(t, weight);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getWeight(T t)
|
||||||
|
{
|
||||||
|
return get(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double totalWeight()
|
||||||
|
{
|
||||||
|
if(!modified)
|
||||||
|
{
|
||||||
|
return lastWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
modified = false;
|
||||||
|
Shrinkwrap<Double> s = new Shrinkwrap<Double>(0D);
|
||||||
|
forEachKey(Integer.MAX_VALUE, (d) -> s.set(s.get() + 1));
|
||||||
|
lastWeight = s.get();
|
||||||
|
|
||||||
|
return lastWeight;
|
||||||
|
}
|
||||||
|
}
|
584
src/main/java/ninja/bytecode/iris/util/XML.java
Normal file
584
src/main/java/ninja/bytecode/iris/util/XML.java
Normal file
@ -0,0 +1,584 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2002 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This provides static methods to convert an XML text into a JSONObject, and to
|
||||||
|
* covert a JSONObject into an XML text.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class XML
|
||||||
|
{
|
||||||
|
|
||||||
|
/** The Character '&'. */
|
||||||
|
public static final Character AMP = '&';
|
||||||
|
|
||||||
|
/** The Character '''. */
|
||||||
|
public static final Character APOS = '\'';
|
||||||
|
|
||||||
|
/** The Character '!'. */
|
||||||
|
public static final Character BANG = '!';
|
||||||
|
|
||||||
|
/** The Character '='. */
|
||||||
|
public static final Character EQ = '=';
|
||||||
|
|
||||||
|
/** The Character '>'. */
|
||||||
|
public static final Character GT = '>';
|
||||||
|
|
||||||
|
/** The Character '<'. */
|
||||||
|
public static final Character LT = '<';
|
||||||
|
|
||||||
|
/** The Character '?'. */
|
||||||
|
public static final Character QUEST = '?';
|
||||||
|
|
||||||
|
/** The Character '"'. */
|
||||||
|
public static final Character QUOT = '"';
|
||||||
|
|
||||||
|
/** The Character '/'. */
|
||||||
|
public static final Character SLASH = '/';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace special characters with XML escapes:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* & <small>(ampersand)</small> is replaced by &amp;
|
||||||
|
* < <small>(less than)</small> is replaced by &lt;
|
||||||
|
* > <small>(greater than)</small> is replaced by &gt;
|
||||||
|
* " <small>(double quote)</small> is replaced by &quot;
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* The string to be escaped.
|
||||||
|
* @return The escaped string.
|
||||||
|
*/
|
||||||
|
public static String escape(String string)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder(string.length());
|
||||||
|
for(int i = 0, length = string.length(); i < length; i++)
|
||||||
|
{
|
||||||
|
char c = string.charAt(i);
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case '&':
|
||||||
|
sb.append("&");
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
sb.append("<");
|
||||||
|
break;
|
||||||
|
case '>':
|
||||||
|
sb.append(">");
|
||||||
|
break;
|
||||||
|
case '"':
|
||||||
|
sb.append(""");
|
||||||
|
break;
|
||||||
|
case '\'':
|
||||||
|
sb.append("'");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throw an exception if the string contains whitespace. Whitespace is not
|
||||||
|
* allowed in tagNames and attributes.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* A string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static void noSpace(String string) throws JSONException
|
||||||
|
{
|
||||||
|
int i, length = string.length();
|
||||||
|
if(length == 0)
|
||||||
|
{
|
||||||
|
throw new JSONException("Empty string.");
|
||||||
|
}
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
if(Character.isWhitespace(string.charAt(i)))
|
||||||
|
{
|
||||||
|
throw new JSONException("'" + string + "' contains a space character.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scan the content following the named tag, attaching it to the context.
|
||||||
|
*
|
||||||
|
* @param x
|
||||||
|
* The XMLTokener containing the source string.
|
||||||
|
* @param context
|
||||||
|
* The JSONObject that will include the new material.
|
||||||
|
* @param name
|
||||||
|
* The tag name.
|
||||||
|
* @return true if the close tag is processed.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int i;
|
||||||
|
JSONObject jsonobject = null;
|
||||||
|
String string;
|
||||||
|
String tagName;
|
||||||
|
Object token;
|
||||||
|
|
||||||
|
// Test for and skip past these forms:
|
||||||
|
// <!-- ... -->
|
||||||
|
// <! ... >
|
||||||
|
// <![ ... ]]>
|
||||||
|
// <? ... ?>
|
||||||
|
// Report errors for these forms:
|
||||||
|
// <>
|
||||||
|
// <=
|
||||||
|
// <<
|
||||||
|
|
||||||
|
token = x.nextToken();
|
||||||
|
|
||||||
|
// <!
|
||||||
|
|
||||||
|
if(token == BANG)
|
||||||
|
{
|
||||||
|
c = x.next();
|
||||||
|
if(c == '-')
|
||||||
|
{
|
||||||
|
if(x.next() == '-')
|
||||||
|
{
|
||||||
|
x.skipPast("-->");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
x.back();
|
||||||
|
} else if(c == '[')
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
if("CDATA".equals(token))
|
||||||
|
{
|
||||||
|
if(x.next() == '[')
|
||||||
|
{
|
||||||
|
string = x.nextCDATA();
|
||||||
|
if(string.length() > 0)
|
||||||
|
{
|
||||||
|
context.accumulate("content", string);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw x.syntaxError("Expected 'CDATA['");
|
||||||
|
}
|
||||||
|
i = 1;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
token = x.nextMeta();
|
||||||
|
if(token == null)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Missing '>' after '<!'.");
|
||||||
|
} else if(token == LT)
|
||||||
|
{
|
||||||
|
i += 1;
|
||||||
|
} else if(token == GT)
|
||||||
|
{
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
} while(i > 0);
|
||||||
|
return false;
|
||||||
|
} else if(token == QUEST)
|
||||||
|
{
|
||||||
|
|
||||||
|
// <?
|
||||||
|
|
||||||
|
x.skipPast("?>");
|
||||||
|
return false;
|
||||||
|
} else if(token == SLASH)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Close tag </
|
||||||
|
|
||||||
|
token = x.nextToken();
|
||||||
|
if(name == null)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Mismatched close tag " + token);
|
||||||
|
}
|
||||||
|
if(!token.equals(name))
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Mismatched " + name + " and " + token);
|
||||||
|
}
|
||||||
|
if(x.nextToken() != GT)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped close tag");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} else if(token instanceof Character)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
|
||||||
|
// Open tag <
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
tagName = (String) token;
|
||||||
|
token = null;
|
||||||
|
jsonobject = new JSONObject();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
if(token == null)
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
// attribute = value
|
||||||
|
|
||||||
|
if(token instanceof String)
|
||||||
|
{
|
||||||
|
string = (String) token;
|
||||||
|
token = x.nextToken();
|
||||||
|
if(token == EQ)
|
||||||
|
{
|
||||||
|
token = x.nextToken();
|
||||||
|
if(!(token instanceof String))
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Missing value");
|
||||||
|
}
|
||||||
|
jsonobject.accumulate(string, XML.stringToValue((String) token));
|
||||||
|
token = null;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
jsonobject.accumulate(string, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty tag <.../>
|
||||||
|
|
||||||
|
} else if(token == SLASH)
|
||||||
|
{
|
||||||
|
if(x.nextToken() != GT)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
}
|
||||||
|
if(jsonobject.length() > 0)
|
||||||
|
{
|
||||||
|
context.accumulate(tagName, jsonobject);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
context.accumulate(tagName, "");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Content, between <...> and </...>
|
||||||
|
|
||||||
|
} else if(token == GT)
|
||||||
|
{
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
token = x.nextContent();
|
||||||
|
if(token == null)
|
||||||
|
{
|
||||||
|
if(tagName != null)
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Unclosed tag " + tagName);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} else if(token instanceof String)
|
||||||
|
{
|
||||||
|
string = (String) token;
|
||||||
|
if(string.length() > 0)
|
||||||
|
{
|
||||||
|
jsonobject.accumulate("content", XML.stringToValue(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nested element
|
||||||
|
|
||||||
|
} else if(token == LT)
|
||||||
|
{
|
||||||
|
if(parse(x, jsonobject, tagName))
|
||||||
|
{
|
||||||
|
if(jsonobject.length() == 0)
|
||||||
|
{
|
||||||
|
context.accumulate(tagName, "");
|
||||||
|
} else if(jsonobject.length() == 1 && jsonobject.opt("content") != null)
|
||||||
|
{
|
||||||
|
context.accumulate(tagName, jsonobject.opt("content"));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
context.accumulate(tagName, jsonobject);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw x.syntaxError("Misshaped tag");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to convert a string into a number, boolean, or null. If the string
|
||||||
|
* can't be converted, return the string. This is much less ambitious than
|
||||||
|
* JSONObject.stringToValue, especially because it does not attempt to
|
||||||
|
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
|
||||||
|
* points.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* A String.
|
||||||
|
* @return A simple JSON value.
|
||||||
|
*/
|
||||||
|
public static Object stringToValue(String string)
|
||||||
|
{
|
||||||
|
if("true".equalsIgnoreCase(string))
|
||||||
|
{
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
if("false".equalsIgnoreCase(string))
|
||||||
|
{
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
if("null".equalsIgnoreCase(string))
|
||||||
|
{
|
||||||
|
return JSONObject.NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it might be a number, try converting it, first as a Long, and then
|
||||||
|
// as a
|
||||||
|
// Double. If that doesn't work, return the string.
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
char initial = string.charAt(0);
|
||||||
|
if(initial == '-' || (initial >= '0' && initial <= '9'))
|
||||||
|
{
|
||||||
|
Long value = new Long(string);
|
||||||
|
if(value.toString().equals(string))
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(Exception ignore)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Double value = new Double(string);
|
||||||
|
if(value.toString().equals(string))
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
} catch(Exception ignoreAlso)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||||
|
* JSONObject. Some information may be lost in this transformation because
|
||||||
|
* JSON is a data format and XML is a document format. XML uses elements,
|
||||||
|
* attributes, and content text, while JSON uses unordered collections of
|
||||||
|
* name/value pairs and arrays of values. JSON does not does not like to
|
||||||
|
* distinguish between elements and attributes. Sequences of similar
|
||||||
|
* elements are represented as JSONArrays. Content text may be placed in a
|
||||||
|
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||||
|
* are ignored.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* The source string.
|
||||||
|
* @return A JSONObject containing the structured data from the XML string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static JSONObject toJSONObject(String string) throws JSONException
|
||||||
|
{
|
||||||
|
JSONObject jo = new JSONObject();
|
||||||
|
XMLTokener x = new XMLTokener(string);
|
||||||
|
while(x.more() && x.skipPast("<"))
|
||||||
|
{
|
||||||
|
parse(x, jo, null);
|
||||||
|
}
|
||||||
|
return jo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||||
|
*
|
||||||
|
* @param object
|
||||||
|
* A JSONObject.
|
||||||
|
* @return A string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static String toString(Object object) throws JSONException
|
||||||
|
{
|
||||||
|
return toString(object, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||||
|
*
|
||||||
|
* @param object
|
||||||
|
* A JSONObject.
|
||||||
|
* @param tagName
|
||||||
|
* The optional name of the enclosing tag.
|
||||||
|
* @return A string.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public static String toString(Object object, String tagName) throws JSONException
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int i;
|
||||||
|
JSONArray ja;
|
||||||
|
JSONObject jo;
|
||||||
|
String key;
|
||||||
|
Iterator<String> keys;
|
||||||
|
int length;
|
||||||
|
String string;
|
||||||
|
Object value;
|
||||||
|
if(object instanceof JSONObject)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Emit <tagName>
|
||||||
|
|
||||||
|
if(tagName != null)
|
||||||
|
{
|
||||||
|
sb.append('<');
|
||||||
|
sb.append(tagName);
|
||||||
|
sb.append('>');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop thru the keys.
|
||||||
|
|
||||||
|
jo = (JSONObject) object;
|
||||||
|
keys = jo.keys();
|
||||||
|
while(keys.hasNext())
|
||||||
|
{
|
||||||
|
key = keys.next();
|
||||||
|
value = jo.opt(key);
|
||||||
|
if(value == null)
|
||||||
|
{
|
||||||
|
value = "";
|
||||||
|
}
|
||||||
|
string = value instanceof String ? (String) value : null;
|
||||||
|
|
||||||
|
// Emit content in body
|
||||||
|
|
||||||
|
if("content".equals(key))
|
||||||
|
{
|
||||||
|
if(value instanceof JSONArray)
|
||||||
|
{
|
||||||
|
ja = (JSONArray) value;
|
||||||
|
length = ja.length();
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
if(i > 0)
|
||||||
|
{
|
||||||
|
sb.append('\n');
|
||||||
|
}
|
||||||
|
sb.append(escape(ja.get(i).toString()));
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(escape(value.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit an array of similar keys
|
||||||
|
|
||||||
|
} else if(value instanceof JSONArray)
|
||||||
|
{
|
||||||
|
ja = (JSONArray) value;
|
||||||
|
length = ja.length();
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
value = ja.get(i);
|
||||||
|
if(value instanceof JSONArray)
|
||||||
|
{
|
||||||
|
sb.append('<');
|
||||||
|
sb.append(key);
|
||||||
|
sb.append('>');
|
||||||
|
sb.append(toString(value));
|
||||||
|
sb.append("</");
|
||||||
|
sb.append(key);
|
||||||
|
sb.append('>');
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(toString(value, key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if("".equals(value))
|
||||||
|
{
|
||||||
|
sb.append('<');
|
||||||
|
sb.append(key);
|
||||||
|
sb.append("/>");
|
||||||
|
|
||||||
|
// Emit a new tag <k>
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(toString(value, key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(tagName != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Emit the </tagname> close tag
|
||||||
|
|
||||||
|
sb.append("</");
|
||||||
|
sb.append(tagName);
|
||||||
|
sb.append('>');
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
|
||||||
|
// XML does not have good support for arrays. If an array appears in
|
||||||
|
// a place
|
||||||
|
// where XML is lacking, synthesize an <array> element.
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if(object.getClass().isArray())
|
||||||
|
{
|
||||||
|
object = new JSONArray(object);
|
||||||
|
}
|
||||||
|
if(object instanceof JSONArray)
|
||||||
|
{
|
||||||
|
ja = (JSONArray) object;
|
||||||
|
length = ja.length();
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
string = (object == null) ? "null" : escape(object.toString());
|
||||||
|
return (tagName == null) ? "\"" + string + "\"" : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + ">" + string + "</" + tagName + ">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
425
src/main/java/ninja/bytecode/iris/util/XMLTokener.java
Normal file
425
src/main/java/ninja/bytecode/iris/util/XMLTokener.java
Normal file
@ -0,0 +1,425 @@
|
|||||||
|
package ninja.bytecode.iris.util;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2002 JSON.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
The Software shall be used for Good, not Evil.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The XMLTokener extends the JSONTokener to provide additional methods for the
|
||||||
|
* parsing of XML texts.
|
||||||
|
*
|
||||||
|
* @author JSON.org
|
||||||
|
* @version 2014-05-03
|
||||||
|
*/
|
||||||
|
public class XMLTokener extends JSONTokener
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The table of entity values. It initially contains Character values for
|
||||||
|
* amp, apos, gt, lt, quot.
|
||||||
|
*/
|
||||||
|
public static final java.util.HashMap<String, Character> entity;
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
entity = new java.util.HashMap<String, Character>(8);
|
||||||
|
entity.put("amp", XML.AMP);
|
||||||
|
entity.put("apos", XML.APOS);
|
||||||
|
entity.put("gt", XML.GT);
|
||||||
|
entity.put("lt", XML.LT);
|
||||||
|
entity.put("quot", XML.QUOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an XMLTokener from a string.
|
||||||
|
*
|
||||||
|
* @param s
|
||||||
|
* A source string.
|
||||||
|
*/
|
||||||
|
public XMLTokener(String s)
|
||||||
|
{
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the text in the CDATA block.
|
||||||
|
*
|
||||||
|
* @return The string up to the <code>]]></code>.
|
||||||
|
* @throws JSONException
|
||||||
|
* If the <code>]]></code> is not found.
|
||||||
|
*/
|
||||||
|
public String nextCDATA() throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int i;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
if(end())
|
||||||
|
{
|
||||||
|
throw syntaxError("Unclosed CDATA");
|
||||||
|
}
|
||||||
|
sb.append(c);
|
||||||
|
i = sb.length() - 3;
|
||||||
|
if(i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>')
|
||||||
|
{
|
||||||
|
sb.setLength(i);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next XML outer token, trimming whitespace. There are two kinds of
|
||||||
|
* tokens: the '<' character which begins a markup tag, and the content text
|
||||||
|
* between markup tags.
|
||||||
|
*
|
||||||
|
* @return A string, or a '<' Character, or null if there is no more source
|
||||||
|
* text.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public Object nextContent() throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
StringBuilder sb;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
} while(Character.isWhitespace(c));
|
||||||
|
if(c == 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if(c == '<')
|
||||||
|
{
|
||||||
|
return XML.LT;
|
||||||
|
}
|
||||||
|
sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
if(c == '<' || c == 0)
|
||||||
|
{
|
||||||
|
back();
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
if(c == '&')
|
||||||
|
{
|
||||||
|
sb.append(nextEntity(c));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
c = next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the next entity. These entities are translated to Characters:
|
||||||
|
* <code>& ' > < "</code>.
|
||||||
|
*
|
||||||
|
* @param ampersand
|
||||||
|
* An ampersand character.
|
||||||
|
* @return A Character or an entity String if the entity is not recognized.
|
||||||
|
* @throws JSONException
|
||||||
|
* If missing ';' in XML entity.
|
||||||
|
*/
|
||||||
|
public Object nextEntity(char ampersand) throws JSONException
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
char c = next();
|
||||||
|
if(Character.isLetterOrDigit(c) || c == '#')
|
||||||
|
{
|
||||||
|
sb.append(Character.toLowerCase(c));
|
||||||
|
} else if(c == ';')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw syntaxError("Missing ';' in XML entity: &" + sb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String string = sb.toString();
|
||||||
|
Object object = entity.get(string);
|
||||||
|
return object != null ? object : ampersand + string + ";";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the next XML meta token. This is used for skipping over
|
||||||
|
* <!...> and <?...?> structures.
|
||||||
|
*
|
||||||
|
* @return Syntax characters (<code>< > / = ! ?</code>) are returned as
|
||||||
|
* Character, and strings and names are returned as Boolean. We
|
||||||
|
* don't care what the values actually are.
|
||||||
|
* @throws JSONException
|
||||||
|
* If a string is not properly closed or if the XML is badly
|
||||||
|
* structured.
|
||||||
|
*/
|
||||||
|
public Object nextMeta() throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
char q;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
} while(Character.isWhitespace(c));
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
throw syntaxError("Misshaped meta tag");
|
||||||
|
case '<':
|
||||||
|
return XML.LT;
|
||||||
|
case '>':
|
||||||
|
return XML.GT;
|
||||||
|
case '/':
|
||||||
|
return XML.SLASH;
|
||||||
|
case '=':
|
||||||
|
return XML.EQ;
|
||||||
|
case '!':
|
||||||
|
return XML.BANG;
|
||||||
|
case '?':
|
||||||
|
return XML.QUEST;
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
q = c;
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
if(c == 0)
|
||||||
|
{
|
||||||
|
throw syntaxError("Unterminated string");
|
||||||
|
}
|
||||||
|
if(c == q)
|
||||||
|
{
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
if(Character.isWhitespace(c))
|
||||||
|
{
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
case '<':
|
||||||
|
case '>':
|
||||||
|
case '/':
|
||||||
|
case '=':
|
||||||
|
case '!':
|
||||||
|
case '?':
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
back();
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next XML Token. These tokens are found inside of angle brackets.
|
||||||
|
* It may be one of these characters: <code>/ > = ! ?</code> or it may be a
|
||||||
|
* string wrapped in single quotes or double quotes, or it may be a name.
|
||||||
|
*
|
||||||
|
* @return a String or a Character.
|
||||||
|
* @throws JSONException
|
||||||
|
* If the XML is not well formed.
|
||||||
|
*/
|
||||||
|
public Object nextToken() throws JSONException
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
char q;
|
||||||
|
StringBuilder sb;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
} while(Character.isWhitespace(c));
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
throw syntaxError("Misshaped element");
|
||||||
|
case '<':
|
||||||
|
throw syntaxError("Misplaced '<'");
|
||||||
|
case '>':
|
||||||
|
return XML.GT;
|
||||||
|
case '/':
|
||||||
|
return XML.SLASH;
|
||||||
|
case '=':
|
||||||
|
return XML.EQ;
|
||||||
|
case '!':
|
||||||
|
return XML.BANG;
|
||||||
|
case '?':
|
||||||
|
return XML.QUEST;
|
||||||
|
|
||||||
|
// Quoted string
|
||||||
|
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
q = c;
|
||||||
|
sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
if(c == 0)
|
||||||
|
{
|
||||||
|
throw syntaxError("Unterminated string");
|
||||||
|
}
|
||||||
|
if(c == q)
|
||||||
|
{
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
if(c == '&')
|
||||||
|
{
|
||||||
|
sb.append(nextEntity(c));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
|
||||||
|
// Name
|
||||||
|
|
||||||
|
sb = new StringBuilder();
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
sb.append(c);
|
||||||
|
c = next();
|
||||||
|
if(Character.isWhitespace(c))
|
||||||
|
{
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
return sb.toString();
|
||||||
|
case '>':
|
||||||
|
case '/':
|
||||||
|
case '=':
|
||||||
|
case '!':
|
||||||
|
case '?':
|
||||||
|
case '[':
|
||||||
|
case ']':
|
||||||
|
back();
|
||||||
|
return sb.toString();
|
||||||
|
case '<':
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
throw syntaxError("Bad character in a name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skip characters until past the requested string. If it is not found, we
|
||||||
|
* are left at the end of the source with a result of false.
|
||||||
|
*
|
||||||
|
* @param to
|
||||||
|
* A string to skip past.
|
||||||
|
* @throws JSONException
|
||||||
|
*/
|
||||||
|
public boolean skipPast(String to) throws JSONException
|
||||||
|
{
|
||||||
|
boolean b;
|
||||||
|
char c;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int offset = 0;
|
||||||
|
int length = to.length();
|
||||||
|
char[] circle = new char[length];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* First fill the circle buffer with as many characters as are in the to
|
||||||
|
* string. If we reach an early end, bail.
|
||||||
|
*/
|
||||||
|
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
c = next();
|
||||||
|
if(c == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
circle[i] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We will loop, possibly for all of the remaining characters. */
|
||||||
|
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
j = offset;
|
||||||
|
b = true;
|
||||||
|
|
||||||
|
/* Compare the circle buffer with the to string. */
|
||||||
|
|
||||||
|
for(i = 0; i < length; i += 1)
|
||||||
|
{
|
||||||
|
if(circle[j] != to.charAt(i))
|
||||||
|
{
|
||||||
|
b = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
j += 1;
|
||||||
|
if(j >= length)
|
||||||
|
{
|
||||||
|
j -= length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we exit the loop with b intact, then victory is ours. */
|
||||||
|
|
||||||
|
if(b)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the next character. If there isn't one, then defeat is ours.
|
||||||
|
*/
|
||||||
|
|
||||||
|
c = next();
|
||||||
|
if(c == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Shove the character in the circle buffer and advance the circle
|
||||||
|
* offset. The offset is mod n.
|
||||||
|
*/
|
||||||
|
circle[offset] = c;
|
||||||
|
offset += 1;
|
||||||
|
if(offset >= length)
|
||||||
|
{
|
||||||
|
offset -= length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user