Skip to content

Commit 8a8fa0c

Browse files
Add example for each function
1 parent 987af6d commit 8a8fa0c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ default float getFloat(String columnName) {
179179
/**
180180
* @param columnIndex index of the column
181181
* @return the value of a column with type T. return value(T) can be null.
182+
* <p>
183+
* Example:
184+
* <p>
185+
* Struct row = ...
186+
* String name = row.getOrNull(1, row::getString)
187+
* </p>
188+
* </p>
182189
*/
183190
default <T> T getOrNull(int columnIndex, Function<Integer, T> function) {
184191
return isNull(columnIndex) ? null : function.apply(columnIndex);
@@ -187,6 +194,13 @@ default <T> T getOrNull(int columnIndex, Function<Integer, T> function) {
187194
/**
188195
* @param columnName index of the column
189196
* @return the value of a column with type T. return value(T) can be null.
197+
* <p>
198+
* Example:
199+
* <p>
200+
* Struct row = ...
201+
* String name = row.getOrNull("name", row::getString)
202+
* </p>
203+
* </p>
190204
*/
191205
default <T> T getOrNull(String columnName, Function<String, T> function) {
192206
return isNull(columnName) ? null : function.apply(columnName);
@@ -195,6 +209,13 @@ default <T> T getOrNull(String columnName, Function<String, T> function) {
195209
/**
196210
* @param columnIndex index of the column
197211
* @return the value of a column with type T. if column value is null, returns default value.
212+
* <p>
213+
* Example:
214+
* <p>
215+
* Struct row = ...
216+
* String name = row.getOrDefault(1, row::getString, "")
217+
* </p>
218+
* </p>
198219
*/
199220
default <T> T getOrDefault(int columnIndex, Function<Integer, T> function, T defaultValue) {
200221
return isNull(columnIndex) ? defaultValue : function.apply(columnIndex);
@@ -203,6 +224,13 @@ default <T> T getOrDefault(int columnIndex, Function<Integer, T> function, T def
203224
/**
204225
* @param columnName name of the column
205226
* @return the value of a column with type T. if column value is null, returns default value.
227+
* <p>
228+
* Example:
229+
* <p>
230+
* Struct row = ...
231+
* String name = row.getOrDefault("name", row::getString, "")
232+
* </p>
233+
* </p>
206234
*/
207235
default <T> T getOrDefault(String columnName, Function<String, T> function, T defaultValue) {
208236
return isNull(columnName) ? defaultValue : function.apply(columnName);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITReadOnlyTxnTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.fail;
2222

2323
import com.google.cloud.Timestamp;
24+
import com.google.cloud.spanner.AbstractStructReader;
2425
import com.google.cloud.spanner.Database;
2526
import com.google.cloud.spanner.DatabaseClient;
2627
import com.google.cloud.spanner.IntegrationTestEnv;
@@ -32,14 +33,17 @@
3233
import com.google.cloud.spanner.ResultSet;
3334
import com.google.cloud.spanner.Statement;
3435
import com.google.cloud.spanner.Struct;
36+
import com.google.cloud.spanner.StructReader;
3537
import com.google.cloud.spanner.TimestampBound;
3638
import com.google.common.collect.ImmutableList;
3739
import java.util.Collections;
3840
import java.util.List;
3941
import java.util.NavigableMap;
4042
import java.util.TreeMap;
4143
import java.util.concurrent.TimeUnit;
44+
import java.util.function.Function;
4245
import javax.annotation.Nullable;
46+
import org.apache.commons.math3.analysis.function.Abs;
4347
import org.junit.Before;
4448
import org.junit.BeforeClass;
4549
import org.junit.ClassRule;
@@ -133,6 +137,10 @@ public void singleStrong() {
133137
ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction();
134138
Struct row = readRow(readContext);
135139
assertThat(row).isNotNull();
140+
row.getString("StringValue");
141+
String name = getOrNull(row, 0, row::getString);
142+
// String name = row.getOrNull(0, row::getString);
143+
assertThat(name).isEqualTo(expected.value);
136144
assertThat(row.getString(0)).isEqualTo(expected.value);
137145
assertThat(readContext.getReadTimestamp()).isAtLeast(expected.timestamp);
138146

@@ -141,6 +149,10 @@ public void singleStrong() {
141149
assertThat(row.getString(0)).isEqualTo(expected.value);
142150
}
143151

152+
<T> T getOrNull(Struct row, int columnIndex, Function<Integer, T> function) {
153+
return row.isNull(columnIndex) ? null : function.apply(columnIndex);
154+
}
155+
144156
@Test
145157
public void singleReadTimestamp() {
146158
History expected = history.get(2);

0 commit comments

Comments
 (0)