1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.util.ObjectUtils; 13 14 import hunt.Exceptions; 15 import std.format; 16 17 18 /** 19 * <p> 20 * The root class from which all event state objects shall be derived. 21 * <p> 22 * All Events are constructed with a reference to the object, the "source", 23 * that is logically deemed to be the object upon which the Event in question 24 * initially occurred upon. 25 */ 26 class EventObject { 27 28 /** 29 * The object on which the Event initially occurred. 30 */ 31 protected Object source; 32 33 /** 34 * Constructs a prototypical Event. 35 * 36 * @param source The object on which the Event initially occurred. 37 * @exception IllegalArgumentException if source is null. 38 */ 39 this(Object source) { 40 if (source is null) 41 throw new IllegalArgumentException("null source"); 42 43 this.source = source; 44 } 45 46 /** 47 * The object on which the Event initially occurred. 48 * 49 * @return The object on which the Event initially occurred. 50 */ 51 Object getSource() { 52 return source; 53 } 54 55 /** 56 * Returns a string representation of this EventObject. 57 * 58 * @return A a string representation of this EventObject. 59 */ 60 override 61 string toString() { 62 return typeid(this).name ~ "[source=" ~ source.toString() ~ "]"; 63 } 64 } 65 66 67 68 class ObjectUtils { 69 70 private enum int INITIAL_HASH = 7; 71 private enum int MULTIPLIER = 31; 72 73 private enum string EMPTY_STRING = ""; 74 private enum string NULL_STRING = "null"; 75 private enum string ARRAY_START = "{"; 76 private enum string ARRAY_END = "}"; 77 private enum string EMPTY_ARRAY = ARRAY_START ~ ARRAY_END; 78 private enum string ARRAY_ELEMENT_SEPARATOR = ", "; 79 80 /** 81 * Return a string representation of an object's overall identity. 82 * @param obj the object (may be {@code null}) 83 * @return the object's identity as string representation, 84 * or an empty string if the object was {@code null} 85 */ 86 static string identityToString(Object obj) { 87 if (obj is null) { 88 return EMPTY_STRING; 89 } 90 return typeid(obj).name ~ "@" ~ getIdentityHexString(obj); 91 } 92 93 94 95 /** 96 * Return a hex String form of an object's identity hash code. 97 * @param obj the object 98 * @return the object's identity code in hex notation 99 */ 100 static string getIdentityHexString(Object obj) { 101 return format("%s", cast(void*)obj); 102 } 103 104 105 //--------------------------------------------------------------------- 106 // Convenience methods for content-based equality/hash-code handling 107 //--------------------------------------------------------------------- 108 109 /** 110 * Determine if the given objects are equal, returning {@code true} if 111 * both are {@code null} or {@code false} if only one is {@code null}. 112 * <p>Compares arrays with {@code Arrays.equals}, performing an equality 113 * check based on the array elements rather than the array reference. 114 * @param o1 first Object to compare 115 * @param o2 second Object to compare 116 * @return whether the given objects are equal 117 * @see Object#equals(Object) 118 * @see java.util.Arrays#equals 119 */ 120 static bool nullSafeEquals(Object o1, Object o2) { 121 if (o1 is o2) { 122 return true; 123 } 124 if (o1 is null || o2 is null) { 125 return false; 126 } 127 if (o1 == o2) { 128 return true; 129 } 130 // if (o1.getClass().isArray() && o2.getClass().isArray()) { 131 // return arrayEquals(o1, o2); 132 // } 133 return false; 134 } 135 136 137 }