/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2020, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.clustering.marshalling.protostream.util;

import java.io.IOException;
import java.util.Collection;
import java.util.OptionalInt;
import java.util.function.Function;

import org.infinispan.protostream.ImmutableSerializationContext;
import org.infinispan.protostream.RawProtoStreamReader;
import org.infinispan.protostream.RawProtoStreamWriter;
import org.wildfly.clustering.marshalling.protostream.ObjectMarshaller;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller;

/**
 * @author Paul Ferraro
 */
public class SingletonCollectionMarshaller<T extends Collection<Object>> implements ProtoStreamMarshaller<T> {

    private final Function<Object, T> factory;

    public SingletonCollectionMarshaller(Function<Object, T> factory) {
        this.factory = factory;
    }

    @Override
    public T readFrom(ImmutableSerializationContext context, RawProtoStreamReader reader) throws IOException {
        Object value = ObjectMarshaller.INSTANCE.readFrom(context, reader);
        return this.factory.apply(value);
    }

    @Override
    public void writeTo(ImmutableSerializationContext context, RawProtoStreamWriter writer, T collection) throws IOException {
        ObjectMarshaller.INSTANCE.writeTo(context, writer, collection.iterator().next());
    }

    @Override
    public OptionalInt size(ImmutableSerializationContext context, T collection) {
        return ObjectMarshaller.INSTANCE.size(context, collection.iterator().next());
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<? extends T> getJavaClass() {
        return (Class<T>) this.factory.apply(null).getClass();
    }
}

@@ -1083,36 +1083,8 @@
 l;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -1112,38 +1112,8 @@
 on;%0A
-import java.util.OptionalInt;%0A
 impo
@@ -1160,311 +1160,67 @@
 org.
-infinispan.protostream.ImmutableSerializationContext;%0Aimport org.infinispan.protostream.RawProtoStreamReader;%0Aimport org.infinispan.protostream.RawProtoStreamWriter;%0Aimport org.wildfly.clustering.marshalling.protostream.ObjectMarshaller;%0Aimport org.wildfly.clustering.marshalling.protostream.ProtoStream
+wildfly.clustering.marshalling.protostream.FunctionalObject
 Mars
@@ -1337,30 +1337,32 @@
 t%3E%3E 
-implements ProtoStream
+extends FunctionalObject
 Mars
@@ -1382,51 +1382,38 @@
     
-private final Function%3CObject, T%3E factory;%0A
+@SuppressWarnings(%22unchecked%22)
 %0A   
@@ -1493,527 +1493,185 @@
     
-this.factory = factory;%0A    %7D%0A%0A    @Override%0A    public T readFrom(ImmutableSerializationContext context, RawProtoStreamReader reader) throws IOException %7B%0A        Object value = ObjectMarshaller.INSTANCE.readFrom(context, reader);%0A        return this.factory.apply(value);%0A    %7D%0A%0A    @Override%0A    public void writeTo(ImmutableSerializationContext context, RawProtoStreamWriter writer, T collection) throws IOException %7B%0A        ObjectMarshaller.INSTANCE.writeTo(context, writer, collection.iterator().next());
+super((Class%3CT%3E) factory.apply(null).getClass(), factory, new Accessor%3C%3E());%0A    %7D%0A%0A    static class Accessor%3CT extends Collection%3CObject%3E%3E implements Function%3CT, Object%3E %7B
 %0A    
-%7D%0A%0A
     
@@ -1688,16 +1688,20 @@
     
+    
 public O
 ptio
@@ -1700,63 +1700,20 @@
 ic O
-ptionalInt size(ImmutableSerializationContext context, 
+bject apply(
 T co
@@ -1720,24 +1720,28 @@
 llection) %7B%0A
+    
         retu
@@ -1746,48 +1746,8 @@
 turn
- ObjectMarshaller.INSTANCE.size(context,
  col
@@ -1775,176 +1775,19 @@
 xt()
-)
 ;%0A    
-%7D%0A%0A
     
-@SuppressWarnings(%22unchecked%22)%0A    @Override%0A    public Class%3C? extends T%3E getJavaClass() %7B%0A        return (Class%3CT%3E) this.factory.apply(null).getClass();
+%7D
 %0A   

Reimplement using existing object wrapper marshaller.