@Override public int size() { return map.size(); } }
@Override public int size() { return map.size() + updatedKeys.length; } }
public ListValue keys() { String[] keys = new String[size()]; int i = 0; for ( String key : keySet() ) { keys[i++] = key; } return VirtualValues.fromArray( Values.stringArray( keys ) ); }
@Override public boolean equals( VirtualValue other ) { if ( !(other instanceof MapValue) ) { return false; } MapValue that = (MapValue) other; int size = size(); if ( size != that.size() ) { return false; } Iterable<String> keys = keySet(); for ( String key : keys ) { if ( !get( key ).equals( that.get( key ) ) ) { return false; } } return true; }
static <T> T build( final StructureBuilder<AnyValue,T> builder, MapValue map ) { if ( map.size() == 0 ) { throw new InvalidValuesArgumentException( "At least one temporal unit must be specified." ); } map.foreach( builder::add ); return builder.build(); }
@Override public void writeRelationship( long relId, long startNodeId, long endNodeId, TextValue type, MapValue properties ) { append( format( "-[id=%d :%s", relId, type.stringValue() ) ); if ( properties.size() > 0 ) { append( " " ); properties.writeTo( this ); } append( "]-" ); }
private static String onlyTimezone( MapValue map ) { if ( map.size() == 1 ) { String key = single( map.keySet() ); if ( "timezone".equalsIgnoreCase( key ) ) { AnyValue timezone = map.get( key ); if ( timezone instanceof TextValue ) { return ((TextValue) timezone).stringValue(); } } } return null; }
public static Map<String,Object> readMetaDataMap( Neo4jPack.Unpacker unpacker ) throws IOException { MapValue metaDataMapValue = unpacker.unpackMap(); PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter(); Map<String,Object> metaDataMap = new HashMap<>( metaDataMapValue.size() ); metaDataMapValue.foreach( ( key, value ) -> { Object convertedValue = AuthToken.containsSensitiveInformation( key ) ? writer.sensitiveValueAsObject( value, key ) : writer.valueAsObject( value ); metaDataMap.put( key, convertedValue ); } ); return metaDataMap; } }
static Map<String,Object> parseTransactionMetadata( MapValue meta ) throws BoltIOException { AnyValue anyValue = meta.get( TX_META_DATA_KEY ); if ( anyValue == Values.NO_VALUE ) { return null; } else if ( anyValue instanceof MapValue ) { MapValue mapValue = (MapValue) anyValue; TransactionMetadataWriter writer = new TransactionMetadataWriter(); Map<String,Object> txMeta = new HashMap<>( mapValue.size() ); mapValue.foreach( ( key, value ) -> txMeta.put( key, writer.valueAsObject( value ) ) ); return txMeta; } else { throw new BoltIOException( Status.Request.Invalid, "Expecting transaction metadata value to be a Map value, but got: " + anyValue ); } }
@Override public void writeNode( long nodeId, TextArray labels, MapValue properties ) { append( format( "(id=%d", nodeId ) ); String sep = " "; for ( int i = 0; i < labels.length(); i++ ) { append( sep ); append( ":" + labels.stringValue( i ) ); sep = ""; } if ( properties.size() > 0 ) { append( " " ); properties.writeTo( this ); } append( ")" ); }
@Override public <E extends Exception> void writeTo( AnyValueWriter<E> writer ) throws E { writer.beginMap( size() ); foreach( ( s, anyValue ) -> { writer.writeString( s ); anyValue.writeTo( writer ); } ); writer.endMap(); }
private void assertMapValueEquals( MapValue a, MapValue b ) { assertThat( a, equalTo( b ) ); assertThat( a.size(), equalTo( b.size() ) ); assertThat( a.hashCode(), equalTo( b.hashCode() ) ); assertThat( a.keySet(), containsInAnyOrder( Iterables.asArray( String.class, b.keySet() ) ) ); assertThat( Arrays.asList( a.keys().asArray() ), containsInAnyOrder( b.keys().asArray() ) ); a.foreach( ( k, v ) -> assertThat( b.get( k ), equalTo( v ) ) ); b.foreach( ( k, v ) -> assertThat( a.get( k ), equalTo( v ) ) ); }
public static LocalDateTimeValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone ) { Pair<LocalDate,LocalTime> pair = getTruncatedDateAndTime( unit, input, "local date time" ); LocalDate truncatedDate = pair.first(); LocalTime truncatedTime = pair.other(); LocalDateTime truncatedLDT = LocalDateTime.of( truncatedDate, truncatedTime ); if ( fields.size() == 0 ) { return localDateTime( truncatedLDT ); } else { return updateFieldMapWithConflictingSubseconds( fields, unit, truncatedLDT, ( mapValue, localDateTime ) -> { if ( mapValue.size() == 0 ) { return localDateTime( localDateTime ); } else { return build( mapValue.updatedWith( "datetime", localDateTime( localDateTime ) ), defaultZone ); } } ); } }
private static Map<String,Object> toRawMap( MapValue mapValue ) { Deserializer deserializer = new Deserializer(); HashMap<String,Object> map = new HashMap<>( mapValue.size() ); mapValue.foreach( ( key, value ) -> { value.writeTo( deserializer ); map.put( key, deserializer.value() ); } ); return map; }
public static LocalTimeValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone ) { LocalTime localTime = input.getLocalTimePart(); LocalTime truncatedLT = assertValidUnit( () -> localTime.truncatedTo( unit ) ); if ( fields.size() == 0 ) { return localTime( truncatedLT ); } else { return updateFieldMapWithConflictingSubseconds( fields, unit, truncatedLT, ( mapValue, localTime1 ) -> { if ( mapValue.size() == 0 ) { return localTime( localTime1 ); } else { return build( mapValue.updatedWith( "time", localTime( localTime1 ) ), defaultZone ); } } ); } }
public static DateValue truncate( TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone ) { LocalDate localDate = input.getDatePart(); DateValue truncated = date( truncateTo( localDate, unit ) ); if ( fields.size() == 0 ) { return truncated; } else { MapValue updatedFields = fields.updatedWith( "date", truncated ); return build( updatedFields, defaultZone ); } }
private static void assertMapEqualsWithDelta( MapValue a, MapValue b, double delta ) { assertThat( "Map should have same size", a.size(), equalTo( b.size() ) ); a.foreach( ( key, value ) -> { //assertThat( "Missing key", b.get( key ) != Values.NO_VALUE ); AnyValue aValue = value; AnyValue bValue = b.get( key ); if ( aValue instanceof MapValue ) { assertThat( "Value mismatch", bValue instanceof MapValue ); assertMapEqualsWithDelta( (MapValue) aValue, (MapValue) bValue, delta ); } else if ( aValue instanceof DoubleValue ) { assertThat( "Value mismatch", ((DoubleValue) aValue).doubleValue(), closeTo( ((DoubleValue) bValue).doubleValue(), delta ) ); } else { assertThat( "Value mismatch", aValue, equalTo( bValue ) ); } } ); }
@Test void shouldHandleMaps() { // Given Map<String,Object> map = MapUtil.map( "a", Arrays.asList( "foo", 42 ) ); // When AnyValue anyValue = ValueUtils.of( map ); // Then assertThat( anyValue, instanceOf( MapValue.class ) ); MapValue mapValue = (MapValue) anyValue; assertThat( mapValue.get( "a" ), equalTo( VirtualValues.list( stringValue( "foo" ), intValue( 42 ) ) ) ); assertThat( mapValue.size(), equalTo( 1 ) ); }
@Test public void shouldBeAbleToPackAndUnpackMap() throws IOException { // Given PackedOutputArray output = new PackedOutputArray(); Neo4jPack.Packer packer = neo4jPack.newPacker( output ); packer.packMapHeader( ALICE.properties().size() ); ALICE.properties().foreach( ( s, value ) -> { try { packer.pack( s ); packer.pack( value ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } } ); AnyValue unpacked = unpacked( output.bytes() ); // Then assertThat( unpacked, instanceOf( MapValue.class ) ); MapValue unpackedMap = (MapValue) unpacked; assertThat( unpackedMap, equalTo( ALICE.properties() ) ); }
@Test public void profileStatisticConversion() { MapValue convertedMap = ExecutionPlanConverter.convert( new TestExecutionPlanDescription( "description", getProfilerStatistics(), getIdentifiers(), getArguments() ) ); assertEquals( convertedMap.get( "operatorType" ), stringValue( "description" ) ); assertEquals( convertedMap.get( "args" ), ValueUtils.asMapValue( getArguments() ) ); assertEquals( convertedMap.get( "identifiers" ), ValueUtils.asListValue( getIdentifiers() )); assertEquals( convertedMap.get( "children" ), VirtualValues.EMPTY_LIST ); assertEquals( convertedMap.get( "rows" ), longValue( 1L )); assertEquals( convertedMap.get( "dbHits" ), longValue( 2L ) ); assertEquals( convertedMap.get( "pageCacheHits" ), longValue( 3L ) ); assertEquals( convertedMap.get( "pageCacheMisses" ), longValue( 2L ) ); assertEquals( ((DoubleValue) convertedMap.get( "pageCacheHitRatio" )).doubleValue(), 3.0 / 5, 0.0001 ); assertEquals( convertedMap.size(), 9 ); }