001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.client.tide.data.impl;
022
023 import java.util.Date;
024
025 import org.granite.client.tide.PropertyHolder;
026 import org.granite.client.tide.data.Identifiable;
027 import org.granite.client.tide.data.Lazyable;
028 import org.granite.client.tide.data.spi.DataManager;
029 import org.granite.client.tide.data.spi.EntityDescriptor;
030
031 /**
032 * @author William DRAI
033 */
034 public class ObjectUtil {
035
036 public static boolean isSimple(Object value) {
037 return value instanceof String || value instanceof Boolean || value instanceof Number || value instanceof Date;
038 }
039
040 public static String toString(Object obj) {
041 return obj != null ? obj.toString() : "null";
042 }
043
044 /**
045 * Equality for objects, using uid property when possible
046 *
047 * @param obj1 object
048 * @param obj2 object
049 *
050 * @return true when objects are instances of the same entity
051 */
052 public static boolean objectEquals(DataManager dataManager, Object obj1, Object obj2) {
053 if ((obj1 instanceof PropertyHolder && obj2 instanceof Identifiable) || (obj1 instanceof Identifiable && obj2 instanceof PropertyHolder))
054 return false;
055
056 if (obj1 instanceof Identifiable && obj2 instanceof Identifiable && obj1.getClass() == obj2.getClass()) {
057 if (obj1 instanceof Lazyable && (!((Lazyable)obj1).isInitialized() || !((Lazyable)obj2).isInitialized())) {
058 // Compare with identifier for uninitialized entities
059 EntityDescriptor edesc = dataManager.getEntityDescriptor(obj1);
060 if (edesc.getIdPropertyName() != null)
061 return objectEquals(dataManager, dataManager.getProperty(obj1, edesc.getIdPropertyName()), dataManager.getProperty(obj2, edesc.getIdPropertyName()));
062 }
063 return ((Identifiable)obj1).getUid().equals(((Identifiable)obj2).getUid());
064 }
065
066 if (obj1 == null)
067 return obj2 == null;
068
069 return obj1.equals(obj2);
070 }
071 }