blob: 071f177add39c03ac9b87413b858352c895f4bf8 [file] [log] [blame]
Andreas Wundsam27303462013-07-16 12:52:35 -07001//:: # Copyright 2013, Big Switch Networks, Inc.
2//:: #
3//:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4//:: # the following special exception:
5//:: #
6//:: # LOXI Exception
7//:: #
8//:: # As a special exception to the terms of the EPL, you may distribute libraries
9//:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10//:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11//:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12//:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13//:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14//:: #
15//:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16//:: #
17//:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18//:: # a copy of the EPL at:
19//:: #
20//:: # http::: #www.eclipse.org/legal/epl-v10.html
21//:: #
22//:: # Unless required by applicable law or agreed to in writing, software
23//:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24//:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25//:: # EPL for the specific language governing permissions and limitations
26//:: # under the EPL.
27//::
28//:: from loxi_ir import *
Andreas Wundsambc679f72013-08-01 22:13:09 -070029//:: import os
Andreas Wundsam27303462013-07-16 12:52:35 -070030//:: import itertools
Andreas Wundsam27303462013-07-16 12:52:35 -070031//:: include('_copyright.java')
32
33//:: include('_autogen.java')
34
35package ${msg.package};
36
37//:: include("_imports.java", msg=msg)
38
Andreas Wundsam99e931d2013-08-22 07:53:53 -070039class ${impl_class} implements ${msg.interface.inherited_declaration()} {
Andreas Wundsam70aa5492013-10-23 15:26:53 -070040//:: if genopts.instrument:
41 private static final Logger logger = LoggerFactory.getLogger(${impl_class}.class);
42//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070043 // version: ${version}
Yotam Harchol791e4882013-09-05 16:32:56 -070044 final static byte WIRE_VERSION = ${version.int_version};
Andreas Wundsam27303462013-07-16 12:52:35 -070045//:: if msg.is_fixed_length:
Yotam Harchol791e4882013-09-05 16:32:56 -070046 final static int LENGTH = ${msg.length};
Andreas Wundsam27303462013-07-16 12:52:35 -070047//:: else:
Yotam Harchol791e4882013-09-05 16:32:56 -070048 final static int MINIMUM_LENGTH = ${msg.min_length};
Andreas Wundsam27303462013-07-16 12:52:35 -070049//:: #endif
50
51//:: for prop in msg.data_members:
Andreas Wundsamc8912c12013-11-15 13:44:48 -080052 //:: if prop.java_type.public_type != msg.interface.member_by_name(prop.name).java_type.public_type:
53 //:: raise Exception("Interface and Class types do not match up: C: {} <-> I: {}".format(prop.java_type.public_type, msg.interface.member_by_name(prop.name).java_type.public_type))
54 //:: #endif
Andreas Wundsamf89f7822013-09-23 14:49:24 -070055 //:: if prop.default_value:
56 private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value};
57 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070058//:: #end
59
60 // OF message fields
61//:: for prop in msg.data_members:
62 private final ${prop.java_type.public_type} ${prop.name};
63//:: #endfor
Andreas Wundsame962d372013-10-02 18:15:58 -070064//
65//:: if all(prop.default_value for prop in msg.data_members):
66 // Immutable default instance
67 final static ${impl_class} DEFAULT = new ${impl_class}(
68 ${", ".join(prop.default_name for prop in msg.data_members)}
69 );
70//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070071
Andreas Wundsam99e931d2013-08-22 07:53:53 -070072 //:: if msg.data_members:
73 // package private constructor - used by readers, builders, and factory
Andreas Wundsam27303462013-07-16 12:52:35 -070074 ${impl_class}(${
75 ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
76//:: for prop in msg.data_members:
77 this.${prop.name} = ${prop.name};
78//:: #endfor
79 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -070080 //:: else:
81 final static ${impl_class} INSTANCE = new ${impl_class}();
82 // private empty constructor - use shared instance!
83 private ${impl_class}() {
84 }
85 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070086
87 // Accessors for OF message fields
Yotam Harcholf25e8142013-09-09 14:30:13 -070088 //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -070089
Andreas Wundsambc679f72013-08-01 22:13:09 -070090 //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -070091 //:: include("custom/%s.java" % msg.name, msg=msg, version=version)
Andreas Wundsambc679f72013-08-01 22:13:09 -070092 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070093
Andreas Wundsam99e931d2013-08-22 07:53:53 -070094 //:: if msg.data_members:
Andreas Wundsam5204de22013-07-30 11:34:45 -070095 public ${msg.interface.name}.Builder createBuilder() {
96 return new BuilderWithParent(this);
Andreas Wundsam27303462013-07-16 12:52:35 -070097 }
98
Andreas Wundsam5204de22013-07-30 11:34:45 -070099 static class BuilderWithParent implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700100 final ${impl_class} parentMessage;
101
102 // OF message fields
103//:: for prop in msg.data_members:
104 private boolean ${prop.name}Set;
105 private ${prop.java_type.public_type} ${prop.name};
106//:: #endfor
107
Andreas Wundsam5204de22013-07-30 11:34:45 -0700108 BuilderWithParent(${impl_class} parentMessage) {
Andreas Wundsam27303462013-07-16 12:52:35 -0700109 this.parentMessage = parentMessage;
110 }
111
Yotam Harcholf25e8142013-09-09 14:30:13 -0700112//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True)
Andreas Wundsam27303462013-07-16 12:52:35 -0700113
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700114
Andreas Wundsam27303462013-07-16 12:52:35 -0700115 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700116 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700117 //:: for prop in msg.data_members:
118 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name};
119 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
120 if(${prop.name} == null)
121 throw new NullPointerException("Property ${prop.name} must not be null");
122 //:: #endif
123 //:: #endfor
124
125 //
126 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700127 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700128 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700129 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700130 //:: for i, prop in enumerate(msg.data_members):
131 //:: comma = "," if i < len(msg.data_members)-1 else ""
132 ${prop.name}${comma}
133 //:: #endfor
134 );
Andreas Wundsam27303462013-07-16 12:52:35 -0700135 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700136 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700137 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, version=version, has_parent=True)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700138 //:: #endif
139
Andreas Wundsam27303462013-07-16 12:52:35 -0700140 }
141
Andreas Wundsam5204de22013-07-30 11:34:45 -0700142 static class Builder implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700143 // OF message fields
144//:: for prop in msg.data_members:
145 private boolean ${prop.name}Set;
146 private ${prop.java_type.public_type} ${prop.name};
147//:: #endfor
148
Yotam Harcholf25e8142013-09-09 14:30:13 -0700149//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -0700150//
151 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700152 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700153 //:: for prop in msg.data_members:
154 //:: if prop.default_value:
155 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name};
156 //:: else:
157 if(!this.${prop.name}Set)
158 throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set");
159 //:: #endif
160 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
161 if(${prop.name} == null)
162 throw new NullPointerException("Property ${prop.name} must not be null");
163 //:: #endif
164 //:: #endfor
165
166 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700167 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700168 //:: #endif
169
Andreas Wundsam27303462013-07-16 12:52:35 -0700170 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700171 //:: for i, prop in enumerate(msg.data_members):
172 //:: comma = "," if i < len(msg.data_members)-1 else ""
173 ${prop.name}${comma}
174 //:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700175 );
176 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700177 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700178 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700179 //:: #endif
180
Andreas Wundsam27303462013-07-16 12:52:35 -0700181 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700182 //:: else:
183 // no data members - do not support builder
184 public ${msg.interface.name}.Builder createBuilder() {
185 throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded");
186 }
187 //:: #endif
188
Andreas Wundsam27303462013-07-16 12:52:35 -0700189
190 final static Reader READER = new Reader();
191 static class Reader implements OFMessageReader<${msg.interface.name}> {
192 @Override
193 public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
Yotam Harchola86e4252013-09-06 15:36:28 -0700194//:: for prop in msg.members:
195//:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value):
Andreas Wundsam001b1822013-08-02 22:25:55 -0700196 int start = bb.readerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700197//:: break
198//:: #endif
199//:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700200//:: fields_with_length_member = {}
201//:: for prop in msg.members:
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700202//:: if prop.is_virtual:
203//:: continue
204//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700205 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700206 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
207//:: elif prop.is_pad:
208 // pad: ${prop.length} bytes
209 bb.skipBytes(${prop.length});
Andreas Wundsam27303462013-07-16 12:52:35 -0700210//:: elif prop.is_length_value:
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700211 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
212 //:: if prop.is_fixed_value:
213 if(${prop.name} != ${prop.value})
214 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
215 //:: else:
Andreas Wundsam27303462013-07-16 12:52:35 -0700216 if(${prop.name} < MINIMUM_LENGTH)
217 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700218 //:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700219 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
220 // Buffer does not have all data yet
221 bb.readerIndex(start);
222 return null;
223 }
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700224 //:: if genopts.instrument:
Andreas Wundsame8802ac2013-11-05 11:47:04 -0800225 if(logger.isTraceEnabled())
226 logger.trace("readFrom - length={}", ${prop.name});
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700227 //:: #endif
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700228//:: elif prop.is_fixed_value:
229 // fixed value property ${prop.name} == ${prop.value}
230 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
231 if(${prop.name} != ${prop.priv_value})
232 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
233//:: elif prop.is_field_length_value:
234//:: fields_with_length_member[prop.member.field_name] = prop.name
235 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
236//:: else:
237 // fixme: todo ${prop.name}
Yotam Harchol0178c202013-09-05 16:25:50 -0700238//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700239//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700240 //:: if msg.align:
Andreas Wundsam87bdcf42013-10-23 15:19:01 -0700241 //:: if msg.length_includes_align:
242 // align message to ${msg.align} bytes (length contains aligned value)
243 bb.skipBytes(length - (bb.readerIndex() - start));
244 //:: else:
245 // align message to ${msg.align} bytes (length does not contain alignment)
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700246 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
247 //:: #endif
Andreas Wundsam87bdcf42013-10-23 15:19:01 -0700248 //:: #endif
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700249
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700250 //:: if msg.data_members:
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700251 //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700252 //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700253 //:: #endif
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700254 ${impl_class} ${msg.variable_name} = new ${impl_class}(
Andreas Wundsam27303462013-07-16 12:52:35 -0700255 ${",\n ".join(
256 [ prop.name for prop in msg.data_members])}
257 );
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700258 //:: if genopts.instrument:
Andreas Wundsame8802ac2013-11-05 11:47:04 -0800259 if(logger.isTraceEnabled())
260 logger.trace("readFrom - read={}", ${msg.variable_name});
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700261 //:: #endif
262 return ${msg.variable_name};
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700263 //:: else:
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700264 //:: if genopts.instrument:
Andreas Wundsame8802ac2013-11-05 11:47:04 -0800265 if(logger.isTraceEnabled())
266 logger.trace("readFrom - returning shared instance={}", INSTANCE);
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700267 //:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700268 return INSTANCE;
269 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700270 }
271 }
272
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700273 public void putTo(PrimitiveSink sink) {
274 FUNNEL.funnel(this, sink);
275 }
276
277 final static ${impl_class}Funnel FUNNEL = new ${impl_class}Funnel();
278 static class ${impl_class}Funnel implements Funnel<${impl_class}> {
279 private static final long serialVersionUID = 1L;
280 @Override
281 public void funnel(${impl_class} message, PrimitiveSink sink) {
282//:: for prop in msg.members:
283//:: if prop.is_virtual:
284//:: continue
285//:: elif prop.is_data:
286 ${prop.java_type.funnel_op(version, "message." + prop.name, pub_type=True)};
287//:: elif prop.is_pad:
288 // skip pad (${prop.length} bytes)
289//:: elif prop.is_fixed_value:
290 // fixed value property ${prop.name} = ${prop.value}
291 ${prop.java_type.funnel_op(version, prop.priv_value, pub_type=False)};
292//:: else:
293 // FIXME: skip funnel of ${prop.name}
294//:: #endif
295//:: #endfor
296 }
297 }
298
299
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700300 public void writeTo(ChannelBuffer bb) {
301 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700302 }
303
304 final static Writer WRITER = new Writer();
305 static class Writer implements OFMessageWriter<${impl_class}> {
306 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700307 public void write(ChannelBuffer bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700308//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700309 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700310//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700311//:: fields_with_length_member = {}
312//:: for prop in msg.members:
313//:: if prop.c_name in fields_with_length_member:
314 int ${prop.name}StartIndex = bb.writerIndex();
315//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700316//:: if prop.is_virtual:
317//:: continue
318//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700319 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700320//:: elif prop.is_pad:
321 // pad: ${prop.length} bytes
322 bb.writeZero(${prop.length});
323//:: elif prop.is_fixed_value:
324 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700325 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700326//:: elif prop.is_length_value:
327 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700328//:: if not msg.is_fixed_length:
329 int lengthIndex = bb.writerIndex();
330//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700331 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700332
Andreas Wundsam27303462013-07-16 12:52:35 -0700333//:: elif prop.is_field_length_value:
334//:: fields_with_length_member[prop.member.field_name] = prop.name
335 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
336 // udpated when ${prop.member.field_name} has been written
337 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700338 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700339//:: else:
340 // FIXME: todo write ${prop.name}
341//:: #endif
342//:: if prop.c_name in fields_with_length_member:
343//:: length_member_name = fields_with_length_member[prop.c_name]
344 // update field length member ${length_member_name}
345 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
346 bb.setShort(${length_member_name}Index, ${prop.name}Length);
347//:: #endif
348//:: #endfor
349
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700350//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700351 // update length field
352 int length = bb.writerIndex() - startIndex;
Andreas Wundsam5da68512013-10-22 22:18:00 -0700353 //:: if msg.align:
354 int alignedLength = ((length + ${msg.align-1})/${msg.align} * ${msg.align});
355 //:: #endif
356 bb.setShort(lengthIndex, ${"alignedLength" if msg.length_includes_align else "length"});
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700357 //:: if msg.align:
358 // align message to ${msg.align} bytes
Andreas Wundsam5da68512013-10-22 22:18:00 -0700359 bb.writeZero(alignedLength - length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700360 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700361//:: #end
362
363 }
364 }
365
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800366 //:: if os.path.exists("%s/custom/%s_toString.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700367 //:: include("custom/%s_toString.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800368 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700369 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700370 public String toString() {
371 StringBuilder b = new StringBuilder("${msg.name}(");
372 //:: for i, prop in enumerate(msg.data_members):
373 //:: if i > 0:
374 b.append(", ");
375 //:: #endif
376 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
377 //:: #endfor
378 b.append(")");
379 return b.toString();
380 }
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800381 //:: #endif
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700382
383 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700384 public boolean equals(Object obj) {
385 if (this == obj)
386 return true;
387 if (obj == null)
388 return false;
389 if (getClass() != obj.getClass())
390 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700391 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700392 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700393 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700394
395 //:: for prop in msg.data_members:
396 //:: if prop.java_type.is_primitive:
397 if( ${prop.name} != other.${prop.name})
398 return false;
399 //:: elif prop.java_type.is_array:
400 if (!Arrays.equals(${prop.name}, other.${prop.name}))
401 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700402 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700403 if (${prop.name} == null) {
404 if (other.${prop.name} != null)
405 return false;
406 } else if (!${prop.name}.equals(other.${prop.name}))
407 return false;
408 //:: #endif
409 //:: #endfor
410 return true;
411 }
412
413 @Override
414 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700415 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700416 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700417 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700418 int result = 1;
419
420 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700421 //:: if prop.java_type.pub_type == 'long':
422 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700423 //:: elif prop.java_type.pub_type == 'boolean':
424 result = prime * result + (${prop.name} ? 1231 : 1237);
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700425 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700426 result = prime * result + ${prop.name};
427 //:: elif prop.java_type.is_array:
428 result = prime * result + Arrays.hashCode(${prop.name});
429 //:: else:
430 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
431 //:: #endif
432 //:: #endfor
433 return result;
434 }
435
Andreas Wundsam27303462013-07-16 12:52:35 -0700436}