DataElement.vm 5.93 KB
Newer Older
1
#parse("templates/common.vm")
2
3
4
5
#if($package)
package $package;
#end

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

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

11
import com.fasterxml.jackson.annotation.JsonAutoDetect;
12
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
13
14
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
15
16
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
17

18
import lombok.AccessLevel;
Daniel Bluhm's avatar
Daniel Bluhm committed
19
import lombok.Data;
20
import lombok.Getter;
Daniel Bluhm's avatar
Daniel Bluhm committed
21
import lombok.NoArgsConstructor;
Daniel Bluhm's avatar
Daniel Bluhm committed
22
import lombok.NonNull;
23
import lombok.Setter;
Daniel Bluhm's avatar
Daniel Bluhm committed
24

25
26
import org.eclipse.ice.data.JavascriptValidator;

Daniel Bluhm's avatar
Daniel Bluhm committed
27
28
29
30
/**
 * This is an implementation of $interface that satisfies the dependencies of
 * the @DataElement Annotation and was auto-generated by the ICE Framework.
 */
Daniel Bluhm's avatar
Daniel Bluhm committed
31
32
@Data
@NoArgsConstructor
33
34
35
36
37
38
@JsonAutoDetect(
	fieldVisibility = Visibility.ANY,
	getterVisibility = Visibility.NONE,
	isGetterVisibility = Visibility.NONE,
	setterVisibility = Visibility.NONE
)
Daniel Bluhm's avatar
Daniel Bluhm committed
39
public class ${class} implements ${interface}, Serializable {
40
41
42
43
44

	/**
	 * Logging tool
	 */
	private static final Logger logger = LoggerFactory.getLogger(${class}.class);
Daniel Bluhm's avatar
Daniel Bluhm committed
45
	#foreach($field in $fields)
46

47
48
		#fielddoc
		#nonnull($tab, $newline)
Daniel Bluhm's avatar
Daniel Bluhm committed
49
		#if(!${field.Getter} || ${field.VarNameDifferent})
50
	@Getter(AccessLevel.NONE)
51
		#end
Daniel Bluhm's avatar
Daniel Bluhm committed
52
		#if(!${field.Setter} || ${field.VarNameDifferent})
53
	@Setter(AccessLevel.NONE)
54
55
		#end
		#fielddecl
56
	#end
57
58
59
60
	#foreach($field in ${fields.Constants})

		#fielddoc
		#fielddecl
61
	#end
62

Daniel Bluhm's avatar
Daniel Bluhm committed
63
	#foreach($field in ${fields.VarNamesDiffer})
64
		#if(${field.Getter})
65
	/**
Daniel Bluhm's avatar
Daniel Bluhm committed
66
	 * Get ${field.VarName}.
67
68
	 * @return ${field.Name}
	 */
69
	public #fieldtype ${field.GetterName}() {
Daniel Bluhm's avatar
Daniel Bluhm committed
70
		return ${field.VarName};
71
	}
72
73
		#end
		#if(${field.Setter})
74
	/**
Daniel Bluhm's avatar
Daniel Bluhm committed
75
76
	 * Set ${field.VarName}.
	 * @param ${field.VarName} new value for ${field.Name}.
77
	 */
Daniel Bluhm's avatar
Daniel Bluhm committed
78
79
	public void set${field.NameForMethod}(#fieldparametertype ${field.VarName}) {
		this.${field.VarName} = ${field.VarName};
80
81
	}
		#end
Daniel Bluhm's avatar
Daniel Bluhm committed
82
83
	#end

84
	/**
Daniel Bluhm's avatar
Daniel Bluhm committed
85
86
87
88
	 * All args constructor for $class.
	 *
	 * Used in JSON Deserialization.
	#foreach($field in $fields)
Daniel Bluhm's avatar
Daniel Bluhm committed
89
	 * @param ${field.VarName} {@code #fieldtype} for field ${field.VarName}
Daniel Bluhm's avatar
Daniel Bluhm committed
90
	#end
91
	 */
Daniel Bluhm's avatar
Daniel Bluhm committed
92
93
94
	@JsonCreator
	public $class(
	#foreach($field in $fields)
Daniel Bluhm's avatar
Daniel Bluhm committed
95
		@JsonProperty("${field.VarName}") #fieldparametertype ${field.VarName}#if($foreach.hasNext),#end
Daniel Bluhm's avatar
Daniel Bluhm committed
96
97
	#end
	) {
Daniel Bluhm's avatar
Daniel Bluhm committed
98
		#foreach($field in $fields)
Daniel Bluhm's avatar
Daniel Bluhm committed
99
		this.${field.VarName} = ${field.VarName};
Daniel Bluhm's avatar
Daniel Bluhm committed
100
101
		#end
	}
102

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

Daniel Bluhm's avatar
Daniel Bluhm committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	/**
	 * 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
127
128
129
130
131
132
133
	/**
	 * 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)
134
			#if($field.Getter)
Daniel Bluhm's avatar
Daniel Bluhm committed
135
		this.${field.VarName} = element.${field.GetterName}();
Daniel Bluhm's avatar
Daniel Bluhm committed
136
137
138
139
			#end
		#end
	}

140
141
142
143
144
145
146
147
148
149
	/**
	 * 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
150
	@Override
Daniel Bluhm's avatar
Daniel Bluhm committed
151
	public boolean matches(Object o) {
152
153
154
155
156
157
158
159
		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
160
161
162

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

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

					retval =
179
180
					#@settab(6)
						#foreach($field in ${fields.Match})#@nonewline
Daniel Bluhm's avatar
Daniel Bluhm committed
181
						${field.VarName}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$newline#else #end#end
Daniel Bluhm's avatar
Daniel Bluhm committed
182
						#end#end;
183
					#end
184
185
186
187
188
189
190
191
				}
			} 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
192
193
	}

194
195
	/**
	 * This operation serializes the data element to a string in verified JSON.
196
	 *
197
198
	 * @return a JSON string describing the element
	 */
199
200
	@Override
	public String toJson() {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
		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.
216
	 *
217
218
	 * @param jsonDataElement the contents of this data element as JSON
	 */
219
220
	@Override
	public $interface fromJson(final String jsonDataElement) {
221
222
223
		// Load the data from the string with Jackson.
		ObjectMapper mapper = new ObjectMapper();

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

Daniel Bluhm's avatar
Daniel Bluhm committed
230
		return this;
231
	}
232
}