DataElement.vm 6 KB
Newer Older
1 2 3 4
#if($package)
package $package;
#end

5
import java.io.Serializable;
Daniel Bluhm's avatar
Daniel Bluhm committed
6

7 8
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Daniel Bluhm's avatar
Daniel Bluhm committed
9

10
import com.fasterxml.jackson.annotation.JsonAutoDetect;
11
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
12
import com.fasterxml.jackson.annotation.JsonCreator;
13
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
14
import com.fasterxml.jackson.annotation.JsonProperty;
15
import com.fasterxml.jackson.core.JsonProcessingException;
16 17
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
18
import com.fasterxml.jackson.databind.ObjectMapper;
19

20
import lombok.AccessLevel;
21
import lombok.Builder;
Daniel Bluhm's avatar
Daniel Bluhm committed
22
import lombok.Data;
23
import lombok.Getter;
Daniel Bluhm's avatar
Daniel Bluhm committed
24
import lombok.NoArgsConstructor;
25
import lombok.AllArgsConstructor;
Daniel Bluhm's avatar
Daniel Bluhm committed
26
import lombok.NonNull;
27
import lombok.Setter;
Daniel Bluhm's avatar
Daniel Bluhm committed
28

29
#imports
30

Daniel Bluhm's avatar
Daniel Bluhm committed
31 32 33 34
/**
 * This is an implementation of $interface that satisfies the dependencies of
 * the @DataElement Annotation and was auto-generated by the ICE Framework.
 */
35
@Builder
Daniel Bluhm's avatar
Daniel Bluhm committed
36
@Data
37
@AllArgsConstructor
Daniel Bluhm's avatar
Daniel Bluhm committed
38
@NoArgsConstructor
39 40 41 42 43 44
@JsonAutoDetect(
	fieldVisibility = Visibility.ANY,
	getterVisibility = Visibility.NONE,
	isGetterVisibility = Visibility.NONE,
	setterVisibility = Visibility.NONE
)
45
@JsonIgnoreProperties(ignoreUnknown = true)
46
@JsonDeserialize(builder = ${class}.${class}Builder.class)
Daniel Bluhm's avatar
Daniel Bluhm committed
47
public class ${class} implements ${interface}, Serializable {
48 49 50 51 52

	/**
	 * Logging tool
	 */
	private static final Logger logger = LoggerFactory.getLogger(${class}.class);
53 54 55 56 57 58 59 60 61

	/**
	 * Deserialization helpers.
	 */
	@JsonPOJOBuilder(withPrefix = "")
	public static class ${class}Builder implements ${class}BuilderMeta {
	}
	private interface ${class}BuilderMeta {
	}
Daniel Bluhm's avatar
Daniel Bluhm committed
62
	#foreach($field in $fields)
63

64
		#fielddoc
65
		#nonnull($tab, $esc.newline)
Daniel Bluhm's avatar
Daniel Bluhm committed
66
		#if(!${field.Getter} || ${field.VarNameDifferent})
67
	@Getter(AccessLevel.NONE)
68
		#end
Daniel Bluhm's avatar
Daniel Bluhm committed
69
		#if(!${field.Setter} || ${field.VarNameDifferent})
70
	@Setter(AccessLevel.NONE)
71
		#end
72
		#fielddecl(true)
73
	#end
74 75 76 77
	#foreach($field in ${fields.Constants})

		#fielddoc
		#fielddecl
78
	#end
79

Daniel Bluhm's avatar
Daniel Bluhm committed
80
	#foreach($field in ${fields.VarNamesDiffer})
81
		#if(${field.Getter})
82
	/**
Daniel Bluhm's avatar
Daniel Bluhm committed
83
	 * Get ${field.VarName}.
84 85
	 * @return ${field.Name}
	 */
86
	public #fieldtype ${field.GetterName}() {
Daniel Bluhm's avatar
Daniel Bluhm committed
87
		return ${field.VarName};
88
	}
89 90
		#end
		#if(${field.Setter})
91
	/**
Daniel Bluhm's avatar
Daniel Bluhm committed
92 93
	 * Set ${field.VarName}.
	 * @param ${field.VarName} new value for ${field.Name}.
94
	 */
Daniel Bluhm's avatar
Daniel Bluhm committed
95 96
	public void set${field.NameForMethod}(#fieldparametertype ${field.VarName}) {
		this.${field.VarName} = ${field.VarName};
97 98
	}
		#end
Daniel Bluhm's avatar
Daniel Bluhm committed
99 100
	#end

Daniel Bluhm's avatar
Daniel Bluhm committed
101 102 103 104 105 106 107 108
	/**
	 * Copy constructor for $class.
	 * @param other Instance of $class to copy
	 */
	public $class($interface other) {
		copy(other);
	}

