|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.docs.driver; |
| 20 | + |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import org.neo4j.driver.Record; |
| 24 | +import org.neo4j.driver.Result; |
| 25 | +import org.neo4j.driver.Session; |
| 26 | +import org.neo4j.driver.Value; |
| 27 | + |
| 28 | +import static org.neo4j.driver.Values.parameters; |
| 29 | + |
| 30 | +public class ReadingValuesExample extends BaseApplication |
| 31 | +{ |
| 32 | + private static final String nullFieldName = "fieldName"; |
| 33 | + private static final String intFieldName = "fieldName"; |
| 34 | + |
| 35 | + public ReadingValuesExample( String uri, String user, String password ) |
| 36 | + { |
| 37 | + super( uri, user, password ); |
| 38 | + } |
| 39 | + |
| 40 | + public Boolean nullIsNull() |
| 41 | + { |
| 42 | + return echo( null, record -> |
| 43 | + { |
| 44 | + // tag::java-driver-reading-values-null[] |
| 45 | + Value possibleNullValue = record.get( nullFieldName ); |
| 46 | + |
| 47 | + // Checking if its null |
| 48 | + boolean wasNull = possibleNullValue.isNull(); // true |
| 49 | + |
| 50 | + // end::java-driver-reading-values-null[] |
| 51 | + return wasNull; |
| 52 | + } ); |
| 53 | + } |
| 54 | + |
| 55 | + public String nullAsString() |
| 56 | + { |
| 57 | + return echo( null, record -> |
| 58 | + { |
| 59 | + Value possibleNullValue = record.get( nullFieldName ); |
| 60 | + // tag::java-driver-reading-values-null[] |
| 61 | + // Getting the null value as string |
| 62 | + String stringWithNullContent = possibleNullValue.asString(); // "null" |
| 63 | + |
| 64 | + // end::java-driver-reading-values-null[] |
| 65 | + return stringWithNullContent; |
| 66 | + } ); |
| 67 | + } |
| 68 | + |
| 69 | + public Object nullAsObject() |
| 70 | + { |
| 71 | + return echo( null, record -> |
| 72 | + { |
| 73 | + Value possibleNullValue = record.get( nullFieldName ); |
| 74 | + // tag::java-driver-reading-values-null[] |
| 75 | + // Getting `null` as object |
| 76 | + Object nullObject = possibleNullValue.asObject(); // null |
| 77 | + |
| 78 | + // end::java-driver-reading-values-null[] |
| 79 | + return nullObject; |
| 80 | + } ); |
| 81 | + } |
| 82 | + |
| 83 | + public float nullAsObjectFloatDefaultValue() |
| 84 | + { |
| 85 | + return echo( null, record -> |
| 86 | + { |
| 87 | + Value possibleNullValue = record.get( nullFieldName ); |
| 88 | + // tag::java-driver-reading-values-null[] |
| 89 | + // Coercing value with a default value set |
| 90 | + float floatValue = possibleNullValue.asFloat( 1.0f ); // 1.0f |
| 91 | + |
| 92 | + // end::java-driver-reading-values-null[] |
| 93 | + return floatValue; |
| 94 | + } ); |
| 95 | + } |
| 96 | + |
| 97 | + public void nullAsObjectFloat() |
| 98 | + { |
| 99 | + echo( null, record -> |
| 100 | + { |
| 101 | + Value possibleNullValue = record.get( nullFieldName ); |
| 102 | + // tag::java-driver-reading-values-null[] |
| 103 | + // Could not cast null to float |
| 104 | + float floatValue = possibleNullValue.asFloat(); // throws org.neo4j.driver.exceptions.value.Uncoercible |
| 105 | + // end::java-driver-reading-values-null[] |
| 106 | + return floatValue; |
| 107 | + } ); |
| 108 | + } |
| 109 | + |
| 110 | + public Boolean integerFieldIsNull() |
| 111 | + { |
| 112 | + return echo( 4, record -> |
| 113 | + { |
| 114 | + // tag::java-driver-reading-values-non-null[] |
| 115 | + Value value = record.get( intFieldName ); |
| 116 | + // Checking if the value is null |
| 117 | + Boolean falseBoolean = value.isNull(); // false |
| 118 | + |
| 119 | + // end::java-driver-reading-values-non-null[] |
| 120 | + return falseBoolean; |
| 121 | + } ); |
| 122 | + } |
| 123 | + |
| 124 | + public int integerAsInteger() |
| 125 | + { |
| 126 | + return echo( 4, record -> |
| 127 | + { |
| 128 | + Value value = record.get( intFieldName ); |
| 129 | + |
| 130 | + // tag::java-driver-reading-values-non-null[] |
| 131 | + // Getting as int |
| 132 | + int intValue = value.asInt(); // the int |
| 133 | + |
| 134 | + // end::java-driver-reading-values-non-null[] |
| 135 | + return intValue; |
| 136 | + } ); |
| 137 | + } |
| 138 | + |
| 139 | + public long integerAsLong() |
| 140 | + { |
| 141 | + return echo( 4, record -> |
| 142 | + { |
| 143 | + Value value = record.get( intFieldName ); |
| 144 | + |
| 145 | + // tag::java-driver-reading-values-non-null[] |
| 146 | + // Getting value asLong is also possible for int values |
| 147 | + long longValue = value.asLong(); // the int casted to long |
| 148 | + |
| 149 | + // end::java-driver-reading-values-non-null[] |
| 150 | + return longValue; |
| 151 | + } ); |
| 152 | + } |
| 153 | + |
| 154 | + public void integerAsString() |
| 155 | + { |
| 156 | + echo( 4, record -> |
| 157 | + { |
| 158 | + Value value = record.get( intFieldName ); |
| 159 | + |
| 160 | + // tag::java-driver-reading-values-non-null[] |
| 161 | + // But it's not possible to get the int value as string |
| 162 | + String stringValue = value.asString(); // throws org.neo4j.driver.exceptions.value.Uncoercible |
| 163 | + // end::java-driver-reading-values-non-null[] |
| 164 | + return stringValue; |
| 165 | + } ); |
| 166 | + } |
| 167 | + |
| 168 | + private <T, O> O echo( T value, Function<Record,O> transformation ) |
| 169 | + { |
| 170 | + try ( Session session = driver.session() ) |
| 171 | + { |
| 172 | + return session.readTransaction( tx -> |
| 173 | + { |
| 174 | + Result result = tx.run( "RETURN $in AS fieldName", parameters( "in", value ) ); |
| 175 | + Record record = result.next(); |
| 176 | + return transformation.apply( record ); |
| 177 | + } ); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments