entity-generator.0.3.source-code.DataEntity.vm Maven / Gradle / Ivy
The newest version!
#set( $parametertype = "${class.name}Parameter" )
// Generated by the DataEntity generator, do not edit
package $class.package;
import java.util.*;
import org.jentity.datamodel.visitor.*;
import org.jentity.datamodel.*;
#if ($class.JavaDoc)
/**
* $class.JavaDoc
*/
#end
public class $class.name extends DataEntity {
#foreach( $att in $class.Attributes )
#if ($att.JavaDoc)
/**
* $att.JavaDoc
*/
#end
public static final ${parametertype} ${att.NameAllUpper} = new ${parametertype}("$att.Name");
public $att.Type get${att.NameUpper}() {
#if($att.Type == "int")
if (getAttribute(${att.NameAllUpper}) != null) {
return ((Integer)getAttribute(${att.NameAllUpper})).intValue();
} else {
return -1;
}
#elseif($att.Type == "boolean")
if (getAttribute(${att.NameAllUpper}) != null) {
return ((Boolean)getAttribute(${att.NameAllUpper})).booleanValue();
} else {
return false;
}
#elseif($att.Type == "long")
if (getAttribute(${att.NameAllUpper}) != null) {
return ((Long)getAttribute(${att.NameAllUpper})).longValue();
} else {
return -1;
}
#elseif($att.Type == "Short")
if (getAttribute(${att.NameAllUpper}) != null) {
return ((Short)getAttribute(${att.NameAllUpper})).shortValue();
} else {
return -1;
}
#elseif($att.Type == "double")
if (getAttribute($att.Name) != null) {
return ((Double)getAttribute(${att.NameAllUpper})).doubleValue();
} else {
return -1;
}
#else
return ($att.Type)getAttribute(${att.NameAllUpper});
#end
}
public void set${att.NameUpper}($att.Type $att.Name) {
#if($att.Type == "int")
setAttribute(${att.NameAllUpper}, new Integer($att.Name));
#elseif($att.Type == "boolean")
setAttribute(${att.NameAllUpper}, new Boolean($att.Name));
#elseif($att.Type == "long")
setAttribute(${att.NameAllUpper}, new Float($att.Name));
#elseif($att.Type == "Short")
setAttribute(${att.NameAllUpper}, new Short($att.Name));
#elseif($att.Type == "double")
setAttribute(${att.NameAllUpper}, new Double($att.Name));
#else
setAttribute(${att.NameAllUpper}, $att.Name);
#end
}
#end
public static class ${parametertype} extends ParameterEnum {
public ${class.name}Parameter(String name) {
super(name);
}
}
@SuppressWarnings("unchecked")
public Class getParameterEnumClass() {
return ${parametertype}.class;
}
@Override
public DataEntity createInstance() {
return new ${class.name}();
}
private static final Map<${parametertype}, AttributeVisitor> visitorMap = new HashMap<${parametertype}, AttributeVisitor>();
static {
#foreach( $att in $class.Attributes )
visitorMap.put(${att.NameAllUpper}, new $att.Visitor ());
#end
}
@Override
public AttributeVisitor getVisitor(ParameterEnum parameter) {
return (AttributeVisitor)visitorMap.get(parameter);
}
//-----------------------------Listener functionality------------------------------------
private LinkedList<${class.name}Listener> listeners = new LinkedList<${class.name}Listener>();
private final ChangeListener coreListener = new ChangeListener() {
public void handleUpdate(ChangeEvent coreChange) {
${class.name}Change changeEvent = new ${class.name}Change(coreChange);
for (Iterator<${class.name}Listener> iter = listeners.iterator(); iter.hasNext();) {
${class.name}Listener listener = (${class.name}Listener) iter.next();
listener.handleUpdate(changeEvent);
}
}
};
/**
* Adds a listener to this Entity. Listeners will be notified of changes to
* the attribute set.
*/
@SuppressWarnings("unchecked")
public void addListener(${class.name}Listener listener) {
if (listeners.size() == 0) {
addListener(coreListener);
}
listeners = (LinkedList<${class.name}Listener>)listeners.clone();
listeners.add(listener);
DataEntity entity = copy();
listener.handleUpdate(new ${class.name}Change(new ChangeListener.ChangeEvent(entity, entity, entity)));
}
/**
* Removes the indicated listener.
*/
@SuppressWarnings("unchecked")
public void removeListener(${class.name}Listener listener) {
listeners = (LinkedList<${class.name}Listener>)listeners.clone();
listeners.add(listener);
if (listeners.size() == 0) {
removeListener(coreListener);
}
}
/**
* Data core listeners must implement this interface.
*/
public interface ${class.name}Listener {
public void handleUpdate(${class.name}Change change);
}
/**
* Contains information one state change in a data core.
*/
public static class ${class.name}Change {
private final ChangeListener.ChangeEvent change;
/**
* Constructs a ChangeEvent
object composed of the supplied parameters.
*/
private ${class.name}Change(ChangeListener.ChangeEvent change) {
this.change = change;
}
/**
* @return Returns the new attribute set applied to the entity.
*/
public ${class.name} getUpdateValues() {
return (${class.name})change.getUpdateValues();
}
/**
* @return Returns the old attribute set replaced in the update. May be null if
* undefined
*/
public ${class.name} getOldValues() {
return (${class.name})change.getOldValues();
}
public ${class.name} getModel() {
return (${class.name})change.getModel();
}
}
}