Skip to content

Commit 39cf50f

Browse files
author
Steve Powell
committed
Make *Properties.Builder and build immutable Properties object:
remove {AMQ/}{File/Stream/Test/Tunnel}Properties.java (unused abstracts); generate *Properties.Builder with setters and build() methods; remove uses of setters on *Properties object (and modify interface); adjust tests that set properties to use builders instead.
1 parent ca6d804 commit 39cf50f

21 files changed

+246
-265
lines changed

codegen.py

Lines changed: 106 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,22 @@ def java_constant_name(c):
5151
'Date'
5252
])
5353

54-
# the types in the range of javaTypeMap must be in java_scala_types
54+
# the scalar types in the range of javaTypeMap must be in java_scala_types
5555
java_scalar_types = set([
5656
'int',
5757
'long',
5858
'boolean'
5959
])
60+
javaScalarDefaultMap = {
61+
'int': '0',
62+
'long': '0L',
63+
'boolean': 'false'
64+
}
65+
def java_scalar_default(jtype):
66+
if jtype in java_scalar_types:
67+
return javaScalarDefaultMap[jtype]
68+
else:
69+
return 'null'
6070

6171
# the java_scalar_types must be in the domain of javaBoxedTypeMap
6272
javaBoxedTypeMap = {
@@ -153,6 +163,7 @@ def printHeader():
153163
printFileHeader()
154164
print "package com.rabbitmq.client;"
155165
print
166+
print "import java.io.DataInputStream;"
156167
print "import java.io.IOException;"
157168
print "import java.util.Collections;"
158169
print "import java.util.HashMap;"
@@ -195,12 +206,8 @@ def genArgMethods(spec, m):
195206
for a in m.arguments:
196207
(jfType, jfName, jfDefault) = typeNameDefault(spec, a)
197208

198-
if jfType == "Map<String,Object>":
199-
print " public Builder %s(%s %s)" % (jfName, jfType, jfName)
200-
print " { this.%s = %s==null ? null : Collections.unmodifiableMap(new HashMap<String,Object>(%s)); return this; }" % (jfName, jfName, jfName)
201-
else:
202-
print " public Builder %s(%s %s)" % (jfName, jfType, jfName)
203-
print " { this.%s = %s; return this; }" % (jfName, jfName)
209+
print " public Builder %s(%s %s)" % (jfName, jfType, jfName)
210+
print " { this.%s = %s; return this; }" % (jfName, jfName)
204211

205212
if jfType == "boolean":
206213
print " public Builder %s()" % (jfName)
@@ -238,27 +245,26 @@ def printClassInterfaces():
238245
print " }"
239246
print " }"
240247

241-
def printReadPropertiesFrom(c):
242-
print
243-
print " public void readPropertiesFrom(ContentHeaderPropertyReader reader)"
244-
print " throws IOException"
245-
print " {"
248+
def printReadProperties(c):
249+
if c.fields:
250+
for f in c.fields:
251+
(jfName, jfType) = (java_field_name(f.name), java_field_type(spec, f.domain))
252+
if jfType in java_scalar_types:
253+
print " this.%sIsSet = reader.readPresence();" % (jfName)
254+
else:
255+
print " boolean %s_present = reader.readPresence();" % (jfName)
256+
print
246257

247-
for f in c.fields:
248-
(jfName, jfType) = (java_field_name(f.name), java_field_type(spec, f.domain))
249-
if jfType in java_scalar_types:
250-
print " this.%sIsSet = reader.readPresence();" % (jfName)
251-
else:
252-
print " boolean %s_present = reader.readPresence();" % (jfName)
253258
print " reader.finishPresence();"
254-
for f in c.fields:
255-
(jfName, jfType, jfClass) = (java_field_name(f.name), java_field_type(spec, f.domain), java_class_name(f.domain))
256-
if jfType in java_scalar_types:
257-
print " if (this.%sIsSet) this.%s = reader.read%s();" % (jfName, jfName, jfClass)
258-
else:
259-
print " this.%s = %s_present ? reader.read%s() : null;" % (jfName, jfName, jfClass)
260259

261-
print " }"
260+
if c.fields:
261+
print
262+
for f in c.fields:
263+
(jfName, jfType, jfClass) = (java_field_name(f.name), java_field_type(spec, f.domain), java_class_name(f.domain))
264+
if jfType in java_scalar_types:
265+
print " if (this.%sIsSet) this.%s = reader.read%s();" % (jfName, jfName, jfClass)
266+
else:
267+
print " this.%s = %s_present ? reader.read%s() : null;" % (jfName, jfName, jfClass)
262268

263269
def printWritePropertiesTo(c):
264270
print
@@ -298,9 +304,53 @@ def optionalValueClause(jField, jType):
298304
print " }"
299305

300306
def printPropertiesBuilderClass(c):
307+
def printBuilderSetter(fieldType, fieldName):
308+
if fieldType in java_scalar_types:
309+
print " public Builder %s(%s %s)" % (fieldName, fieldType, fieldName)
310+
print " { this.%s = %s; this.%sIsSet = true; return this; }" % (fieldName, fieldName, fieldName)
311+
print " public Builder %sUnSet() { this.%sIsSet = false; return this; }" % (fieldName, fieldName)
312+
if fieldType == "boolean":
313+
print " public Builder %s()" % (fieldName)
314+
print " { return this.%s(true); }" % (fieldName)
315+
else:
316+
print " public Builder %s(%s %s)" % (fieldName, fieldType, fieldName)
317+
print " { this.%s = %s; return this; }" % (fieldName, fieldName)
318+
if fieldType == "LongString":
319+
print " public Builder %s(String %s)" % (fieldName, fieldName)
320+
print " { return this.%s(LongStringHelper.asLongString(%s)); }" % (fieldName, fieldName)
321+
322+
def ctorParm(field):
323+
(fType, fName) = (java_field_type(spec, field.domain), java_field_name(field.name))
324+
if fType in java_scalar_types:
325+
return "%sIsSet ? %s : null" % (fName, fName)
326+
else:
327+
return fName
328+
329+
print
301330
print " public static final class Builder {"
331+
# fields
302332
for f in c.fields:
303-
print " private %s %s;" % (java_field_type(spec, f.domain),java_field_name(f.name))
333+
(fType, fName) = (java_field_type(spec, f.domain), java_field_name(f.name))
334+
if fType in java_scalar_types:
335+
print " private boolean %sIsSet = false;" % (fName)
336+
print " private %s %s;" % (fType, fName)
337+
# ctor
338+
print
339+
print " public Builder() {};"
340+
# setters
341+
print
342+
for f in c.fields:
343+
printBuilderSetter(java_field_type(spec, f.domain), java_field_name(f.name))
344+
print
345+
jClassName = java_class_name(c.name)
346+
# build()
347+
objName = "%sProperties" % (jClassName)
348+
ctor_parm_list = [ ctorParm(f) for f in c.fields ]
349+
print " public %s build() {" % (objName)
350+
print " return new %s" % (objName)
351+
print " ( %s" % ("\n , ".join(ctor_parm_list))
352+
print " );"
353+
print " }"
304354

305355
print " }"
306356

@@ -309,42 +359,51 @@ def printGetterAndSetter(fieldType, fieldName):
309359
capFieldName = fieldName[0].upper() + fieldName[1:]
310360
print " public %s get%s() { return this.%s; }" % (fieldType, capFieldName, fieldName)
311361
if fieldType in java_scalar_types:
312-
print " public void set%s(%s %s)" % (capFieldName, fieldType, fieldName)
313-
print " { this.%s = %s; this.%sIsSet = true; }" % (fieldName, fieldName, fieldName)
362+
print " public void set%s(%s %s)" % (capFieldName, fieldType, fieldName)
363+
print " { this.%s = %s; this.%sIsSet = true; }" % (fieldName, fieldName, fieldName)
314364
else:
315-
print " public void set%s(%s %s) { this.%s = %s; }" % (capFieldName, fieldType, fieldName, fieldName, fieldName)
365+
print " public void set%s(%s %s) { this.%s = %s; }" % (capFieldName, fieldType, fieldName, fieldName, fieldName)
316366

317367
jClassName = java_class_name(c.name)
318368

319369
print
320370
print " public static class %sProperties extends com.rabbitmq.client.impl.AMQ%sProperties {" % (jClassName, jClassName)
321371
#property fields
322372
for f in c.fields:
323-
(fType, fName) = (java_field_type(spec, f.domain),java_field_name(f.name))
373+
(fType, fName) = (java_field_type(spec, f.domain), java_field_name(f.name))
324374
if fType in java_scalar_types:
325375
print " private boolean %sIsSet = false;" % (fName)
326376
print " private %s %s;" % (fType, fName)
327377

328378
#explicit constructor
329379
if c.fields:
330380
print
331-
consParmList = [ "%s %s" % (java_boxed_type(java_field_type(spec,f.domain)),java_field_name(f.name))
381+
consParmList = [ "%s %s" % (java_boxed_type(java_field_type(spec,f.domain)), java_field_name(f.name))
332382
for f in c.fields ]
333383
print " public %sProperties(" % (jClassName)
334384
print " %s)" % (",\n ".join(consParmList))
335385
print " {"
336386
for f in c.fields:
337-
(fType, fName) = (java_field_type(spec, f.domain),java_field_name(f.name))
387+
(fType, fName) = (java_field_type(spec, f.domain), java_field_name(f.name))
338388
if fType in java_scalar_types:
339-
print " if (%s == null) { this.%sIsSet = false; }" % (fName, fName)
389+
print " if (%s == null) { this.%sIsSet = false; this.%s = %s; }" % (fName, fName, fName, java_scalar_default(fType))
340390
print " else { this.%sIsSet = true; this.%s = %s; }" % (fName, fName, fName)
341391
else:
342-
print " this.%s = %s;" % (fName, fName)
392+
if fType == "Map<String,Object>":
393+
print " this.%s = %s==null ? null : Collections.unmodifiableMap(new HashMap<String,Object>(%s));" % (fName, fName, fName)
394+
else:
395+
print " this.%s = %s;" % (fName, fName)
343396
print " }"
344397

345-
#default constructor
398+
#datainputstream constructor
346399
print
347-
print " public %sProperties() {}" % (jClassName)
400+
print " public %sProperties(DataInputStream in) throws IOException {" % (jClassName)
401+
print " super(in);"
402+
print " ContentHeaderPropertyReader reader = new ContentHeaderPropertyReader(in);"
403+
404+
printReadProperties(c)
405+
406+
print " }"
348407

349408
#class properties
350409
print " public int getClassId() { return %i; }" % (c.index)
@@ -355,7 +414,6 @@ def printGetterAndSetter(fieldType, fieldName):
355414
for f in c.fields:
356415
printGetterAndSetter(java_field_type(spec, f.domain), java_field_name(f.name))
357416

358-
printReadPropertiesFrom(c)
359417
printWritePropertiesTo(c)
360418
printAppendArgumentDebugStringTo(c)
361419
printPropertiesBuilderClass(c)
@@ -387,6 +445,8 @@ def printHeader():
387445
print
388446
print "import java.io.IOException;"
389447
print "import java.io.DataInputStream;"
448+
print "import java.util.Collections;"
449+
print "import java.util.HashMap;"
390450
print "import java.util.Map;"
391451
print
392452
print "import com.rabbitmq.client.AMQP;"
@@ -413,11 +473,16 @@ def constructors():
413473
fieldsToNullCheckInCons = nullCheckedFields(spec, m)
414474

415475
for f in fieldsToNullCheckInCons:
416-
print " if(%s == null)" % (f)
476+
print " if (%s == null)" % (f)
417477
print " throw new IllegalStateException(\"Invalid configuration: '%s' must be non-null.\");" % (f)
418478

419479
for a in m.arguments:
420-
print " this.%s = %s;" % (java_field_name(a.name), java_field_name(a.name))
480+
(jfType, jfName) = (java_field_type(spec, a.domain), java_field_name(a.name))
481+
if jfType == "Map<String,Object>":
482+
print " this.%s = %s==null ? null : Collections.unmodifiableMap(new HashMap<String,Object>(%s));" % (jfName, jfName, jfName)
483+
else:
484+
print " this.%s = %s;" % (jfName, jfName)
485+
421486
print " }"
422487

423488
consArgs = [ "rdr.read%s()" % (java_class_name(spec.resolveDomain(a.domain))) for a in m.arguments ]
@@ -525,11 +590,11 @@ def printContentHeaderReader():
525590
print " switch (classId) {"
526591
for c in spec.allClasses():
527592
if c.fields:
528-
print " case %s: return new %sProperties();" %(c.index, (java_class_name(c.name)))
593+
print " case %s: return new %sProperties(in);" %(c.index, (java_class_name(c.name)))
529594
print " default: break;"
530595
print " }"
531596
print
532-
print " throw new UnknownClassOrMethodId(classId, -1);"
597+
print " throw new UnknownClassOrMethodId(classId);"
533598
print " }"
534599

535600
printHeader()

src/com/rabbitmq/client/BasicProperties.java

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -100,82 +100,5 @@ public interface BasicProperties {
100100
*/
101101
public abstract String getAppId();
102102

103-
/**
104-
* Set the contentType field, or null indicating the field is not set
105-
* @param contentType the value to set the field to
106-
*/
107-
public abstract void setContentType(String contentType);
108-
109-
/**
110-
* Set the contentEncoding field, or null indicating the field is not set
111-
* @param contentEncoding - the value to set the field to
112-
*/
113-
public abstract void setContentEncoding(String contentEncoding);
114-
115-
/**
116-
* Set the headers table, or null indicating the field is not set
117-
* @param headers a map of table field names and values
118-
*/
119-
public abstract void setHeaders(Map<String, Object> headers);
120-
121-
/**
122-
* Set the deliveryMode field, or null indicating the field is not set
123-
* @param deliveryMode the value to set the field to
124-
*/
125-
public abstract void setDeliveryMode(int deliveryMode);
126-
127-
/**
128-
* Set the priority field, or null indicating the field is not set
129-
* @param priority the value to set the field to
130-
*/
131-
public abstract void setPriority(int priority);
132-
133-
/**
134-
* Set the correlationId field, or null indicating the field is not set
135-
* @param correlationId the value to set the field to
136-
*/
137-
public abstract void setCorrelationId(String correlationId);
138-
139-
/**
140-
* Set the replyTo field, or null indicating the field is not set
141-
* @param replyTo the value to set the field to
142-
*/
143-
public abstract void setReplyTo(String replyTo);
144-
145-
/**
146-
* Set the expiration field, or null indicating the field is not set
147-
* @param expiration the value to set the field to
148-
*/
149-
public abstract void setExpiration(String expiration);
150-
151-
/**
152-
* Set the messageId field, or null indicating the field is not set
153-
* @param messageId the value to set the field to
154-
*/
155-
public abstract void setMessageId(String messageId);
156-
157-
/**
158-
* Set the timestamp field, or null indicating the field is not set
159-
* @param timestamp the value to set the field to
160-
*/
161-
public abstract void setTimestamp(Date timestamp);
162-
163-
/**
164-
* Set the type field, or null indicating the field is not set
165-
* @param type the value to set the field to
166-
*/
167-
public abstract void setType(String type);
168-
169-
/**
170-
* Set the userId field, or null indicating the field is not set
171-
* @param userId the value to set the field to
172-
*/
173-
public abstract void setUserId(String userId);
174-
175-
/**
176-
* Set the appId field, or null indicating the field is not set
177-
* @param appId the value to set the field to
178-
*/
179-
public abstract void setAppId(String appId);
180-
103+
181104
}

0 commit comments

Comments
 (0)