Create more Vec implementations

This commit is contained in:
TheMode 2021-06-30 01:53:01 +02:00
parent c42b494a62
commit b93f1a57cd
2 changed files with 92 additions and 27 deletions

View File

@ -8,17 +8,22 @@ import java.util.function.DoubleUnaryOperator;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
public interface Vec { public interface Vec {
Vec ZERO = new VecImpl(0); Vec ZERO = vec(0);
Vec ONE = new VecImpl(1); Vec ONE = vec(1);
@Contract(pure = true) @Contract(pure = true)
static @NotNull Vec vec(double x, double y, double z) { static @NotNull Vec vec(double x, double y, double z) {
return new VecImpl(x, y, z); return new VecImpl.Vec3(x, y, z);
}
@Contract(pure = true)
static @NotNull Vec vec(double x, double z) {
return new VecImpl.Tuple(x, z);
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull Vec vec(double value) { static @NotNull Vec vec(double value) {
return new VecImpl(value); return new VecImpl.Single(value);
} }
@Contract(pure = true) @Contract(pure = true)

View File

@ -2,36 +2,96 @@ package net.minestom.server.utils.incubator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
class VecImpl implements Vec { class VecImpl {
private final double x, y, z;
public VecImpl(double x, double y, double z) { static class Vec3 implements Vec {
this.x = x; private final double x, y, z;
this.y = y;
this.z = z; Vec3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vec3(double value) {
this(value, value, value);
}
@Override
public @NotNull Vec with(double x, double y, double z) {
return new Vec3(x, y, z);
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return y;
}
@Override
public double z() {
return z;
}
} }
public VecImpl(double value) { static class Tuple implements Vec {
this(value, value, value); private final double x, z;
Tuple(double x, double z) {
this.x = x;
this.z = z;
}
@Override
public @NotNull Vec with(double x, double y, double z) {
return new Vec3(x, y, z);
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return 0;
}
@Override
public double z() {
return z;
}
} }
@Override static class Single implements Vec {
public @NotNull Vec with(double x, double y, double z) { private final double value;
return new VecImpl(x, y, z);
}
@Override Single(double value) {
public double x() { this.value = value;
return x; }
}
@Override @Override
public double y() { public @NotNull Vec with(double x, double y, double z) {
return y; return new Vec3(x, y, z);
} }
@Override @Override
public double z() { public double x() {
return z; return value;
}
@Override
public double y() {
return value;
}
@Override
public double z() {
return value;
}
} }
} }