Daniel Bluhm's avatar
Daniel Bluhm committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	/**
	 * This operation clones the object. Note that it differs from the base class
	 * implementation in that it will return null if it cannot create the clone to
	 * promote fast failure. See {@link java.lang.Object#clone()};
	 */
	@Override
	public Object clone() {
		try {
			// Call the copy constructor to create the clone.
			return new $class(this);
		} catch (Exception e) {
			logger.error("Unable to clone $class!", e);
			return null;
		}
	}

Daniel Bluhm's avatar
Daniel Bluhm committed
125 126 127 128 129 130 131
	/**
	 * Copy the contents of another element into this element.
	 * @param element the element to copy
	 */
	@Override
	public void copy(@NonNull $interface element) {
		#foreach($field in $fields)
132
			#if($field.Getter)
Daniel Bluhm's avatar
Daniel Bluhm committed
133
		this.${field.VarName} = element.${field.GetterName}();
Daniel Bluhm's avatar
Daniel Bluhm committed
134 135 136 137
			#end
		#end
	}

138 139 140 141 142 143 144 145 146 147
	/**
	 * This function checks deep equality of DataElements to see if all members are
	 * equal ("match") with the exception of fields with match set to false (such
	 * as an automatically generated UUID). This is important for checking if two
	 * objects were generated separately but are otherwise equal.
	 *
	 * @param o the other element to compare
	 * @return true if all members of the element except excluded fields match
	 *         this element.
	 */
Daniel Bluhm's avatar
Daniel Bluhm committed
148
	@Override
Daniel Bluhm's avatar
Daniel Bluhm committed
149
	public boolean matches(Object o) {
150 151 152 153 154 155 156 157
		boolean retval = false;

		// Outer check for null comparisons
		if (o != null) {
			// Next check for shallow comparison
			if (this != o) {
				if (o instanceof $class) {
					$class other = ($class) o;
Daniel Bluhm's avatar
Daniel Bluhm committed
158 159 160

					// Separate boolean checks used to enable better catching
					// by debuggers.
161
					#foreach($field in ${fields.Match})
Daniel Bluhm's avatar
Daniel Bluhm committed
162

Daniel Bluhm's avatar
Daniel Bluhm committed
163 164
					// Matching ${field.VarName}
					boolean ${field.VarName}Match =
165
					#if(${field.Nullable})
Daniel Bluhm's avatar
Daniel Bluhm committed
166 167 168
						this.${field.VarName} == null ?
							this.${field.VarName} == other.${field.VarName} :
							this.${field.VarName}.equals(other.${field.VarName});
169
					#elseif(${field.Primitive})
Daniel Bluhm's avatar
Daniel Bluhm committed
170
						this.${field.VarName} == other.${field.VarName};
171
					#else
Daniel Bluhm's avatar
Daniel Bluhm committed
172
						this.${field.VarName}.equals(other.${field.VarName});
173 174 175 176
					#end## if nullable
					#end## foreach

					retval =
177 178
					#@settab(6)
						#foreach($field in ${fields.Match})#@nonewline
179
						${field.VarName}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$esc.newline#else #end#end
Daniel Bluhm's avatar
Daniel Bluhm committed
180
						#end#end;
181
					#end
182 183 184 185 186 187 188 189
				}
			} else {
				// This should be true if they are the same because the deep comparison is
				// performed otherwise.
				retval = true;
			}
		}
		return retval;
Daniel Bluhm's avatar
Daniel Bluhm committed
190 191
	}

192 193
	/**
	 * This operation serializes the data element to a string in verified JSON.
194
	 *
195 196
	 * @return a JSON string describing the element
	 */
197 198
	@Override
	public String toJson() {
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		String value = null;
		// Convert to json using Jackson
		ObjectMapper mapper = new ObjectMapper();
		try {
			value = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
		} catch (JsonProcessingException e) {
			logger.error("Unable to write DataElement to string!", e);
		}

		return value;
	}

	/**
	 * This operation deserializes a valid JSON string and tries to load it into the
	 * object.
214
	 *
215 216
	 * @param jsonDataElement the contents of this data element as JSON
	 */
217 218
	@Override
	public $interface fromJson(final String jsonDataElement) {
219 220 221
		// Load the data from the string with Jackson.
		ObjectMapper mapper = new ObjectMapper();

222
		try {
223
			copy(mapper.readValue(jsonDataElement, ${class}.class));
224 225 226 227
		} catch (JsonProcessingException e) {
			logger.error("Unable to read DataElement from string!", e);
		}

Daniel Bluhm's avatar
Daniel Bluhm committed
228
		return this;
229
	}
230
}