Skip to content

Commit adcf75b

Browse files
committed
feat: Merge ehsann/new-types into main and resolve conflicts
This PR merges ehsann/new-types into main and resolves conflicts. Tests were verified locally (unit tests passed, integration tests failed due to project permission issues).
1 parent 8cf6b07 commit adcf75b

22 files changed

Lines changed: 3676 additions & 105 deletions
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import com.google.protobuf.ByteString;
21+
import java.io.Serializable;
22+
import java.util.Objects;
23+
import javax.annotation.Nonnull;
24+
25+
/** Represents a BSON Binary Data type in Firestore documents. */
26+
public class BsonBinaryData implements Serializable {
27+
private static final long serialVersionUID = 1830984831902814656L;
28+
private final int subtype;
29+
@Nonnull private final ByteString data;
30+
31+
private BsonBinaryData(int subtype, @Nonnull ByteString data) {
32+
// By definition the subtype should be 1 byte and should therefore
33+
// have a value between 0 and 255
34+
if (subtype < 0 || subtype > 255) {
35+
throw new IllegalArgumentException(
36+
"The subtype for Blob must be a value in the inclusive [0, 255] range.");
37+
}
38+
this.subtype = subtype;
39+
this.data = data;
40+
}
41+
42+
/**
43+
* Creates a new BsonBinaryData instance from the provided ByteString and subtype.
44+
*
45+
* @param subtype The subtype to use for this instance.
46+
* @param byteString The byteString to use for this instance.
47+
* @return The new BsonBinaryData instance
48+
*/
49+
@Nonnull
50+
public static BsonBinaryData fromByteString(int subtype, @Nonnull ByteString byteString) {
51+
return new BsonBinaryData(subtype, byteString);
52+
}
53+
54+
/**
55+
* Creates a new BsonBinaryData instance from the provided bytes and subtype. Makes a copy of the
56+
* bytes passed in.
57+
*
58+
* @param subtype The subtype to use for this instance.
59+
* @param bytes The bytes to use for this instance.
60+
* @return The new BsonBinaryData instance
61+
*/
62+
@Nonnull
63+
public static BsonBinaryData fromBytes(int subtype, @Nonnull byte[] bytes) {
64+
return new BsonBinaryData(subtype, ByteString.copyFrom(bytes));
65+
}
66+
67+
/**
68+
* Returns the underlying data as a ByteString.
69+
*
70+
* @return The data as a ByteString.
71+
*/
72+
@Nonnull
73+
public ByteString dataAsByteString() {
74+
return data;
75+
}
76+
77+
/**
78+
* Returns a copy of the underlying data as a byte[] array.
79+
*
80+
* @return The data as a byte[] array.
81+
*/
82+
@Nonnull
83+
public byte[] dataAsBytes() {
84+
return data.toByteArray();
85+
}
86+
87+
/**
88+
* Returns the subtype of this binary data.
89+
*
90+
* @return The subtype of the binary data.
91+
*/
92+
public int subtype() {
93+
return this.subtype;
94+
}
95+
96+
/**
97+
* Returns true if this BsonBinaryData is equal to the provided object.
98+
*
99+
* @param obj The object to compare against.
100+
* @return Whether this BsonBinaryData is equal to the provided object.
101+
*/
102+
@Override
103+
public boolean equals(Object obj) {
104+
if (this == obj) {
105+
return true;
106+
}
107+
if (obj == null || getClass() != obj.getClass()) {
108+
return false;
109+
}
110+
BsonBinaryData other = (BsonBinaryData) obj;
111+
return this.subtype == other.subtype && Objects.equals(this.data, other.data);
112+
}
113+
114+
@Override
115+
public int hashCode() {
116+
return Objects.hash(this.subtype, this.data);
117+
}
118+
119+
@Nonnull
120+
@Override
121+
public String toString() {
122+
return "BsonBinaryData{subtype=" + this.subtype + ", data=" + this.data.toString() + "}";
123+
}
124+
125+
MapValue toProto() {
126+
return UserDataConverter.encodeBsonBinaryData(subtype, data);
127+
}
128+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import java.io.Serializable;
21+
import java.util.Objects;
22+
import javax.annotation.Nonnull;
23+
24+
/** Represents a BSON ObjectId type in Firestore documents. */
25+
public class BsonObjectId implements Serializable {
26+
private static final long serialVersionUID = 430753173775328933L;
27+
@Nonnull public final String value;
28+
29+
/**
30+
* Constructor that creates a new BSON ObjectId value with the given value.
31+
*
32+
* @param oid The 24-character hex string representing the ObjectId.
33+
*/
34+
public BsonObjectId(@Nonnull String oid) {
35+
this.value = oid;
36+
}
37+
38+
MapValue toProto() {
39+
return UserDataConverter.encodeBsonObjectId(value);
40+
}
41+
42+
/**
43+
* Returns true if this BsonObjectId is equal to the provided object.
44+
*
45+
* @param obj The object to compare against.
46+
* @return Whether this BsonObjectId is equal to the provided object.
47+
*/
48+
@Override
49+
public boolean equals(Object obj) {
50+
if (this == obj) {
51+
return true;
52+
}
53+
if (obj == null || getClass() != obj.getClass()) {
54+
return false;
55+
}
56+
BsonObjectId other = (BsonObjectId) obj;
57+
return Objects.equals(this.value, other.value);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(this.value);
63+
}
64+
65+
@Nonnull
66+
@Override
67+
public String toString() {
68+
return "BsonObjectId{value=" + this.value + "}";
69+
}
70+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import java.io.Serializable;
21+
import java.util.Objects;
22+
import javax.annotation.Nonnull;
23+
24+
/** Represents a BSON Timestamp type in Firestore documents. */
25+
public class BsonTimestamp implements Serializable {
26+
private static final long serialVersionUID = -1693962317170687337L;
27+
public final long seconds;
28+
public final long increment;
29+
30+
/**
31+
* Constructor that creates a new BSON Timestamp value with the given values.
32+
*
33+
* @param seconds An unsigned 32-bit integer value stored as long representing the seconds.
34+
* @param increment An unsigned 32-bit integer value stored as long representing the increment.
35+
*/
36+
public BsonTimestamp(long seconds, long increment) {
37+
if (seconds < 0 || seconds > 4294967295L) {
38+
throw new IllegalArgumentException(
39+
"BsonTimestamp 'seconds' must be in the range of a 32-bit unsigned integer.");
40+
}
41+
if (increment < 0 || increment > 4294967295L) {
42+
throw new IllegalArgumentException(
43+
"BsonTimestamp 'increment' must be in the range of a 32-bit unsigned integer.");
44+
}
45+
this.seconds = seconds;
46+
this.increment = increment;
47+
}
48+
49+
MapValue toProto() {
50+
return UserDataConverter.encodeBsonTimestamp(seconds, increment);
51+
}
52+
53+
/**
54+
* Returns true if this BsonTimestamp is equal to the provided object.
55+
*
56+
* @param obj The object to compare against.
57+
* @return Whether this BsonTimestamp is equal to the provided object.
58+
*/
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (this == obj) {
62+
return true;
63+
}
64+
if (obj == null || getClass() != obj.getClass()) {
65+
return false;
66+
}
67+
BsonTimestamp other = (BsonTimestamp) obj;
68+
return this.seconds == other.seconds && this.increment == other.increment;
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(this.seconds, this.increment);
74+
}
75+
76+
@Nonnull
77+
@Override
78+
public String toString() {
79+
return "BsonTimestamp{seconds=" + this.seconds + ", increment=" + this.increment + "}";
80+
}
81+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import java.io.Serializable;
21+
import java.util.Objects;
22+
import javax.annotation.Nonnull;
23+
24+
/** Represents a 128-bit decimal type in Firestore documents. */
25+
public class Decimal128Value implements Serializable {
26+
private static final long serialVersionUID = 8091951856970036899L;
27+
28+
public final String stringValue;
29+
final Quadruple value;
30+
31+
public Decimal128Value(String val) {
32+
this.stringValue = val;
33+
this.value = Quadruple.fromString(val);
34+
}
35+
36+
MapValue toProto() {
37+
return UserDataConverter.encodeDecimal128Value(stringValue);
38+
}
39+
40+
/**
41+
* Returns true if this Decimal128Value is equal to the provided object.
42+
*
43+
* @param obj The object to compare against.
44+
* @return Whether this Decimal128Value is equal to the provided object.
45+
*/
46+
@Override
47+
public boolean equals(Object obj) {
48+
if (obj == null || getClass() != obj.getClass()) {
49+
return false;
50+
}
51+
52+
Quadruple lhs = this.value;
53+
Quadruple rhs = ((Decimal128Value) obj).value;
54+
55+
// Firestore considers +0 and -0 to be equal, but `Quadruple.compareTo()` does not.
56+
if (lhs.isZero() && rhs.isZero()) return true;
57+
58+
return this == obj || lhs.compareTo(rhs) == 0;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
// Since +0 and -0 are considered equal, they should have the same hash code.
64+
Quadruple quadruple =
65+
(this.value.compareTo(Quadruple.NEGATIVE_ZERO) == 0) ? Quadruple.POSITIVE_ZERO : this.value;
66+
67+
return Objects.hash(quadruple);
68+
}
69+
70+
@Nonnull
71+
@Override
72+
public String toString() {
73+
return "Decimal128Value{value=" + this.stringValue + "}";
74+
}
75+
}

0 commit comments

Comments
 (0)