/*
 * 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;

import static org.wildfly.clustering.marshalling.protostream.MarshallerProvider.ClassPredicate.ABSTRACT;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import org.infinispan.protostream.SerializationContext;

/**
 * A {@link org.infinispan.protostream.SerializationContextInitializer} that registers enumerated marshallers.
 * @author Paul Ferraro
 * @param <E> the marshaller provider provider type
 */
public class ProviderSerializationContextInitializer<E extends Enum<E> & ProtoStreamMarshallerProvider> extends AbstractSerializationContextInitializer {

    private final Class<E> providerClass;

    public ProviderSerializationContextInitializer(String resourceName, Class<E> providerClass) {
        super(resourceName);
        this.providerClass = providerClass;
    }

    @Override
    public void registerMarshallers(SerializationContext context) {
        Set<E> providers = EnumSet.allOf(this.providerClass);
        List<ProtoStreamMarshaller<?>> abstractMarshallers = new ArrayList<>(providers.size());
        for (E provider : providers) {
            ProtoStreamMarshaller<?> marshaller = provider.getMarshaller();
            // If marshaller type is abstract, register via a MarshallerProvider instead
            if (ABSTRACT.test(marshaller.getJavaClass())) {
                abstractMarshallers.add(marshaller);
            } else {
                context.registerMarshaller(marshaller);
            }
        }
        if (!abstractMarshallers.isEmpty()) {
            context.registerMarshallerProvider(new MarshallerProvider(ABSTRACT, abstractMarshallers));
        }
    }
}

@@ -1705,37 +1705,53 @@
 e final 
-Class%3CE%3E p
+EnumSet%3CE%3E concreteSuperClassP
 rovider
-Clas
 s;%0A%0A    
@@ -1856,70 +1856,278 @@
     
-super(resourceName);%0A        this.providerClass = p
+this(resourceName, EnumSet.noneOf(providerClass));%0A    %7D%0A%0A    public ProviderSerializationContextInitializer(String resourceName, EnumSet%3CE%3E concreteSuperClassProviders) %7B%0A        super(resourceName);%0A        this.concreteSuperClassProviders = concreteSuperClassP
 rovider
-Clas
 s;%0A 
@@ -2253,31 +2253,52 @@
 Set.
-allOf(this.p
+complementOf(this.concreteSuperClassP
 rovider
-Clas
 s);%0A
@@ -2966,20 +2966,394 @@
 ;%0A        %7D%0A
+        // If marshaller represents a non-abstract superclass, register as a marshaller provider%0A        for (E provider : this.concreteSuperClassProviders) %7B%0A            ProtoStreamMarshaller%3C?%3E marshaller = provider.getMarshaller();%0A            context.registerMarshallerProvider(new MarshallerProvider(marshaller.getJavaClass()::isAssignableFrom, marshaller));%0A        %7D%0A
     %7D%0A%7D%0A

Add support for concrete superclass marshaller registration.