Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.topicus.jdbc.shaded.com.google.cloud.spanner;
import nl.topicus.jdbc.shaded.com.google.cloud.ByteArray;
import nl.topicus.jdbc.shaded.com.google.cloud.Date;
import nl.topicus.jdbc.shaded.com.google.cloud.Timestamp;
import nl.topicus.jdbc.shaded.com.google.common.base.Joiner;
import nl.topicus.jdbc.shaded.com.google.common.base.Preconditions;
import nl.topicus.jdbc.shaded.com.google.common.collect.Iterables;
import nl.topicus.jdbc.shaded.com.google.common.collect.Lists;
import nl.topicus.jdbc.shaded.com.google.protobuf.ListValue;
import nl.topicus.jdbc.shaded.com.google.protobuf.NullValue;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import nl.topicus.jdbc.shaded.javax.annotation.Nullable;
import nl.topicus.jdbc.shaded.javax.annotation.concurrent.Immutable;
/**
* Represents a value to be consumed by the Cloud Spanner API. A value can be {@code NULL} or
* non-{@code NULL}; regardless, values always have an associated type.
*
*
The {@code Value} API is optimized for construction, since this is the majority use-case when
* using this class with the Cloud Spanner libraries. The factory method signatures and internal
* representations are design to minimize memory usage and object creation while still maintaining
* the immutability contract of this class. In particular, arrays of primitive types can be
* constructed without requiring boxing into collections of wrapper types. The getters in this class
* are intended primarily for test purposes, and so do not share the same performance
* characteristics; in particular, getters for array types may be expensive.
*
*
{@code Value} instances are immutable.
*/
@Immutable
public abstract class Value implements Serializable {
private static final int MAX_DEBUG_STRING_LENGTH = 32;
private static final String ELLIPSIS = "...";
private static final String NULL_STRING = "NULL";
private static final char LIST_SEPERATOR = ',';
private static final char LIST_OPEN = '[';
private static final char LIST_CLOSE = ']';
private static final long serialVersionUID = -5289864325087675338L;
/**
* Returns a {@code BOOL} value.
*
* @param v the value, which may be null
*/
public static Value bool(@Nullable Boolean v) {
return new BoolImpl(v == null, v == null ? false : v);
}
/** Returns a {@code BOOL} value. */
public static Value bool(boolean v) {
return new BoolImpl(false, v);
}
/**
* Returns an {@code INT64} value.
*
* @param v the value, which may be null
*/
public static Value int64(@Nullable Long v) {
return new Int64Impl(v == null, v == null ? 0 : v);
}
/** Returns an {@code INT64} value. */
public static Value int64(long v) {
return new Int64Impl(false, v);
}
/**
* Returns a {@code FLOAT64} value.
*
* @param v the value, which may be null
*/
public static Value float64(@Nullable Double v) {
return new Float64Impl(v == null, v == null ? 0 : v);
}
/** Returns a {@code FLOAT64} value. */
public static Value float64(double v) {
return new Float64Impl(false, v);
}
/**
* Returns a {@code STRING} value.
*
* @param v the value, which may be null
*/
public static Value string(@Nullable String v) {
return new StringImpl(v == null, v);
}
/**
* Returns a {@code BYTES} value.
*
* @param v the value, which may be null
*/
public static Value bytes(@Nullable ByteArray v) {
return new BytesImpl(v == null, v);
}
/** Returns a {@code TIMESTAMP} value. */
public static Value timestamp(@Nullable Timestamp v) {
return new TimestampImpl(v == null, v);
}
/**
* Returns a {@code DATE} value. The range [1678-01-01, 2262-01-01) is the legal interval for
* cloud spanner dates. A write to a date column is rejected if the value is outside of that
* interval.
*/
public static Value date(@Nullable Date v) {
return new DateImpl(v == null, v);
}
// TODO(user): Implement struct()/structArray().
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value boolArray(@Nullable boolean[] v) {
return boolArray(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value boolArray(@Nullable boolean[] v, int pos, int length) {
return boolArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value boolArray(@Nullable Iterable v) {
// TODO(user): Consider memory optimizing boolArray() to use BitSet instead of boolean[].
return boolArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value int64Array(@Nullable long[] v) {
return int64Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value int64Array(@Nullable long[] v, int pos, int length) {
return int64ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value int64Array(@Nullable Iterable v) {
return int64ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value float64Array(@Nullable double[] v) {
return float64Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value float64Array(@Nullable double[] v, int pos, int length) {
return float64ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value float64Array(@Nullable Iterable v) {
return float64ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value stringArray(@Nullable Iterable v) {
return new StringArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value bytesArray(@Nullable Iterable v) {
return new BytesArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value timestampArray(@Nullable Iterable v) {
return new TimestampArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY} value. The range [1678-01-01, 2262-01-01) is the legal interval
* for cloud spanner dates. A write to a date column is rejected if the value is outside of that
* interval.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value dateArray(@Nullable Iterable v) {
return new DateArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
private Value() {}
/** Returns the type of this value. This will return a type even if {@code isNull()} is true. */
public abstract Type getType();
/** Returns {@code true} if this instance represents a {@code NULL} value. */
public abstract boolean isNull();
/**
* Returns the value of a {@code BOOL}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract boolean getBool();
/**
* Returns the value of a {@code INT64}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract long getInt64();
/**
* Returns the value of a {@code FLOAT64}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract double getFloat64();
/**
* Returns the value of a {@code STRING}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract String getString();
/**
* Returns the value of a {@code BYTES}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract ByteArray getBytes();
/**
* Returns the value of a {@code TIMESTAMP}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract Timestamp getTimestamp();
/**
* Returns the value of a {@code DATE}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract Date getDate();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself will
* never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getBoolArray();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getInt64Array();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getFloat64Array();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getStringArray();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getBytesArray();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getTimestampArray();
/**
* Returns the value of an {@code ARRAY}-typed instance. While the returned list itself will
* never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List getDateArray();
@Override
public String toString() {
StringBuilder b = new StringBuilder();
toString(b);
return b.toString();
}
// END OF PUBLIC API.
/**
* Returns an {@code ARRAY>} value.
*
*
This method is intentionally not in the public API for Value: {@code ARRAY>}
* values are not accepted by the backend.
*
* @param fieldTypes the types of the fields in the array elements. All non-null elements must
* conform to this type.
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
static Value structArray(Iterable fieldTypes, @Nullable Iterable v) {
Type elementType = Type.struct(fieldTypes);
if (v == null) {
return new StructArrayImpl(elementType, null);
}
List values = immutableCopyOf(v);
for (Struct value : values) {
if (value != null && !value.getType().equals(elementType)) {
throw new IllegalArgumentException(
"Members of v must have type " + elementType + " (found " + value.getType() + ")");
}
}
return new StructArrayImpl(elementType, values);
}
/**
* Returns the value of an {@code ARRAY>}-typed instance. While the returned list
* itself will never be {@code null}, elements of that list may be null.
*
*