package com.kryptnostic.rhizome.hazelcast.serializers;

import java.io.IOException;
import java.util.Set;

import jersey.repackaged.com.google.common.collect.Sets;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;

public class SetStreamSerializers {
    
    public static <T> void serialize( ObjectDataOutput out , Set<T> elements, IoPerformingConsumer<T> c ) throws IOException {
        out.writeInt( elements.size() );
        for( T elem :  elements ) {
            c.accept( elem );
        }
    }

    public static <T> Set<T> deserialize( ObjectDataInput in, IoPerformingFunction<ObjectDataInput, T> f ) throws IOException {
        int size = in.readInt();
        Set<T> s = Sets.newHashSetWithExpectedSize( size );
        for ( int i = 0; i < size; ++i ) {
            T elem = f.apply( in );
            if ( elem != null ) {
                s.add( elem );
            }
        }
        return s;
    }
}

@@ -280,20 +280,16 @@
 izers %7B%0A
-    
 %0A    pub
@@ -705,18 +705,31 @@
     
-Set%3CT%3E s =
+return deserialize( in,
  Set
@@ -764,17 +764,170 @@
 ( size )
-;
+,  f );%0A    %7D%0A%0A    public static %3CT%3E Set%3CT%3E deserialize( ObjectDataInput in, Set%3CT%3E set, IoPerformingFunction%3CObjectDataInput, T%3E f ) throws IOException %7B
 %0A       
@@ -948,20 +948,26 @@
  0; i %3C 
+set.
 size
+()
 ; ++i ) 
@@ -1055,16 +1055,18 @@
        s
+et
 .add( el
@@ -1087,32 +1087,32 @@
     %7D%0A        %7D%0A
-
         return s
@@ -1111,16 +1111,18 @@
 return s
+et
 ;%0A    %7D%0A

allow deserialization to a custom set