Skip to content

Commit f9b57dd

Browse files
Change Function interface to BiFunction
1 parent 7f5b751 commit f9b57dd

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/StructReader.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.math.BigDecimal;
2525
import java.util.List;
2626
import java.util.UUID;
27+
import java.util.function.BiFunction;
2728
import java.util.function.Function;
2829

2930
/**
@@ -179,58 +180,55 @@ default float getFloat(String columnName) {
179180
/**
180181
* @param columnIndex index of the column
181182
* @return the value of a column with type T or null if the column contains a null value
182-
* <p>Example
183-
*
184-
* <pre>{@code
183+
* <p>Example
184+
* <pre>{@code
185185
* Struct row = ...
186-
* String name = row.getOrNull(1, row::getString)
186+
* String name = row.getOrNull(1, StructReader::getString)
187187
* }</pre>
188188
*/
189-
default <T> T getOrNull(int columnIndex, Function<Integer, T> function) {
190-
return isNull(columnIndex) ? null : function.apply(columnIndex);
189+
default <T> T getOrNull(int columnIndex, BiFunction<StructReader, Integer, T> function) {
190+
return isNull(columnIndex) ? null : function.apply(this, columnIndex);
191191
}
192192

193193
/**
194194
* @param columnName index of the column
195195
* @return the value of a column with type T or null if the column contains a null value
196-
* <p>Example
197-
*
198-
* <pre>{@code
196+
* <p>Example
197+
* <pre>{@code
199198
* Struct row = ...
200-
* String name = row.getOrNull("name", row::getString)
199+
* String name = row.getOrNull("name", StructReader::getString)
201200
* }</pre>
202201
*/
203-
default <T> T getOrNull(String columnName, Function<String, T> function) {
204-
return isNull(columnName) ? null : function.apply(columnName);
202+
default <T> T getOrNull(String columnName, BiFunction<StructReader, String, T> function) {
203+
return isNull(columnName) ? null : function.apply(this, columnName);
205204
}
206205

207206
/**
208207
* @param columnIndex index of the column
209208
* @return the value of a column with type T, or the given default if the column value is null
210-
* <p>Example
211-
*
212-
* <pre>{@code
209+
* <p>Example
210+
* <pre>{@code
213211
* Struct row = ...
214-
* String name = row.getOrDefault(1, row::getString, "")
212+
* String name = row.getOrDefault(1, StructReader::getString, "")
215213
* }</pre>
216214
*/
217-
default <T> T getOrDefault(int columnIndex, Function<Integer, T> function, T defaultValue) {
218-
return isNull(columnIndex) ? defaultValue : function.apply(columnIndex);
215+
default <T> T getOrDefault(
216+
int columnIndex, BiFunction<StructReader, Integer, T> function, T defaultValue) {
217+
return isNull(columnIndex) ? defaultValue : function.apply(this, columnIndex);
219218
}
220219

221220
/**
222221
* @param columnName name of the column
223222
* @return the value of a column with type T, or the given default if the column value is null
224-
*
225-
* <p>Example
226-
*
227-
* <pre>{@code
223+
* <p>Example
224+
* <pre>{@code
228225
* Struct row = ...
229-
* String name = row.getOrDefault("name", row::getString, "")
226+
* String name = row.getOrDefault("name", StructReader::getString, "")
230227
* }</pre>
231228
*/
232-
default <T> T getOrDefault(String columnName, Function<String, T> function, T defaultValue) {
233-
return isNull(columnName) ? defaultValue : function.apply(columnName);
229+
default <T> T getOrDefault(
230+
String columnName, BiFunction<StructReader, String, T> function, T defaultValue) {
231+
return isNull(columnName) ? defaultValue : function.apply(this, columnName);
234232
}
235233

236234
/**

google-cloud-spanner/src/test/java/com/google/cloud/spanner/StructTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ public void getOrNullTests() {
6868
.set("f3")
6969
.to(Value.bool(null))
7070
.build();
71-
String column1 = struct.getOrNull(0, struct::getString);
71+
String column1 = struct.getOrNull(0, StructReader::getString);
7272
assertThat(column1).isEqualTo("x");
7373

74-
Long column2 = struct.getOrNull(1, struct::getLong);
74+
Long column2 = struct.getOrNull(1, StructReader::getLong);
7575
assertThat(column2).isEqualTo(2);
7676

77-
String column3 = struct.getOrNull("f3", struct::getString);
77+
String column3 = struct.getOrNull("f3", StructReader::getString);
7878
assertThat(column3).isNull();
7979
}
8080

@@ -89,13 +89,13 @@ public void getOrDefaultTests() {
8989
.set("f3")
9090
.to(Value.bool(null))
9191
.build();
92-
String column1 = struct.getOrDefault(0, struct::getString, "");
92+
String column1 = struct.getOrDefault(0, StructReader::getString, "");
9393
assertThat(column1).isEqualTo("x");
9494

95-
Long column2 = struct.getOrDefault("f2", struct::getLong, -1L);
95+
Long column2 = struct.getOrDefault("f2", StructReader::getLong, -1L);
9696
assertThat(column2).isEqualTo(2);
9797

98-
String column3 = struct.getOrDefault(2, struct::getString, "");
98+
String column3 = struct.getOrDefault(2, StructReader::getString, "");
9999
assertThat(column3).isEqualTo("");
100100
}
101101

0 commit comments

Comments
 (0)