/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This product is licensed to you under the Apache License, Version 2.0 (the "License").
 * You may not use this product except in compliance with the License.
 *
 * This product may include a number of subcomponents with
 * separate copyright notices and license terms. Your use of the source
 * code for these subcomponents is subject to the terms and
 *  conditions of the subcomponent's license, as noted in the LICENSE file.
 */
package org.neo4j.ogm.utils;

import java.util.Collection;

import org.apache.commons.collections4.CollectionUtils;
import org.neo4j.ogm.MetaData;
import org.neo4j.ogm.entity.io.DefaultEntityAccessStrategy;
import org.neo4j.ogm.entity.io.EntityAccessStrategy;
import org.neo4j.ogm.entity.io.FieldReader;
import org.neo4j.ogm.metadata.ClassInfo;
import org.neo4j.ogm.metadata.FieldInfo;

/**
 * The utility methods here will all throw a <code>NullPointerException</code> if invoked with <code>null</code>.
 *
 * @author Luanne Misquitta
 */
public class EntityUtils {

    public static Long identity(Object entity, MetaData metaData) {
        EntityAccessStrategy entityAccessStrategy = new DefaultEntityAccessStrategy();
        ClassInfo classInfo = metaData.classInfo(entity);

        Object id = entityAccessStrategy.getIdentityPropertyReader(classInfo).read(entity);

        return (id == null ? -System.identityHashCode(entity) : (Long) id);
    }

    /**
     * Returns the full set of labels, both static and dynamic, if any, to apply to a node.
     *
     * @param entity
     * @param metaData
     * @return
     */
    public static Collection<String> labels(Object entity, MetaData metaData) {
        ClassInfo classInfo = metaData.classInfo(entity);
        Collection<String> staticLabels = classInfo.staticLabels();
        FieldInfo labelFieldInfo = classInfo.labelFieldOrNull();
        if (labelFieldInfo != null) {
            FieldReader reader = new FieldReader(classInfo, labelFieldInfo);
            Collection<String> labels = (Collection<String>) reader.read(entity);
            return CollectionUtils.union(staticLabels, labels);
        }
        return staticLabels;
    }
}
@@ -581,38 +581,8 @@
 s;%0A%0A
-import java.util.Collection;%0A%0A
 impo
@@ -904,16 +904,46 @@
 dInfo;%0A%0A
+import java.util.Collection;%0A%0A
 /**%0A * T
@@ -1120,29 +1120,30 @@
 %7B%0A%0A    p
-ublic
+rivate
  static 
 Long ide
@@ -1138,65 +1138,13 @@
 tic 
-Long identity(Object entity, MetaData metaData) %7B%0A       
+final
  Ent
@@ -1215,24 +1215,94 @@
 Strategy();%0A
+%0A    public static Long identity(Object entity, MetaData metaData) %7B%0A%0A
         Clas

Improve performance of EntityUtils.identity()