blob: dcb15e8575b1775db0a4c086d51a42ed06cd7a78 [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:
Andreas Wundsam3846e552014-11-12 17:10:33 -080077//:: if not prop.java_type.is_primitive and (not prop.default_value or prop.default_value != "null"):
78 if(${prop.name} == null) {
79 throw new NullPointerException("${msg.name}: property ${prop.name} cannot be null");
80 }
81//:: #endif
82//:: #endfor
83//:: for prop in msg.data_members:
Andreas Wundsam27303462013-07-16 12:52:35 -070084 this.${prop.name} = ${prop.name};
85//:: #endfor
86 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -070087 //:: else:
88 final static ${impl_class} INSTANCE = new ${impl_class}();
89 // private empty constructor - use shared instance!
90 private ${impl_class}() {
91 }
92 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070093
94 // Accessors for OF message fields
Yotam Harcholf25e8142013-09-09 14:30:13 -070095 //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -070096
Andreas Wundsambc679f72013-08-01 22:13:09 -070097 //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -070098 //:: include("custom/%s.java" % msg.name, msg=msg, version=version)
Andreas Wundsambc679f72013-08-01 22:13:09 -070099 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700100
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700101 //:: if msg.data_members:
Andreas Wundsam5204de22013-07-30 11:34:45 -0700102 public ${msg.interface.name}.Builder createBuilder() {
103 return new BuilderWithParent(this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700104 }
105
Andreas Wundsam5204de22013-07-30 11:34:45 -0700106 static class BuilderWithParent implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700107 final ${impl_class} parentMessage;
108
109 // OF message fields
110//:: for prop in msg.data_members:
111 private boolean ${prop.name}Set;
112 private ${prop.java_type.public_type} ${prop.name};
113//:: #endfor
114
Andreas Wundsam5204de22013-07-30 11:34:45 -0700115 BuilderWithParent(${impl_class} parentMessage) {
Andreas Wundsam27303462013-07-16 12:52:35 -0700116 this.parentMessage = parentMessage;
117 }
118
Yotam Harcholf25e8142013-09-09 14:30:13 -0700119//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True)
Andreas Wundsam27303462013-07-16 12:52:35 -0700120
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700121
Andreas Wundsam27303462013-07-16 12:52:35 -0700122 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700123 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700124 //:: for prop in msg.data_members:
125 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name};
126 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
127 if(${prop.name} == null)
128 throw new NullPointerException("Property ${prop.name} must not be null");
129 //:: #endif
130 //:: #endfor
131
132 //
133 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700134 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700135 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700136 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700137 //:: for i, prop in enumerate(msg.data_members):
138 //:: comma = "," if i < len(msg.data_members)-1 else ""
139 ${prop.name}${comma}
140 //:: #endfor
141 );
Andreas Wundsam27303462013-07-16 12:52:35 -0700142 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700143 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700144 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, version=version, has_parent=True)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700145 //:: #endif
146
Andreas Wundsam27303462013-07-16 12:52:35 -0700147 }
148
Andreas Wundsam5204de22013-07-30 11:34:45 -0700149 static class Builder implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700150 // OF message fields
151//:: for prop in msg.data_members:
152 private boolean ${prop.name}Set;
153 private ${prop.java_type.public_type} ${prop.name};
154//:: #endfor
155
Yotam Harcholf25e8142013-09-09 14:30:13 -0700156//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -0700157//
158 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700159 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700160 //:: for prop in msg.data_members:
161 //:: if prop.default_value:
162 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name};
163 //:: else:
164 if(!this.${prop.name}Set)
165 throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set");
166 //:: #endif
167 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
168 if(${prop.name} == null)
169 throw new NullPointerException("Property ${prop.name} must not be null");
170 //:: #endif
171 //:: #endfor
172
173 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700174 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700175 //:: #endif
176
Andreas Wundsam27303462013-07-16 12:52:35 -0700177 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700178 //:: for i, prop in enumerate(msg.data_members):
179 //:: comma = "," if i < len(msg.data_members)-1 else ""
180 ${prop.name}${comma}
181 //:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700182 );
183 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700184 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700185 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700186 //:: #endif
187
Andreas Wundsam27303462013-07-16 12:52:35 -0700188 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700189 //:: else:
190 // no data members - do not support builder
191 public ${msg.interface.name}.Builder createBuilder() {
192 throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded");
193 }
194 //:: #endif
195
Andreas Wundsam27303462013-07-16 12:52:35 -0700196
197 final static Reader READER = new Reader();
198 static class Reader implements OFMessageReader<${msg.interface.name}> {
199 @Override
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800200 public ${msg.interface.name} readFrom(ByteBuf bb) throws OFParseError {
Yotam Harchola86e4252013-09-06 15:36:28 -0700201//:: for prop in msg.members:
202//:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value):
Andreas Wundsam001b1822013-08-02 22:25:55 -0700203 int start = bb.readerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700204//:: break
205//:: #endif
206//:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700207//:: fields_with_length_member = {}
208//:: for prop in msg.members:
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700209//:: if prop.is_virtual:
210//:: continue
211//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700212 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700213 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
214//:: elif prop.is_pad:
215 // pad: ${prop.length} bytes
216 bb.skipBytes(${prop.length});
Andreas Wundsam27303462013-07-16 12:52:35 -0700217//:: elif prop.is_length_value:
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700218 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
219 //:: if prop.is_fixed_value:
220 if(${prop.name} != ${prop.value})
221 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
222 //:: else:
Andreas Wundsam27303462013-07-16 12:52:35 -0700223 if(${prop.name} < MINIMUM_LENGTH)
224 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700225 //:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700226 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
227 // Buffer does not have all data yet
228 bb.readerIndex(start);
229 return null;
230 }
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700231 //:: if genopts.instrument:
Andreas Wundsame8802ac2013-11-05 11:47:04 -0800232 if(logger.isTraceEnabled())
233 logger.trace("readFrom - length={}", ${prop.name});
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700234 //:: #endif
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700235//:: elif prop.is_fixed_value:
236 // fixed value property ${prop.name} == ${prop.value}
237 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
238 if(${prop.name} != ${prop.priv_value})
239 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
240//:: elif prop.is_field_length_value:
241//:: fields_with_length_member[prop.member.field_name] = prop.name
242 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
243//:: else:
244 // fixme: todo ${prop.name}
Yotam Harchol0178c202013-09-05 16:25:50 -0700245//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700246//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700247 //:: if msg.align:
Andreas Wundsam87bdcf42013-10-23 15:19:01 -0700248 //:: if msg.length_includes_align:
249 // align message to ${msg.align} bytes (length contains aligned value)
250 bb.skipBytes(length - (bb.readerIndex() - start));
251 //:: else:
252 // align message to ${msg.align} bytes (length does not contain alignment)
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700253 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
254 //:: #endif
Andreas Wundsam87bdcf42013-10-23 15:19:01 -0700255 //:: #endif
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700256
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700257 //:: if msg.data_members:
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700258 //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700259 //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700260 //:: #endif
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700261 ${impl_class} ${msg.variable_name} = new ${impl_class}(
Andreas Wundsam27303462013-07-16 12:52:35 -0700262 ${",\n ".join(
263 [ prop.name for prop in msg.data_members])}
264 );
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700265 //:: if genopts.instrument:
Andreas Wundsame8802ac2013-11-05 11:47:04 -0800266 if(logger.isTraceEnabled())
267 logger.trace("readFrom - read={}", ${msg.variable_name});
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700268 //:: #endif
269 return ${msg.variable_name};
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700270 //:: else:
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700271 //:: if genopts.instrument:
Andreas Wundsame8802ac2013-11-05 11:47:04 -0800272 if(logger.isTraceEnabled())
273 logger.trace("readFrom - returning shared instance={}", INSTANCE);
Andreas Wundsam70aa5492013-10-23 15:26:53 -0700274 //:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700275 return INSTANCE;
276 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700277 }
278 }
279
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700280 public void putTo(PrimitiveSink sink) {
281 FUNNEL.funnel(this, sink);
282 }
283
284 final static ${impl_class}Funnel FUNNEL = new ${impl_class}Funnel();
285 static class ${impl_class}Funnel implements Funnel<${impl_class}> {
286 private static final long serialVersionUID = 1L;
287 @Override
288 public void funnel(${impl_class} message, PrimitiveSink sink) {
289//:: for prop in msg.members:
290//:: if prop.is_virtual:
291//:: continue
292//:: elif prop.is_data:
293 ${prop.java_type.funnel_op(version, "message." + prop.name, pub_type=True)};
294//:: elif prop.is_pad:
295 // skip pad (${prop.length} bytes)
296//:: elif prop.is_fixed_value:
297 // fixed value property ${prop.name} = ${prop.value}
298 ${prop.java_type.funnel_op(version, prop.priv_value, pub_type=False)};
299//:: else:
300 // FIXME: skip funnel of ${prop.name}
301//:: #endif
302//:: #endfor
303 }
304 }
305
306
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800307 public void writeTo(ByteBuf bb) {
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700308 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700309 }
310
311 final static Writer WRITER = new Writer();
312 static class Writer implements OFMessageWriter<${impl_class}> {
313 @Override
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800314 public void write(ByteBuf bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700315//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700316 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700317//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700318//:: fields_with_length_member = {}
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800319//:: has_length_value = False
Andreas Wundsam27303462013-07-16 12:52:35 -0700320//:: for prop in msg.members:
321//:: if prop.c_name in fields_with_length_member:
322 int ${prop.name}StartIndex = bb.writerIndex();
323//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700324//:: if prop.is_virtual:
325//:: continue
326//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700327 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700328//:: elif prop.is_pad:
329 // pad: ${prop.length} bytes
330 bb.writeZero(${prop.length});
331//:: elif prop.is_fixed_value:
332 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700333 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700334//:: elif prop.is_length_value:
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800335//:: has_length_value = True
Andreas Wundsam27303462013-07-16 12:52:35 -0700336 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700337//:: if not msg.is_fixed_length:
338 int lengthIndex = bb.writerIndex();
339//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700340 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700341
Andreas Wundsam27303462013-07-16 12:52:35 -0700342//:: elif prop.is_field_length_value:
343//:: fields_with_length_member[prop.member.field_name] = prop.name
344 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
345 // udpated when ${prop.member.field_name} has been written
346 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700347 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700348//:: else:
349 // FIXME: todo write ${prop.name}
350//:: #endif
351//:: if prop.c_name in fields_with_length_member:
352//:: length_member_name = fields_with_length_member[prop.c_name]
353 // update field length member ${length_member_name}
354 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
355 bb.setShort(${length_member_name}Index, ${prop.name}Length);
356//:: #endif
357//:: #endfor
358
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800359//:: if not msg.is_fixed_length and has_length_value:
Andreas Wundsam27303462013-07-16 12:52:35 -0700360 // update length field
361 int length = bb.writerIndex() - startIndex;
Andreas Wundsam5da68512013-10-22 22:18:00 -0700362 //:: if msg.align:
363 int alignedLength = ((length + ${msg.align-1})/${msg.align} * ${msg.align});
364 //:: #endif
365 bb.setShort(lengthIndex, ${"alignedLength" if msg.length_includes_align else "length"});
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700366 //:: if msg.align:
367 // align message to ${msg.align} bytes
Andreas Wundsam5da68512013-10-22 22:18:00 -0700368 bb.writeZero(alignedLength - length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700369 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700370//:: #end
371
372 }
373 }
374
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800375 //:: if os.path.exists("%s/custom/%s_toString.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700376 //:: include("custom/%s_toString.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800377 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700378 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700379 public String toString() {
380 StringBuilder b = new StringBuilder("${msg.name}(");
381 //:: for i, prop in enumerate(msg.data_members):
382 //:: if i > 0:
383 b.append(", ");
384 //:: #endif
385 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
386 //:: #endfor
387 b.append(")");
388 return b.toString();
389 }
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800390 //:: #endif
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700391
392 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700393 public boolean equals(Object obj) {
394 if (this == obj)
395 return true;
396 if (obj == null)
397 return false;
398 if (getClass() != obj.getClass())
399 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700400 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700401 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700402 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700403
404 //:: for prop in msg.data_members:
405 //:: if prop.java_type.is_primitive:
406 if( ${prop.name} != other.${prop.name})
407 return false;
408 //:: elif prop.java_type.is_array:
409 if (!Arrays.equals(${prop.name}, other.${prop.name}))
410 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700411 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700412 if (${prop.name} == null) {
413 if (other.${prop.name} != null)
414 return false;
415 } else if (!${prop.name}.equals(other.${prop.name}))
416 return false;
417 //:: #endif
418 //:: #endfor
419 return true;
420 }
421
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800422 //:: if filter(lambda m: m.name == 'xid', msg.data_members):
423 @Override
424 public boolean equalsIgnoreXid(Object obj) {
425 if (this == obj)
426 return true;
427 if (obj == null)
428 return false;
429 if (getClass() != obj.getClass())
430 return false;
431 //:: if len(msg.data_members) > 0:
432 ${msg.name} other = (${msg.name}) obj;
433 //:: #endif
434
435 //:: for prop in msg.data_members:
436 //:: if prop.java_type.is_primitive and prop.name == 'xid':
437 // ignore XID
438 //:: elif prop.java_type.is_primitive:
439 if( ${prop.name} != other.${prop.name})
440 return false;
441 //:: elif prop.java_type.is_array:
442 if (!Arrays.equals(${prop.name}, other.${prop.name}))
443 return false;
444 //:: else:
445 if (${prop.name} == null) {
446 if (other.${prop.name} != null)
447 return false;
448 } else if (!${prop.name}.equals(other.${prop.name}))
449 return false;
450 //:: #endif
451 //:: #endfor
452 return true;
453 }
454
455 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700456 @Override
457 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700458 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700459 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700460 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700461 int result = 1;
462
463 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700464 //:: if prop.java_type.pub_type == 'long':
465 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700466 //:: elif prop.java_type.pub_type == 'boolean':
467 result = prime * result + (${prop.name} ? 1231 : 1237);
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700468 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700469 result = prime * result + ${prop.name};
470 //:: elif prop.java_type.is_array:
471 result = prime * result + Arrays.hashCode(${prop.name});
472 //:: else:
473 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
474 //:: #endif
475 //:: #endfor
476 return result;
477 }
478
Murat Parlakisikf95672c2016-12-05 00:53:17 -0800479 //:: if filter(lambda m: m.name == 'xid', msg.data_members):
480 @Override
481 public int hashCodeIgnoreXid() {
482 //:: if len(msg.data_members) > 0:
483 final int prime = 31;
484 //:: #endif
485 int result = 1;
486
487 //:: for prop in msg.data_members:
488 //:: if prop.java_type.is_primitive and prop.name == 'xid':
489 // ignore XID
490 //:: elif prop.java_type.pub_type == 'long':
491 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
492 //:: elif prop.java_type.pub_type == 'boolean':
493 result = prime * result + (${prop.name} ? 1231 : 1237);
494 //:: elif prop.java_type.is_primitive:
495 result = prime * result + ${prop.name};
496 //:: elif prop.java_type.is_array:
497 result = prime * result + Arrays.hashCode(${prop.name});
498 //:: else:
499 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
500 //:: #endif
501 //:: #endfor
502 return result;
503 }
504
505 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700506}