Skip to content

Commit

Permalink
MessageBatch$Visitor -> BiFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Mar 7, 2016
1 parent fac0d4c commit 08ada25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/org/jgroups/protocols/ENCRYPT.java
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;


/** /**
* ENCRYPT layer. Encrypt and decrypt communication in JGroups * ENCRYPT layer. Encrypt and decrypt communication in JGroups
Expand Down Expand Up @@ -920,11 +921,11 @@ private PublicKey generatePubKey(byte[] encodedKey) {




/** Decrypts all messages in a batch, replacing encrypted messages in-place with their decrypted versions */ /** Decrypts all messages in a batch, replacing encrypted messages in-place with their decrypted versions */
protected class Decrypter implements MessageBatch.Visitor<Message> { protected class Decrypter implements BiFunction<Message,MessageBatch,Message> {
protected Lock lock; protected Lock lock;
protected Cipher cipher; protected Cipher cipher;


public Message visit(Message msg, MessageBatch batch) { public Message apply(Message msg, MessageBatch batch) {
EncryptHeader hdr; EncryptHeader hdr;


if(msg == null || (msg.getLength() == 0 && !encrypt_entire_message) || ((hdr=(EncryptHeader)msg.getHeader(id)) == null)) if(msg == null || (msg.getLength() == 0 && !encrypt_entire_message) || ((hdr=(EncryptHeader)msg.getHeader(id)) == null))
Expand Down
24 changes: 7 additions & 17 deletions src/org/jgroups/util/MessageBatch.java
Expand Up @@ -4,6 +4,7 @@
import org.jgroups.Message; import org.jgroups.Message;


import java.util.*; import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -37,8 +38,8 @@ public class MessageBatch implements Iterable<Message> {
protected Mode mode=Mode.REG; protected Mode mode=Mode.REG;


protected static final int INCR=5; // number of elements to add when resizing protected static final int INCR=5; // number of elements to add when resizing
protected static final Visitor<Integer> length_visitor=(msg, batch) -> msg != null? msg.getLength() : 0; protected static final BiFunction<Message,MessageBatch,Integer> length_visitor=(msg, batch) -> msg != null? msg.getLength() : 0;
protected static final Visitor<Long> total_size_visitor=(msg, batch) -> msg != null? msg.size() : 0; protected static final BiFunction<Message,MessageBatch,Long> total_size_visitor=(msg, batch) -> msg != null? msg.size() : 0;




public MessageBatch(int capacity) { public MessageBatch(int capacity) {
Expand Down Expand Up @@ -197,11 +198,11 @@ public Collection<Message> getMatchingMessages(final short id, boolean remove) {




/** Applies a function to all messages and returns a list of the function results */ /** Applies a function to all messages and returns a list of the function results */
public <T> Collection<T> map(Visitor<T> visitor) { public <T> Collection<T> map(BiFunction<Message,MessageBatch,T> visitor) {
Collection<T> retval=null; Collection<T> retval=null;
for(int i=0; i < index; i++) { for(int i=0; i < index; i++) {
try { try {
T result=visitor.visit(messages[i], this); T result=visitor.apply(messages[i], this);
if(result != null) { if(result != null) {
if(retval == null) if(retval == null)
retval=new ArrayList<>(); retval=new ArrayList<>();
Expand Down Expand Up @@ -257,15 +258,15 @@ else if(messages[i].isFlagSet(Message.Flag.INTERNAL))
public long totalSize() { public long totalSize() {
long retval=0; long retval=0;
for(int i=0; i < index; i++) for(int i=0; i < index; i++)
retval+=total_size_visitor.visit(messages[i], this); retval+=total_size_visitor.apply(messages[i], this);
return retval; return retval;
} }


/** Returns the total number of bytes of the message batch (by calling {@link org.jgroups.Message#getLength()} on all messages) */ /** Returns the total number of bytes of the message batch (by calling {@link org.jgroups.Message#getLength()} on all messages) */
public int length() { public int length() {
int retval=0; int retval=0;
for(int i=0; i < index; i++) for(int i=0; i < index; i++)
retval+=length_visitor.visit(messages[i], this); retval+=length_visitor.apply(messages[i], this);
return retval; return retval;
} }


Expand Down Expand Up @@ -314,17 +315,6 @@ protected void resize() {
} }




/** Used for iteration over the messages */
@FunctionalInterface
public interface Visitor<T> {
/**
* Called when iterating over the message batch
* @param msg The message, can be null
* @param batch
* @return
*/
T visit(final Message msg, final MessageBatch batch);
}


public enum Mode {OOB, REG, INTERNAL, MIXED} public enum Mode {OOB, REG, INTERNAL, MIXED}


Expand Down
Expand Up @@ -15,6 +15,7 @@
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.util.*; import java.util.*;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;


Expand All @@ -32,7 +33,7 @@ public class MessageBatchTest {
UDP_ID=ClassConfigurator.getProtocolId(UDP.class); UDP_ID=ClassConfigurator.getProtocolId(UDP.class);
protected final Address a=Util.createRandomAddress("A"), b=Util.createRandomAddress("B"); protected final Address a=Util.createRandomAddress("A"), b=Util.createRandomAddress("B");


protected static final MessageBatch.Visitor<Integer> print_numbers=(msg, batch) -> msg != null? (Integer)msg.getObject() : null; protected static final BiFunction<Message,MessageBatch,Integer> print_numbers=(msg, batch) -> msg != null? (Integer)msg.getObject() : null;






Expand Down

0 comments on commit 08ada25

Please sign in to comment.