com.google.cloud.firestore.FieldValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of google-cloud-firestore Show documentation
Show all versions of google-cloud-firestore Show documentation
Java idiomatic client for Google Cloud Firestore.
/*
* 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 com.google.cloud.firestore;
import com.google.cloud.firestore.encoding.CustomClassMapper;
import com.google.common.base.Preconditions;
import com.google.firestore.v1.ArrayValue;
import com.google.firestore.v1.DocumentTransform.FieldTransform;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
/** Sentinel values that can be used when writing document fields with set() or update(). */
public abstract class FieldValue {
private static final FieldValue SERVER_TIMESTAMP_SENTINEL =
new FieldValue() {
@Override
boolean includeInDocumentMask() {
return false;
}
@Override
boolean includeInDocumentTransform() {
return true;
}
@Override
String getMethodName() {
return "FieldValue.serverTimestamp()";
}
@Override
FieldTransform toProto(FieldPath path) {
FieldTransform.Builder fieldTransform = FieldTransform.newBuilder();
fieldTransform.setFieldPath(path.getEncodedPath());
fieldTransform.setSetToServerValue(FieldTransform.ServerValue.REQUEST_TIME);
return fieldTransform.build();
}
};
static final FieldValue DELETE_SENTINEL =
new FieldValue() {
@Override
boolean includeInDocumentMask() {
return true;
}
@Override
boolean includeInDocumentTransform() {
return false;
}
@Override
String getMethodName() {
return "FieldValue.delete()";
}
@Override
FieldTransform toProto(FieldPath path) {
throw new IllegalStateException(
"FieldValue.delete() should not be included in a FieldTransform");
}
};
static class NumericIncrementFieldValue extends FieldValue {
final Number operand;
NumericIncrementFieldValue(Number operand) {
this.operand = operand;
}
@Override
boolean includeInDocumentMask() {
return false;
}
@Override
boolean includeInDocumentTransform() {
return true;
}
@Override
String getMethodName() {
return "FieldValue.increment()";
}
@Override
FieldTransform toProto(FieldPath path) {
FieldTransform.Builder fieldTransform = FieldTransform.newBuilder();
fieldTransform.setFieldPath(path.getEncodedPath());
fieldTransform.setIncrement(
UserDataConverter.encodeValue(path, operand, UserDataConverter.ARGUMENT));
return fieldTransform.build();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
NumericIncrementFieldValue that = (NumericIncrementFieldValue) o;
return Objects.equals(operand, that.operand);
}
@Override
public int hashCode() {
return Objects.hash(operand);
}
}
static class ArrayUnionFieldValue extends FieldValue {
final List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy