«ENDIF»
«ENDFOR»
'''
+
+ def protected hashCodeResult(Collection<GeneratedProperty> properties) '''
+ final int prime = 31;
+ int result = 1;
+ «FOR property : properties»
+ result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
+ «ENDFOR»
+ '''
}
import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
import com.google.common.collect.ImmutableMap
-import java.util.Arrays
import java.util.List
import java.util.Map
import java.util.Objects
return hash;
}
- final int prime = 31;
- int result = 1;
- «FOR property : properties»
- «IF property.returnType.name.contains("[")»
- result = prime * result + «Arrays.importedName».hashCode(«property.fieldName»);
- «ELSE»
- result = prime * result + «Objects.importedName».hashCode(«property.fieldName»);
- «ENDIF»
- «ENDFOR»
+ «hashCodeResult(properties)»
«IF augmentType !== null»
result = prime * result + «Objects.importedName».hashCode(«AUGMENTATION_FIELD»);
«ENDIF»
«targetType.importedName» other = («targetType.importedName»)obj;
«FOR property : properties»
«val fieldName = property.fieldName»
- «IF property.returnType.name.contains("[")»
- if (!«Arrays.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
- «ELSE»
- if (!«Objects.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
- «ENDIF»
+ if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterMethodName»())) {
return false;
}
«ENDFOR»
import com.google.common.io.BaseEncoding
import java.beans.ConstructorProperties
import java.util.ArrayList
-import java.util.Arrays
import java.util.Collections
import java.util.List
import java.util.Map
*
* @return string with the <code>hashCode()</code> method definition in JAVA format
*/
- def protected generateHashCode() '''
- «IF !genTO.hashCodeIdentifiers.empty»
+ def protected generateHashCode() {
+ val size = genTO.hashCodeIdentifiers.size
+ if (size == 0) {
+ return ""
+ }
+ return '''
@«Override.importedName»
public int hashCode() {
- final int prime = 31;
- int result = 1;
- «FOR property : genTO.hashCodeIdentifiers»
- «IF property.returnType.name.contains("[")»
- result = prime * result + «Arrays.importedName».hashCode(«property.fieldName»);
- «ELSE»
- result = prime * result + «Objects.importedName».hashCode(«property.fieldName»);
- «ENDIF»
- «ENDFOR»
+ «IF size != 1»
+ «hashCodeResult(genTO.hashCodeIdentifiers)»
return result;
+ «ELSE»
+ return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
+ «ENDIF»
}
- «ENDIF»
- '''
+ '''
+ }
/**
* Template method which generates the method <code>equals()</code>.
«type.name» other = («type.name») obj;
«FOR property : genTO.equalsIdentifiers»
«val fieldName = property.fieldName»
- «IF property.returnType.name.contains("[")»
- if (!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) {
- «ELSE»
- if (!«Objects.importedName».equals(«fieldName», other.«fieldName»)) {
- «ENDIF»
+ if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
return false;
}
«ENDFOR»
import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;
+import java.util.Arrays;
+import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.opendaylight.mdsal.binding.model.api.ConcreteType;
: new ClassTemplate(innerJavaType, gto).generateAsInnerClass();
}
+ /**
+ * Return imported name of java.util class, whose hashCode/equals methods we want to invoke on the property. Returns
+ * {@link Arrays} if the property is an array, {@link Objects} otherwise.
+ *
+ * @param property Generated property
+ * @return Imported class name
+ */
+ final String importedUtilClass(final GeneratedProperty property) {
+ return importedName(property.getReturnType().getName().indexOf('[') != -1 ? Arrays.class : Objects.class);
+ }
+
static final Restrictions restrictionsForSetter(final Type actualType) {
return actualType instanceof GeneratedType ? null : getRestrictions(actualType);
}
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.List;
+import java.util.Objects;
import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
public static <T> List<T> nonnull(final @Nullable List<T> input) {
return input != null ? input : ImmutableList.of();
}
+
+ /**
+ * Return hash code of a single-property wrapper class. Since the wrapper is not null, we really want to discern
+ * this object being present, hence {@link Objects#hashCode()} is not really useful we would end up with {@code 0}
+ * for both non-present and present-with-null objects.
+ *
+ * @param obj Internal object to hash
+ * @return Wrapper object hash code
+ */
+ public static int wrapperHashCode(final @Nullable Object obj) {
+ return wrapHashCode(Objects.hashCode(obj));
+ }
+
+ /**
+ * Return hash code of a single-property wrapper class. Since the wrapper is not null, we really want to discern
+ * this object being present, hence {@link Arrays#hashCode()} is not really useful we would end up with {@code 0}
+ * for both non-present and present-with-null objects.
+ *
+ * @param obj Internal object to hash
+ * @return Wrapper object hash code
+ */
+ public static int wrapperHashCode(final byte @Nullable[] obj) {
+ return wrapHashCode(Arrays.hashCode(obj));
+ }
+
+ /**
+ * The constant '31' is the result of folding this code:
+ * <pre>
+ * final int prime = 31;
+ * int result = 1;
+ * result = result * prime + Objects.hashCode(obj);
+ * return result;
+ * </pre>
+ * when hashCode is returned as 0, such as due to obj being null or its hashCode being 0.
+ *
+ * @param hash Wrapped object hash
+ * @return Wrapper object hash
+ */
+ private static int wrapHashCode(int hash) {
+ return hash == 0 ? 31 : hash;
+ }
}