package org.apache.directmemory.serialization;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Iterator;

import static java.util.ServiceLoader.load;

public final class SerializerFactory
{

    public static Serializer createNewSerializer()
    {
        Iterator<Serializer> serializers = load( Serializer.class ).iterator();

        // iterate over all found services
        while ( serializers.hasNext() )
        {
            // try getting the current service and return
            try
            {
                return serializers.next();
            }
            catch ( Throwable t )
            {
                // just ignore, skip and try getting the next
            }
        }

        return new StandardSerializer();
    }

    public static <S extends Serializer> S createNewSerializer( Class<S> serializer )
    {
        Iterator<Serializer> serializers = load( Serializer.class ).iterator();

        // iterate over all found services
        while ( serializers.hasNext() )
        {
            // try getting the current service and return
            try
            {
                Serializer next = serializers.next();
                if ( next.getClass().getName().equals( serializer.getName() ) )
                {
                    return serializer.cast( next );
                }
            }
            catch ( Throwable t )
            {
                // just ignore, skip and try getting the next
            }
        }

        return null;
    }

    public static Serializer createNewSerializer( String serializerClassName )
    {
        Class<?> serializerClass;
        try
        {
            serializerClass = Class.forName( serializerClassName );
        }
        catch ( ClassNotFoundException e )
        {
            return null;
        }

        if ( serializerClass.isAssignableFrom( Serializer.class ) )
        {
            Iterator<Serializer> serializers = load( Serializer.class ).iterator();

            // iterate over all found services
            while ( serializers.hasNext() )
            {
                // try getting the current service and return
                try
                {
                    Serializer next = serializers.next();
                    if ( next.getClass().getName().equals( serializerClassName ) )
                    {
                        return next;
                    }
                }
                catch ( Throwable t )
                {
                    // just ignore, skip and try getting the next
                }
            }
        }

        return null;
    }

    /**
     * Hidden constructor, this class cannot be instantiated
     */
    private SerializerFactory()
    {
        // do nothing
    }

}

@@ -1955,62 +1955,35 @@
 f ( 
-next.getClass().getName().equals( serializer.getName()
+serializer.isInstance( next
  ) )
@@ -2325,32 +2325,250 @@
 assName )%0A    %7B%0A
+        return createNewSerializer( serializerClassName, SerializerFactory.class.getClassLoader() );%0A    %7D%0A%0A    public static Serializer createNewSerializer( String serializerClassName, ClassLoader classLoader )%0A    %7B%0A
         Class%3C?%3E
@@ -2568,17 +2568,21 @@
 lass%3C?%3E 
-s
+anonS
 erialize
@@ -2623,17 +2623,21 @@
         
-s
+anonS
 erialize
@@ -2649,21 +2649,29 @@
 s = 
-C
+c
 lass
-.forName
+Loader.loadClass
 ( se
@@ -2795,33 +2795,37 @@
 %7D%0A%0A        if ( 
-s
+anonS
 erializerClass.i
@@ -2888,305 +2888,100 @@
     
-Iterator%3CSerializer%3E serializers = load( Serializer.class ).iterator();%0A%0A            // iterate over all found services%0A            while ( serializers.hasNext() )%0A            %7B%0A                // try getting the current service and return%0A                try%0A                %7B%0A                   
+@SuppressWarnings( %22unchecked%22 ) // the assignment is guarded by%0A            Class%3C? extends
  Ser
@@ -2979,39 +2979,33 @@
 tends Serializer
- next =
+%3E
  serializers.nex
@@ -3003,353 +3003,126 @@
 izer
-s.next();%0A                    if ( next.getClass().getName().equals( serializerClassName ) )%0A                    %7B%0A                        return next;%0A                    %7D%0A                %7D%0A                catch ( Throwable t )%0A                %7B%0A                    // just ignore, skip and try getting the next%0A                %7D%0A            %7D
+Class = (Class%3C? extends Serializer%3E) anonSerializerClass;%0A%0A            return createNewSerializer( serializerClass );
 %0A   

check class assignment using java.util.Class APIs Class.forName() dropped in favor of ClassLoader.loadClass() DRY calling existing method