-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change makes MessageFactory into a static class similar to ClassConfigurator. This greatly simplifies adding custom message types, and also allows custom types to be used in Batch/CompositeMessag which otherwise only allows the default types. This comes with the side effect that it's no longer possible to change how the default message types are generated, but this must be a much less common use case than registering custom types. And as a workaround, a new custom type can be registered instead of altering the default types.
- Loading branch information
Showing
24 changed files
with
85 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,56 @@ | ||
package org.jgroups; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Factory to create messages. Uses an array for message IDs less then 32, and a hashmap for | ||
* types above 32 | ||
* @author Bela Ban | ||
* @since 5.0 | ||
*/ | ||
public interface MessageFactory { | ||
|
||
|
||
public class MessageFactory { | ||
protected static final byte MIN_TYPE=32; | ||
protected static final Supplier<? extends Message>[] creators=new Supplier[MIN_TYPE]; | ||
protected static Map<Short,Supplier<? extends Message>> map=new HashMap<>(); | ||
static { | ||
creators[Message.BYTES_MSG]=BytesMessage::new; | ||
creators[Message.NIO_MSG]=NioMessage::new; | ||
creators[Message.EMPTY_MSG]=EmptyMessage::new; | ||
creators[Message.OBJ_MSG]=ObjectMessage::new; | ||
creators[Message.LONG_MSG]=LongMessage::new; | ||
creators[Message.COMPOSITE_MSG]=CompositeMessage::new; | ||
creators[Message.FRAG_MSG]=FragmentedMessage::new; | ||
creators[Message.EARLYBATCH_MSG]=BatchMessage::new; | ||
} | ||
|
||
/** | ||
* Creates a message based on the given ID | ||
* @param id The ID | ||
* @param <T> The type of the message | ||
* @return A message | ||
*/ | ||
<T extends Message> T create(short id); | ||
public static <T extends Message> T create(short type) { | ||
Supplier<? extends Message> creator=type < MIN_TYPE? creators[type] : map.get(type); | ||
if(creator == null) | ||
throw new IllegalArgumentException("no creator found for type " + type); | ||
return (T)creator.get(); | ||
} | ||
|
||
/** | ||
* Registers a new creator of messages | ||
* @param type The type associated with the new payload. Needs to be the same in all nodes of the same cluster, and | ||
* needs to be available (ie., not taken by JGroups or other applications). | ||
* @param generator The creator of the payload associated with the given type | ||
*/ | ||
<M extends MessageFactory> M register(short type, Supplier<? extends Message> generator); | ||
public static void register(short type, Supplier<? extends Message> generator) { | ||
Objects.requireNonNull(generator, "the creator must be non-null"); | ||
if(type < MIN_TYPE) | ||
throw new IllegalArgumentException(String.format("type (%d) must be >= 32", type)); | ||
if(map.containsKey(type)) | ||
throw new IllegalArgumentException(String.format("type %d is already taken", type)); | ||
map.put(type, generator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.