ValueMut.java
package com.surrealdb;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
public class ValueMut extends Native {
private ValueMut(long ptr) {
super(ptr);
}
private static native long newNone();
private static native long newNull();
private static native long newString(String s);
private static native long newBoolean(boolean b);
private static native long newDouble(double d);
private static native long newLong(long l);
private static native long newDecimal(String s);
private static native long newDuration(long l);
private static native long newDatetime(long seconds, long nanos);
private static native long newUuid(String s);
private static native long newId(long ptr);
private static native long newRecordId(long ptr);
private static native long newGeometry(long ptr);
private static native long newArray(long ptr);
private static native long newArrayOf(long[] ptrs);
private static native long newObject(long ptr);
private static native long newObjectOf(long[] ptrs);
private static native long newFile(String bucket, String key);
private static native long newTable(String name);
public static ValueMut createNone() {
return new ValueMut(newNone());
}
public static ValueMut createNull() {
return new ValueMut(newNull());
}
public static ValueMut createString(String s) {
return new ValueMut(newString(s));
}
public static ValueMut createBoolean(boolean b) {
return new ValueMut(newBoolean(b));
}
public static ValueMut createDouble(double d) {
return new ValueMut(newDouble(d));
}
public static ValueMut createLong(long l) {
return new ValueMut(newLong(l));
}
public static ValueMut createBigDecimal(BigDecimal d) {
return new ValueMut(newDecimal(d.toString()));
}
public static ValueMut createDuration(Duration d) {
return new ValueMut(newDuration(d.toMillis()));
}
public static ValueMut createDatetime(Instant i) {
return new ValueMut(newDatetime(i.getEpochSecond(), i.getNano()));
}
public static ValueMut createDatetime(ZonedDateTime d) {
return createDatetime(d.toInstant());
}
public static ValueMut createDatetime(OffsetDateTime d) {
return createDatetime(d.toInstant());
}
/**
* Creates a SurrealDB datetime from a local date-time by interpreting it as
* UTC.
* <p>
* Prefer {@link Instant}, {@link OffsetDateTime}, or {@link ZonedDateTime} when
* the source value represents an absolute point in time or carries zone/offset
* information.
*/
public static ValueMut createDatetime(LocalDateTime d) {
return createDatetime(d.toInstant(ZoneOffset.UTC));
}
/**
* Creates a SurrealDB datetime from a {@link java.util.Date}.
* <p>
* {@code java.sql.Timestamp} keeps its nanosecond precision.
* {@code java.sql.Date} and {@code java.sql.Time} do not support
* {@link java.util.Date#toInstant()}; they are converted through their
* epoch-millisecond value instead.
*/
public static ValueMut createDatetime(java.util.Date d) {
try {
return createDatetime(d.toInstant());
} catch (UnsupportedOperationException e) {
// java.sql.Date and java.sql.Time refuse toInstant()
return createDatetime(Instant.ofEpochMilli(d.getTime()));
}
}
public static ValueMut createUuid(UUID uuid) {
return new ValueMut(newUuid(uuid.toString()));
}
public static ValueMut createId(Id id) {
return new ValueMut(newId(id.getPtr()));
}
public static ValueMut createRecordId(RecordId recordId) {
return new ValueMut(newRecordId(recordId.getPtr()));
}
public static ValueMut createGeometry(Geometry geometry) {
return new ValueMut(newGeometry(geometry.getPtr()));
}
public static ValueMut createArray(Array array) {
return new ValueMut(newArray(array.getPtr()));
}
public static ValueMut createArray(List<ValueMut> values) {
final long[] ptrs = new long[values.size()];
int idx = 0;
// Retrieve the PTR for each element
for (final ValueMut value : values) {
ptrs[idx++] = value.getPtr();
}
final ValueMut value = new ValueMut(newArrayOf(ptrs));
// The Values have been moved
values.forEach(Native::moved);
return value;
}
public static ValueMut createObject(Object object) {
return new ValueMut(newObject(object.getPtr()));
}
public static ValueMut createObject(List<EntryMut> entries) {
final long[] ptrs = new long[entries.size()];
int idx = 0;
// Retrieve the PTR for each element
for (final EntryMut entry : entries) {
ptrs[idx++] = entry.getPtr();
}
final ValueMut value = new ValueMut(newObjectOf(ptrs));
// The Entries have been moved
entries.forEach(Native::moved);
return value;
}
public static ValueMut createFile(String bucket, String key) {
return new ValueMut(newFile(bucket, key));
}
public static ValueMut createTable(String name) {
return new ValueMut(newTable(name));
}
@Override
final native String toString(long ptr);
@Override
final native int hashCode(long ptr);
@Override
final native boolean equals(long ptr1, long ptr2);
@Override
final native void deleteInstance(long ptr);
}