blob: 0b032acb7f0cef7b1390ace061ad4525c41d1553 [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
200 public ${msg.interface.name} readFrom(ChannelBuffer 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
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700307 public void writeTo(ChannelBuffer bb) {
308 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
Andreas Wundsama94273b2013-08-01 22:11:33 -0700314 public void write(ChannelBuffer 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 = {}
319//:: for prop in msg.members:
320//:: if prop.c_name in fields_with_length_member:
321 int ${prop.name}StartIndex = bb.writerIndex();
322//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700323//:: if prop.is_virtual:
324//:: continue
325//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700326 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700327//:: elif prop.is_pad:
328 // pad: ${prop.length} bytes
329 bb.writeZero(${prop.length});
330//:: elif prop.is_fixed_value:
331 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700332 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700333//:: elif prop.is_length_value:
334 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700335//:: if not msg.is_fixed_length:
336 int lengthIndex = bb.writerIndex();
337//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700338 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700339
Andreas Wundsam27303462013-07-16 12:52:35 -0700340//:: elif prop.is_field_length_value:
341//:: fields_with_length_member[prop.member.field_name] = prop.name
342 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
343 // udpated when ${prop.member.field_name} has been written
344 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700345 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700346//:: else:
347 // FIXME: todo write ${prop.name}
348//:: #endif
349//:: if prop.c_name in fields_with_length_member:
350//:: length_member_name = fields_with_length_member[prop.c_name]
351 // update field length member ${length_member_name}
352 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
353 bb.setShort(${length_member_name}Index, ${prop.name}Length);
354//:: #endif
355//:: #endfor
356
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700357//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700358 // update length field
359 int length = bb.writerIndex() - startIndex;
Andreas Wundsam5da68512013-10-22 22:18:00 -0700360 //:: if msg.align:
361 int alignedLength = ((length + ${msg.align-1})/${msg.align} * ${msg.align});
362 //:: #endif
363 bb.setShort(lengthIndex, ${"alignedLength" if msg.length_includes_align else "length"});
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700364 //:: if msg.align:
365 // align message to ${msg.align} bytes
Andreas Wundsam5da68512013-10-22 22:18:00 -0700366 bb.writeZero(alignedLength - length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700367 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700368//:: #end
369
370 }
371 }
372
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800373 //:: if os.path.exists("%s/custom/%s_toString.java" % (template_dir, msg.name)):
Andreas Wundsam1fd83332014-10-10 21:30:20 -0700374 //:: include("custom/%s_toString.java" % msg.name, msg=msg, version=version, has_parent=False)
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800375 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700376 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700377 public String toString() {
378 StringBuilder b = new StringBuilder("${msg.name}(");
379 //:: for i, prop in enumerate(msg.data_members):
380 //:: if i > 0:
381 b.append(", ");
382 //:: #endif
383 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
384 //:: #endfor
385 b.append(")");
386 return b.toString();
387 }
Andreas Wundsam20c73b62014-01-24 20:03:27 -0800388 //:: #endif
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700389
390 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700391 public boolean equals(Object obj) {
392 if (this == obj)
393 return true;
394 if (obj == null)
395 return false;
396 if (getClass() != obj.getClass())
397 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700398 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700399 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700400 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700401
402 //:: for prop in msg.data_members:
403 //:: if prop.java_type.is_primitive:
404 if( ${prop.name} != other.${prop.name})
405 return false;
406 //:: elif prop.java_type.is_array:
407 if (!Arrays.equals(${prop.name}, other.${prop.name}))
408 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700409 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700410 if (${prop.name} == null) {
411 if (other.${prop.name} != null)
412 return false;
413 } else if (!${prop.name}.equals(other.${prop.name}))
414 return false;
415 //:: #endif
416 //:: #endfor
417 return true;
418 }
419
420 @Override
421 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700422 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700423 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700424 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700425 int result = 1;
426
427 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700428 //:: if prop.java_type.pub_type == 'long':
429 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700430 //:: elif prop.java_type.pub_type == 'boolean':
431 result = prime * result + (${prop.name} ? 1231 : 1237);
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700432 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700433 result = prime * result + ${prop.name};
434 //:: elif prop.java_type.is_array:
435 result = prime * result + Arrays.hashCode(${prop.name});
436 //:: else:
437 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
438 //:: #endif
439 //:: #endfor
440 return result;
441 }
442
Andreas Wundsam27303462013-07-16 12:52:35 -0700443}