package org.opendaylight.mdsal.binding.java.api.generator
import static java.util.Objects.requireNonNull
+import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
+import static org.opendaylight.mdsal.binding.model.util.Types.BYTE_ARRAY;
+import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
+import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableList
import com.google.common.collect.Lists
-import com.google.common.io.BaseEncoding
import java.beans.ConstructorProperties
import java.util.ArrayList
-import java.util.Arrays
+import java.util.Base64;
import java.util.Collections
import java.util.List
import java.util.Map
-import java.util.Objects
import java.util.regex.Pattern
import org.opendaylight.mdsal.binding.model.api.ConcreteType
import org.opendaylight.mdsal.binding.model.api.Constant
import org.opendaylight.mdsal.binding.model.api.Type
import org.opendaylight.mdsal.binding.model.util.TypeConstants
import org.opendaylight.yangtools.yang.binding.CodeHelpers
+import org.opendaylight.yangtools.yang.common.Empty
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
/**
protected val List<GeneratedProperty> properties
protected val List<GeneratedProperty> finalProperties
protected val List<GeneratedProperty> parentProperties
- protected val Iterable<GeneratedProperty> allProperties;
+ protected val Iterable<GeneratedProperty> allProperties
protected val Restrictions restrictions
/**
*/
protected val List<Constant> consts
- protected val GeneratedTransferObject genTO;
+ protected val GeneratedTransferObject genTO
- private val AbstractRangeGenerator<?> rangeGenerator
+ val AbstractRangeGenerator<?> rangeGenerator
/**
* Creates instance of this class with concrete <code>genType</code>.
def protected innerClassesDeclarations() '''
«IF !type.enclosedTypes.empty»
«FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
- «IF (innerClass instanceof GeneratedTransferObject)»
- «new ClassTemplate(javaType.getEnclosedType(innerClass.identifier), innerClass).generateAsInnerClass»
- «ENDIF»
+ «generateInnerClass(innerClass)»
«ENDFOR»
«ENDIF»
'''
* consequence of how this code is structured.
*/
IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
- «Objects.importedName».requireNonNull(_value, "Supplied value may not be null");
- «FOR c : consts»
- «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
- «CodeHelpers.importedName».checkPattern(_value, «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
- «ENDIF»
- «ENDFOR»
+ «CodeHelpers.importedName».requireValue(_value);
+ «genPatternEnforcer("_value")»
«ENDIF»
«FOR p : properties»
}
'''
+ def private genPatternEnforcer(String ref) '''
+ «FOR c : consts»
+ «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
+ «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
+ «ENDIF»
+ «ENDFOR»
+ '''
+
def private static paramValue(Type returnType, String paramName) {
if (returnType instanceof ConcreteType) {
return paramName
* @param source Source object
*/
public «type.name»(«genTO.superType.importedName» source) {
- super(source);
+ super(source);
+ «genPatternEnforcer("getValue()")»
}
'''
«val prop = allProperties.get(0)»
«IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
public static «genTO.name» getDefaultInstance(String defaultValue) {
- «IF "byte[]".equals(prop.returnType.name)»
- «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64();
- return new «genTO.name»(baseEncoding.decode(defaultValue));
- «ELSEIF "java.lang.String".equals(prop.returnType.fullyQualifiedName)»
+ «IF BYTE_ARRAY.equals(prop.returnType)»
+ return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
+ «ELSEIF STRING.equals(prop.returnType)»
return new «genTO.name»(defaultValue);
+ «ELSEIF Constants.EMPTY.equals(prop.returnType)»
+ «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
+ return new «genTO.name»(«Empty.importedName».getInstance());
«ELSEIF allProperties.size > 1»
«bitsArgs»
- «ELSEIF "java.lang.Boolean".equals(prop.returnType.fullyQualifiedName)»
+ «ELSEIF BOOLEAN.equals(prop.returnType)»
return new «genTO.name»(«Boolean.importedName».valueOf(defaultValue));
«ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
return new «genTO.name»(«Byte.importedName».valueOf(defaultValue));
*
* @return string with the <code>hashCode()</code> method definition in JAVA format
*/
- def protected generateHashCode() '''
- «IF !genTO.hashCodeIdentifiers.empty»
- @Override
+ 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»
- return result;
+ «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>.
*/
def protected generateEquals() '''
«IF !genTO.equalsIdentifiers.empty»
- @Override
+ @«Override.importedName»
public boolean equals(java.lang.Object obj) {
if (this == obj) {
return true;
«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»