All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.model_proxy.vm Maven / Gradle / Ivy

/**
 * Copyright 2011 Nikolche Mihajlovski
 *
 * This file is part of JAnnocessor.
 *
 * JAnnocessor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAnnocessor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAnnocessor.  If not, see .
 */

package $packageName;

#smart_import()
#if($model.interfaces.size() > 0 && $model.interfaces[0].asElement.name != 'Serializable')
#set($extends = $packageName + "." + $model.interfaces[0].asElement.name.appendPart("Proxy"))
#end
#set($data = "org.jannocessor.data." + $model.name.appendPart("Data"))

@#type("javax.annotation.Generated")("JAnnocessor-bootstraped")
public class $className#if($extends) extends #type(${extends})#end implements #type(${model.qualifiedName})#if(!$extends), #type('org.jannocessor.model.ParentedElement')#end {

	private static final long serialVersionUID = 1L;

#if($model.methods.size() == 0)
    @SuppressWarnings("unused")
#end
    private transient $model.name adapter;

#if($model.methods.size() == 0)
    @SuppressWarnings("unused")
#end
    private #type($data) data;

    public $className($model.name adapter, ${model.name}Data data) {
#if($extends)
        super(adapter, data);
#end
        this.adapter = adapter;
        this.data = data;
    }

#foreach ($m in $model.methods)
#if($m.name != 'getParent' && $m.name != 'getChildren' && !$helper.isCalculated($m))
	private boolean ${m.name}Initialized = false;

#end
#end

#if(!$extends)

	private CodeNode parent;

	private boolean hasOriginalParent = true;

	@Override
	public void setParent(CodeNode parent) {
		this.parent = parent;
		hasOriginalParent = false;
	}

	@Override
	public CodeNode getParent() {
		return retrieveParent();
	}

	@SuppressWarnings("unchecked")
	protected  T retrieveParent() {
		if (hasOriginalParent) {
			if (parent == null) {
				parent = adapter.getParent();
			}
			return (T) parent;
		} else {
			return (T) parent;
		}
	}

	private void writeObject(#type('java.io.ObjectOutputStream') out) throws #type('java.io.IOException') {
		// avoid serialization of the parent
		CodeNode parentBackup = getParent();
		setParent(null);

		// pre-load all data for serialization
		loadAllData();

		out.defaultWriteObject();

		// restore the parent
		setParent(parentBackup);
	}

	private void readObject(#type('java.io.ObjectInputStream') in) throws #type('java.io.IOException'),
			ClassNotFoundException {
		in.defaultReadObject();
		// currently doesn't do anything extra
	}

#end
#foreach ($m in $model.methods)
#if($m.name != 'getParent' && $m.name != 'getChildren' && !$helper.isCalculated($m))
	@Override
    public #type($m.returnType) ${m.name}() {
        if (!${m.name}Initialized) {
			if (adapter == null) {
				throw new IllegalStateException("Invalid model copy!");
			}
#if($m.returnType.erasure.toString().endsWith('PowerList'))
            data.${m.name.replacePart(0, "set")}(#type('org.jannocessor.model.util.ModelUtils').parentedList(adapter.${m.name}(), this));
#else
            data.${m.name.replacePart(0, "set")}(adapter.${m.name}());
#end
			${m.name}Initialized = true;
        }

        return data.${m.name}();
    }
#elseif($helper.isCalculated($m))
	@Override
    public #type($m.returnType) ${m.name}() {
        return #type('org.jannocessor.model.util.ModelUtils').${m.name}(this);
    }
#elseif($m.name == 'getChildren')
	@Override
    public #type($m.returnType) ${m.name}() {
        throw new #type('org.apache.commons.lang.NotImplementedException')();
    }
#elseif($extends && $m.name == 'getParent')
	@Override
    public #type($m.returnType) ${m.name}() {
        return super.retrieveParent();
    }
#end

#end
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof #type(${model.qualifiedName}))) {
			return false;
		}

		#type(${model.qualifiedName}) other = (#type(${model.qualifiedName})) obj;
		return new #type("org.apache.commons.lang.builder.EqualsBuilder")()
#if ($extends)
				.appendSuper(super.equals(other))
#end
#foreach ($m in $model.methods)
#if ($helper.isIdentityProperty($m))
				.append(this.${m.name}(), other.${m.name}())
#end
#end
				.isEquals();
	}

	@Override
	public int hashCode() {
		return new #type("org.apache.commons.lang.builder.HashCodeBuilder")()
#foreach ($m in $model.methods)
#if ($helper.isIdentityProperty($m))
				.append(this.${m.name}())
#end
#end
				.toHashCode();
	}

	@Override
	public String toString() {
		#type("org.jannocessor.util.TypeSpecificStyle") style = new #type("org.jannocessor.util.TypeSpecificStyle")(#type(${model.qualifiedName}).class);
		#type("org.apache.commons.lang.builder.ToStringBuilder") builder = new #type("org.apache.commons.lang.builder.ToStringBuilder")(this, style);
		appendDescription(builder);
		return builder.toString();
	}

#if ($extends)
	@Override
#end
	protected void appendDescription(#type("org.apache.commons.lang.builder.ToStringBuilder") builder) {
#if ($extends)
        super.appendDescription(builder);
#end
#foreach ($m in $model.methods)
#if ($helper.isToStringMember($m))
        builder.append("${helper.fieldName($m)}", #type("org.jannocessor.model.util.ToStringUtil").describe(this.${m.name}()));
#end
#end
	}

#if($className != 'CodeNodeProxy')
	@Override
#end
	protected void loadAllData() {
#if($className != 'CodeNodeProxy')
		super.loadAllData();
#end

		// load all values from the adapter to the data bean
#foreach ($m in $model.methods)
#if($m.name != 'getParent' && $m.name != 'getChildren' && !$helper.isCalculated($m))
		this.${m.name}();
#end
#end
	}

}

#macro( smart_import )
(!PLACEHOLDER:SMART_IMPORT!)
#end

#macro( type $java_type )$types.useType($java_type)#end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy