Skip to content

Commit 61184bf

Browse files
committed
make PackerConfg and UnpackerConfig immutable to make
1 parent e34df59 commit 61184bf

File tree

5 files changed

+108
-37
lines changed

5 files changed

+108
-37
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessagePack.java

Lines changed: 104 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,42 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, in
234234
* MessagePacker configuration.
235235
*/
236236
public static class PackerConfig
237+
implements Cloneable
237238
{
238239
private int smallStringOptimizationThreshold = 512;
239240

240241
private int bufferFlushThreshold = 8192;
241242

242243
private int bufferSize = 8192;
243244

245+
public PackerConfig()
246+
{ }
247+
248+
private PackerConfig(PackerConfig copy)
249+
{
250+
this.smallStringOptimizationThreshold = smallStringOptimizationThreshold;
251+
this.bufferFlushThreshold = bufferFlushThreshold;
252+
this.bufferSize = bufferSize;
253+
}
254+
255+
@Override
256+
public PackerConfig clone()
257+
{
258+
return new PackerConfig(this);
259+
}
260+
261+
@Override
262+
public boolean equals(Object obj)
263+
{
264+
if (!(obj instanceof PackerConfig)) {
265+
return false;
266+
}
267+
PackerConfig o = (PackerConfig) obj;
268+
return this.smallStringOptimizationThreshold == o.smallStringOptimizationThreshold
269+
&& this.bufferFlushThreshold == o.bufferFlushThreshold
270+
&& this.bufferSize == o.bufferSize;
271+
}
272+
244273
/**
245274
* Create a packer that outputs the packed data to a given output
246275
*
@@ -288,10 +317,11 @@ public MessageBufferPacker newBufferPacker()
288317
* Use String.getBytes() for converting Java Strings that are smaller than this threshold into UTF8.
289318
* Note that this parameter is subject to change.
290319
*/
291-
public PackerConfig setSmallStringOptimizationThreshold(int bytes)
320+
public PackerConfig withSmallStringOptimizationThreshold(int bytes)
292321
{
293-
this.smallStringOptimizationThreshold = bytes;
294-
return this;
322+
PackerConfig copy = clone();
323+
copy.smallStringOptimizationThreshold = bytes;
324+
return copy;
295325
}
296326

297327
public int getSmallStringOptimizationThreshold()
@@ -303,10 +333,11 @@ public int getSmallStringOptimizationThreshold()
303333
* When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before
304334
* packing the data (default: 8192).
305335
*/
306-
public PackerConfig setBufferFlushThreshold(int bytes)
336+
public PackerConfig withBufferFlushThreshold(int bytes)
307337
{
308-
this.bufferFlushThreshold = bytes;
309-
return this;
338+
PackerConfig copy = clone();
339+
copy.bufferFlushThreshold = bytes;
340+
return copy;
310341
}
311342

312343
public int getBufferFlushThreshold()
@@ -318,10 +349,11 @@ public int getBufferFlushThreshold()
318349
* When a packer is created with newPacker(OutputStream) or newPacker(WritableByteChannel), the stream will be
319350
* buffered with this size of buffer (default: 8192).
320351
*/
321-
public PackerConfig setBufferSize(int bytes)
352+
public PackerConfig withBufferSize(int bytes)
322353
{
323-
this.bufferSize = bytes;
324-
return this;
354+
PackerConfig copy = clone();
355+
copy.bufferSize = bytes;
356+
return copy;
325357
}
326358

327359
public int getBufferSize()
@@ -334,6 +366,7 @@ public int getBufferSize()
334366
* MessageUnpacker configuration.
335367
*/
336368
public static class UnpackerConfig
369+
implements Cloneable
337370
{
338371
private boolean allowReadingStringAsBinary = true;
339372

@@ -352,6 +385,40 @@ public static class UnpackerConfig
352385
*/
353386
private int stringDecoderBufferSize = 8192;
354387

388+
public UnpackerConfig()
389+
{ }
390+
391+
private UnpackerConfig(UnpackerConfig copy)
392+
{
393+
this.allowReadingStringAsBinary = copy.allowReadingStringAsBinary;
394+
this.allowReadingBinaryAsString = copy.allowReadingBinaryAsString;
395+
this.actionOnMalformedString = copy.actionOnMalformedString;
396+
this.actionOnUnmappableString = copy.actionOnUnmappableString;
397+
this.stringSizeLimit = copy.stringSizeLimit;
398+
this.bufferSize = copy.bufferSize;
399+
}
400+
401+
@Override
402+
public UnpackerConfig clone()
403+
{
404+
return new UnpackerConfig(this);
405+
}
406+
407+
@Override
408+
public boolean equals(Object obj)
409+
{
410+
if (!(obj instanceof UnpackerConfig)) {
411+
return false;
412+
}
413+
UnpackerConfig o = (UnpackerConfig) obj;
414+
return this.allowReadingStringAsBinary == o.allowReadingStringAsBinary
415+
&& this.allowReadingBinaryAsString == o.allowReadingBinaryAsString
416+
&& this.actionOnMalformedString == o.actionOnMalformedString
417+
&& this.actionOnUnmappableString == o.actionOnUnmappableString
418+
&& this.stringSizeLimit == o.stringSizeLimit
419+
&& this.bufferSize == o.bufferSize;
420+
}
421+
355422
/**
356423
* Create an unpacker that reads the data from a given input
357424
*
@@ -410,10 +477,11 @@ public MessageUnpacker newUnpacker(byte[] contents, int offset, int length)
410477
/**
411478
* Allow unpackBinaryHeader to read str format family (default: true)
412479
*/
413-
public UnpackerConfig setAllowReadingStringAsBinary(boolean enable)
480+
public UnpackerConfig withAllowReadingStringAsBinary(boolean enable)
414481
{
415-
this.allowReadingStringAsBinary = enable;
416-
return this;
482+
UnpackerConfig copy = clone();
483+
copy.allowReadingStringAsBinary = enable;
484+
return copy;
417485
}
418486

419487
public boolean getAllowReadingStringAsBinary()
@@ -424,10 +492,11 @@ public boolean getAllowReadingStringAsBinary()
424492
/**
425493
* Allow unpackString and unpackRawStringHeader and unpackString to read bin format family (default: true)
426494
*/
427-
public UnpackerConfig setAllowReadingBinaryAsString(boolean enable)
495+
public UnpackerConfig withAllowReadingBinaryAsString(boolean enable)
428496
{
429-
this.allowReadingBinaryAsString = enable;
430-
return this;
497+
UnpackerConfig copy = clone();
498+
copy.allowReadingBinaryAsString = enable;
499+
return copy;
431500
}
432501

433502
public boolean getAllowReadingBinaryAsString()
@@ -438,10 +507,11 @@ public boolean getAllowReadingBinaryAsString()
438507
/**
439508
* Action when encountered a malformed input (default: REPLACE)
440509
*/
441-
public UnpackerConfig setActionOnMalformedString(CodingErrorAction action)
510+
public UnpackerConfig withActionOnMalformedString(CodingErrorAction action)
442511
{
443-
this.actionOnMalformedString = action;
444-
return this;
512+
UnpackerConfig copy = clone();
513+
copy.actionOnMalformedString = action;
514+
return copy;
445515
}
446516

447517
public CodingErrorAction getActionOnMalformedString()
@@ -452,10 +522,11 @@ public CodingErrorAction getActionOnMalformedString()
452522
/**
453523
* Action when an unmappable character is found (default: REPLACE)
454524
*/
455-
public UnpackerConfig setActionOnUnmappableString(CodingErrorAction action)
525+
public UnpackerConfig withActionOnUnmappableString(CodingErrorAction action)
456526
{
457-
this.actionOnUnmappableString = action;
458-
return this;
527+
UnpackerConfig copy = clone();
528+
copy.actionOnUnmappableString = action;
529+
return copy;
459530
}
460531

461532
public CodingErrorAction getActionOnUnmappableString()
@@ -466,10 +537,11 @@ public CodingErrorAction getActionOnUnmappableString()
466537
/**
467538
* unpackString size limit (default: Integer.MAX_VALUE).
468539
*/
469-
public UnpackerConfig setStringSizeLimit(int bytes)
540+
public UnpackerConfig withStringSizeLimit(int bytes)
470541
{
471-
this.stringSizeLimit = bytes;
472-
return this;
542+
UnpackerConfig copy = clone();
543+
copy.stringSizeLimit = bytes;
544+
return copy;
473545
}
474546

475547
public int getStringSizeLimit()
@@ -480,10 +552,11 @@ public int getStringSizeLimit()
480552
/**
481553
*
482554
*/
483-
public UnpackerConfig setStringDecoderBufferSize(int bytes)
555+
public UnpackerConfig withStringDecoderBufferSize(int bytes)
484556
{
485-
this.stringDecoderBufferSize = bytes;
486-
return this;
557+
UnpackerConfig copy = clone();
558+
copy.stringDecoderBufferSize = bytes;
559+
return copy;
487560
}
488561

489562
public int getStringDecoderBufferSize()
@@ -495,10 +568,11 @@ public int getStringDecoderBufferSize()
495568
* When a packer is created with newUnpacker(OutputStream) or newUnpacker(WritableByteChannel), the stream will be
496569
* buffered with this size of buffer (default: 8192).
497570
*/
498-
public UnpackerConfig setBufferSize(int bytes)
571+
public UnpackerConfig withBufferSize(int bytes)
499572
{
500-
this.bufferSize = bytes;
501-
return this;
573+
UnpackerConfig copy = clone();
574+
copy.bufferSize = bytes;
575+
return copy;
502576
}
503577

504578
public int getBufferSize()

msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ public class MessagePacker
113113
public MessagePacker(MessageBufferOutput out, MessagePack.PackerConfig config)
114114
{
115115
this.out = checkNotNull(out, "MessageBufferOutput is null");
116-
// We must copy the configuration parameters here since the config object is mutable
117116
this.smallStringOptimizationThreshold = config.getSmallStringOptimizationThreshold();
118117
this.bufferFlushThreshold = config.getBufferFlushThreshold();
119118
this.position = 0;

msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public class MessageUnpacker
129129
public MessageUnpacker(MessageBufferInput in, MessagePack.UnpackerConfig config)
130130
{
131131
this.in = checkNotNull(in, "MessageBufferInput is null");
132-
// We need to copy the configuration parameters since the config object is mutable
133132
this.allowReadingStringAsBinary = config.getAllowReadingStringAsBinary();
134133
this.allowReadingBinaryAsString = config.getAllowReadingBinaryAsString();
135134
this.actionOnMalformedString = config.getActionOnMalformedString();

msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static void configuration()
247247
{
248248
ByteArrayOutputStream out = new ByteArrayOutputStream();
249249
MessagePacker packer = new PackerConfig()
250-
.setSmallStringOptimizationThreshold(256) // String
250+
.withSmallStringOptimizationThreshold(256) // String
251251
.newPacker(out);
252252

253253
packer.packInt(10);
@@ -257,7 +257,7 @@ public static void configuration()
257257
// Unpack data
258258
byte[] packedData = out.toByteArray();
259259
MessageUnpacker unpacker = new UnpackerConfig()
260-
.setStringDecoderBufferSize(16 * 1024) // If your data contains many large strings (the default is 8k)
260+
.withStringDecoderBufferSize(16 * 1024) // If your data contains many large strings (the default is 8k)
261261
.newUnpacker(packedData);
262262
int i = unpacker.unpackInt(); // 10
263263
boolean b = unpacker.unpackBoolean(); // true

msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,8 @@ class MessagePackTest extends MessagePackSpec {
337337

338338
// Report error on unmappable character
339339
val unpackerConfig = new UnpackerConfig()
340-
unpackerConfig
341-
.setActionOnMalformedString(CodingErrorAction.REPORT)
342-
.setActionOnUnmappableString(CodingErrorAction.REPORT)
340+
.withActionOnMalformedString(CodingErrorAction.REPORT)
341+
.withActionOnUnmappableString(CodingErrorAction.REPORT)
343342

344343
for (bytes <- Seq(unmappable)) {
345344
When("unpacking")

0 commit comments

Comments
 (0)