Skip to content

Commit c71bf37

Browse files
committed
Add CompositeCodec documentation to reference documentation
Provide example for using CompositeCodec. Remove @see in CompositeCodec javadocs. This is handled in the links in the javadoc
1 parent d9329a6 commit c71bf37

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

spring-integration-core/src/main/java/org/springframework/integration/codec/CompositeCodec.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.OutputStream;
2323
import java.util.HashMap;
2424
import java.util.Map;
25-
import java.util.Set;
2625

2726
import org.springframework.integration.util.ClassUtils;
2827
import org.springframework.util.Assert;
@@ -43,7 +42,6 @@
4342
* @author Glenn Renfro
4443
*
4544
* @since 4.2
46-
* @see ClassUtils#findClosestMatch(Class, Set, boolean)
4745
*/
4846
public class CompositeCodec implements Codec {
4947

src/reference/antora/modules/ROOT/pages/codec.adoc

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ See the https://docs.spring.io/spring-integration/api/org/springframework/integr
3939
[[kryo]]
4040
== Kryo
4141

42-
Currently, this is the only implementation of `Codec`, and it provides two kinds of `Codec`:
42+
Currently, this is the only implementation of `Codec`, and it provides three kinds of `Codec`:
4343

4444
* `PojoCodec`: Used in the transformers
4545
* `MessageCodec`: Used in the `CodecMessageConverter`
46+
* `CompositeCodec`: Used in transformers
4647

4748
The framework provides several custom serializers:
4849

@@ -53,6 +54,69 @@ The framework provides several custom serializers:
5354
The first can be used with the `PojoCodec` by initializing it with the `FileKryoRegistrar`.
5455
The second and third are used with the `MessageCodec`, which is initialized with the `MessageKryoRegistrar`.
5556

57+
[[composite-codec]]
58+
=== CompositeCodec
59+
The CompositeCodec is a codec that combines multiple codecs into a single codec, delegating encoding and decoding operations to the appropriate type-specific codec.
60+
This implementation associates object types with their appropriate codecs while providing a fallback default codec for unregistered types.
61+
62+
An example implementation can be seen below:
63+
```java
64+
void encodeDecodeSample() {
65+
Codec codec = getFullyQualifiedCodec();
66+
67+
//Encode and Decode a Dog Object
68+
Dog dog = new Dog("Wolfy", 3, "woofwoof");
69+
dog = codec.decode(
70+
codec.encode(dog),
71+
Dog.class);
72+
System.out.println(dog);
73+
74+
//Encode and Decode a Cat Object
75+
Cat cat = new Cat("Kitty", 2, 8);
76+
cat = codec.decode(
77+
codec.encode(cat),
78+
Cat.class);
79+
System.out.println(cat);
80+
81+
//Use the default code if the type being decoded and encoded is not Cat or dog.
82+
Animal animal = new Animal("Badger", 5);
83+
Animal animalOut = codec.decode(
84+
codec.encode(animal),
85+
Animal.class);
86+
System.out.println(animalOut);
87+
}
88+
89+
/**
90+
* Create and return a {@link CompositeCodec} that associates {@code Dog} and {@code Cat}
91+
* classes with their respective {@link PojoCodec} instances, while providing a default
92+
* codec for {@code Animal} types.
93+
* <p>
94+
* @return a fully qualified {@link CompositeCodec} for {@code Dog}, {@code Cat},
95+
* and fallback for {@code Animal}
96+
*/
97+
static Codec getFullyQualifiedCodec() {
98+
Map<Class<?>, Codec> codecs = new HashMap<Class<?>, Codec>();
99+
codecs.put(Dog.class, new PojoCodec(new KryoClassListRegistrar(Dog.class)));
100+
codecs.put(Cat.class, new PojoCodec(new KryoClassListRegistrar(Cat.class)));
101+
return new CompositeCodec(codecs, new PojoCodec(
102+
new KryoClassListRegistrar(Animal.class)));
103+
}
104+
105+
// Records that will be encoded and decoded in this sample
106+
record Dog(String name, int age, String tag) {}
107+
record Cat(String name, int age, int lives) {}
108+
record Animal(String name, int age){}
109+
```
110+
111+
In some cases a single type of object may return multiple codecs. In these cases an `IllegalStateException` is thrown.
112+
113+
NOTE: This class uses `ClassUtils.findClosestMatch` to select the appropriate codec for a given object type.
114+
When multiple codecs match an object type, `ClassUtils.findClosestMatch` offers the `failOnTie` option.
115+
If `failOnTie` is `false`, it will return any one of the matching codecs.
116+
If `failOnTie` is `true` and multiple codecs match, it will throw an `IllegalStateException`.
117+
CompositeCodec` sets `failOnTie` to `true`, so if multiple codecs match, an
118+
`IllegalStateException` is thrown.
119+
56120
[[customizing-kryo]]
57121
=== Customizing Kryo
58122

0 commit comments

Comments
 (0)