org.neo4j.rest.graphdb.PropertiesMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-neo4j-rest Show documentation
Show all versions of spring-data-neo4j-rest Show documentation
pring Data Neo4j Wrapper for the Neo4j REST API, provides a Graph Database proxy for the remote invocation.
/**
* Copyright (c) 2002-2013 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.rest.graphdb;
/**
* @author mh
* @since 13.12.10
*/
import org.neo4j.graphdb.PropertyContainer;
import java.lang.reflect.Array;
import java.util.*;
public class PropertiesMap {
private final Map values = new HashMap();
public PropertiesMap( PropertyContainer container ) {
for ( String key : container.getPropertyKeys() ) {
values.put( key, container.getProperty( key ) );
}
}
public PropertiesMap( Map map ) {
for ( Map.Entry entry : map.entrySet() ) {
values.put( entry.getKey(), toInternalType( entry.getValue() ) );
}
}
public Object getValue( String key ) {
return values.get( key );
}
public Map serialize() {
// TODO Nice with sorted, but TreeMap the best?
Map result = new TreeMap();
for ( Map.Entry entry : values.entrySet() ) {
result.put( entry.getKey(), toSerializedType( entry.getValue() ) );
}
return result;
}
void storeTo( PropertyContainer container ) {
for ( Map.Entry entry : values.entrySet() ) {
container.setProperty( entry.getKey(), entry.getValue() );
}
}
@SuppressWarnings("unchecked")
private static Object toInternalType( Object value ) {
if ( value instanceof List ) {
List list = (List) value;
if ( list.isEmpty() ) {
return new byte[0];
} else {
Object first = list.get( 0 );
if ( first instanceof String ) {
return stringArray( list );
} else if ( first instanceof Number ) {
return numberArray( list );
} else if ( first instanceof Boolean ) {
return booleanArray( list );
} else {
throw new RuntimeException( "Unsupported array type " + first.getClass() +
". Supported array types are arrays of all java primitives (" +
"byte[], char[], short[], int[], long[], float[], double[]) " +
"and String[]" );
}
}
} else {
return assertSupportedPropertyValue( value );
}
}
public static Object assertSupportedPropertyValue( Object value ) {
if ( value == null ) {
throw new RuntimeException( "null value not supported" );
}
final Class> type = value.getClass();
if (isSupportedType(type) || type.isArray() && isSupportedType(type.getComponentType())) {
return value;
}
throw new RuntimeException( "Unsupported value type " + type + "." +
" Supported value types are all java primitives (byte, char, short, int, " +
"long, float, double) and String, as well as arrays of all those types" );
}
private static boolean isSupportedType(Class> type) {
return type.isPrimitive() || String.class.isAssignableFrom(type) || Number.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type);
}
private static Boolean[] booleanArray( List list ) {
return list.toArray( new Boolean[list.size()] );
}
private static Number[] numberArray( List numbers ) {
Number[] internal = new Number[numbers.size()];
for ( int i = 0; i < internal.length; i++ ) {
Number number = numbers.get( i );
if ( number instanceof Float || number instanceof Double ) {
number = number.doubleValue();
} else {
number = number.longValue();
}
internal[i] = number;
}
final Number[] result;
if ( internal[0] instanceof Double ) {
result = new Double[internal.length];
} else {
result = new Long[internal.length];
}
System.arraycopy( internal, 0, result, 0, internal.length );
return result;
}
private static String[] stringArray( List strings ) {
return strings.toArray( new String[strings.size()] );
}
private Object toSerializedType( Object value ) {
if ( value.getClass().isArray() ) {
if ( value.getClass().getComponentType().isPrimitive() ) {
int size = Array.getLength( value );
List