1212 * @since 5.0
1313 */
1414public class MessageFactory {
15- protected static final byte MIN_TYPE =32 ;
16- protected static final Supplier <? extends Message >[] creators =new Supplier [MIN_TYPE ];
17- protected static Map <Short ,Supplier <? extends Message >> map =new HashMap <>();
18- static {
19- creators [Message .BYTES_MSG ]=BytesMessage ::new ;
20- creators [Message .NIO_MSG ]=NioMessage ::new ;
21- creators [Message .EMPTY_MSG ]=EmptyMessage ::new ;
22- creators [Message .OBJ_MSG ]=ObjectMessage ::new ;
23- creators [Message .LONG_MSG ]=LongMessage ::new ;
24- creators [Message .COMPOSITE_MSG ]=CompositeMessage ::new ;
25- creators [Message .FRAG_MSG ]=FragmentedMessage ::new ;
26- creators [Message .EARLYBATCH_MSG ]=BatchMessage ::new ;
15+ protected static final byte MIN_TYPE =32 ;
16+ protected final Supplier <? extends Message >[] creators =new Supplier [MIN_TYPE ];
17+ protected final Map <Short ,Supplier <? extends Message >> map =new HashMap <>();
18+
19+ private static volatile MessageFactory singleton =null ;
20+
21+ public static byte minType () {return MIN_TYPE ;}
22+
23+ public synchronized static MessageFactory get () {
24+ MessageFactory mf =singleton ;
25+ if (mf != null )
26+ return mf ;
27+ if (singleton == null )
28+ singleton =new MessageFactory ().registerDefaultTypes ();
29+ return singleton ;
30+ }
31+
32+ public MessageFactory registerDefaultTypes () {
33+ registerDefaultMessage (Message .BYTES_MSG , BytesMessage ::new );
34+ registerDefaultMessage (Message .NIO_MSG , NioMessage ::new );
35+ registerDefaultMessage (Message .EMPTY_MSG , EmptyMessage ::new );
36+ registerDefaultMessage (Message .OBJ_MSG , ObjectMessage ::new );
37+ registerDefaultMessage (Message .LONG_MSG , LongMessage ::new );
38+ registerDefaultMessage (Message .COMPOSITE_MSG , CompositeMessage ::new );
39+ registerDefaultMessage (Message .FRAG_MSG , FragmentedMessage ::new );
40+ registerDefaultMessage (Message .EARLYBATCH_MSG , BatchMessage ::new );
41+ return this ;
2742 }
2843
2944 /**
@@ -32,25 +47,40 @@ public class MessageFactory {
3247 * @param <T> The type of the message
3348 * @return A message
3449 */
35- public static <T extends Message > T create (short type ) {
50+ public <T extends Message > T create (short type ) {
3651 Supplier <? extends Message > creator =type < MIN_TYPE ? creators [type ] : map .get (type );
3752 if (creator == null )
3853 throw new IllegalArgumentException ("no creator found for type " + type );
3954 return (T )creator .get ();
4055 }
4156
57+ public <T extends Message > T createIfExists (short type ) {
58+ Supplier <? extends Message > creator =type < MIN_TYPE ? creators [type ] : map .get (type );
59+ return creator == null ? null : (T )creator .get ();
60+ }
61+
62+ public void registerDefaultMessage (short type , Supplier <? extends Message > generator ) {
63+ Objects .requireNonNull (generator , "the creator must be non-null" );
64+ if (type > MIN_TYPE )
65+ throw new IllegalArgumentException (String .format ("type (%d) must be <= 32" , type ));
66+ if (creators [type ] != null )
67+ throw new IllegalArgumentException (String .format ("type %d is already taken" , type ));
68+ creators [type ]=generator ;
69+ }
70+
4271 /**
4372 * Registers a new creator of messages
4473 * @param type The type associated with the new payload. Needs to be the same in all nodes of the same cluster, and
4574 * needs to be available (ie., not taken by JGroups or other applications).
4675 * @param generator The creator of the payload associated with the given type
4776 */
48- public static void register (short type , Supplier <? extends Message > generator ) {
77+ public void register (short type , Supplier <? extends Message > generator ) {
4978 Objects .requireNonNull (generator , "the creator must be non-null" );
5079 if (type < MIN_TYPE )
5180 throw new IllegalArgumentException (String .format ("type (%d) must be >= 32" , type ));
5281 if (map .containsKey (type ))
5382 throw new IllegalArgumentException (String .format ("type %d is already taken" , type ));
5483 map .put (type , generator );
5584 }
85+
5686}
0 commit comments