All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgrapht.util.SupplierUtil Maven / Gradle / Ivy

The newest version!
/*
 * (C) Copyright 2018-2023, by Dimitrios Michail and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * See the CONTRIBUTORS.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the
 * GNU Lesser General Public License v2.1 or later
 * which is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
 */
package org.jgrapht.util;

import org.jgrapht.graph.*;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;

/**
 * Helper class for suppliers.
 *
 * @author Dimitrios Michail
 */
public class SupplierUtil
{

    /**
     * Supplier for {@link DefaultEdge}.
     */
    @SuppressWarnings("unchecked")
    public static final Supplier DEFAULT_EDGE_SUPPLIER =
        (Supplier & Serializable) DefaultEdge::new;

    /**
     * Supplier for {@link DefaultWeightedEdge}.
     */
    @SuppressWarnings("unchecked")
    public static final Supplier DEFAULT_WEIGHTED_EDGE_SUPPLIER =
        (Supplier & Serializable) DefaultWeightedEdge::new;

    /**
     * Supplier for {@link Object}.
     */
    @SuppressWarnings("unchecked")
    public static final Supplier OBJECT_SUPPLIER =
        (Supplier & Serializable) Object::new;

    /**
     * Create a supplier from a class which calls the default constructor.
     * 
     * @param clazz the class
     * @return the supplier
     * @param  the type of results supplied by this supplier
     */
    @SuppressWarnings("unchecked")
    public static  Supplier createSupplier(Class clazz)
    {
        // shortcut to use pre-defined constructor method reference based suppliers
        if (clazz == DefaultEdge.class) {
            return (Supplier) DEFAULT_EDGE_SUPPLIER;
        } else if (clazz == DefaultWeightedEdge.class) {
            return (Supplier) DEFAULT_WEIGHTED_EDGE_SUPPLIER;
        } else if (clazz == Object.class) {
            return (Supplier) OBJECT_SUPPLIER;
        }

        try {
            final Constructor constructor = clazz.getDeclaredConstructor();
            if ((!Modifier.isPublic(constructor.getModifiers())
                || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers()))
                && !constructor.canAccess(null))
            {
                constructor.setAccessible(true);
            }
            return new ConstructorSupplier<>(constructor);
        } catch (ReflectiveOperationException e) {
            // Defer throwing an exception to the first time the supplier is called
            return getThrowingSupplier(e);
        }
    }

    @SuppressWarnings("unchecked")
    private static  Supplier getThrowingSupplier(Throwable e)
    {
        return (Supplier & Serializable) () -> {
            throw new SupplierException(e.getMessage(), e);
        };
    }

    /**
     * Create a default edge supplier.
     * 
     * @return a default edge supplier
     */
    public static Supplier createDefaultEdgeSupplier()
    {
        return DEFAULT_EDGE_SUPPLIER;
    }

    /**
     * Create a default weighted edge supplier.
     * 
     * @return a default weighted edge supplier
     */
    public static Supplier createDefaultWeightedEdgeSupplier()
    {
        return DEFAULT_WEIGHTED_EDGE_SUPPLIER;
    }

    /**
     * Create an integer supplier which returns a sequence starting from zero.
     * 
     * @return an integer supplier
     */
    public static Supplier createIntegerSupplier()
    {
        return createIntegerSupplier(0);
    }

    /**
     * Create an integer supplier which returns a sequence starting from a specific numbers.
     * 
     * @param start where to start the sequence
     * @return an integer supplier
     */
    @SuppressWarnings("unchecked")
    public static Supplier createIntegerSupplier(int start)
    {
        int[] modifiableInt = new int[] { start }; // like a modifiable int
        return (Supplier & Serializable) () -> modifiableInt[0]++;
    }

    /**
     * Create a long supplier which returns a sequence starting from zero.
     * 
     * @return a long supplier
     */
    public static Supplier createLongSupplier()
    {
        return createLongSupplier(0);
    }

    /**
     * Create a long supplier which returns a sequence starting from a specific numbers.
     * 
     * @param start where to start the sequence
     * @return a long supplier
     */
    @SuppressWarnings("unchecked")
    public static Supplier createLongSupplier(long start)
    {
        long[] modifiableLong = new long[] { start }; // like a modifiable long
        return (Supplier & Serializable) () -> modifiableLong[0]++;
    }

    /**
     * Create a string supplier which returns unique strings. The returns strings are simply
     * integers starting from zero.
     * 
     * @return a string supplier
     */
    public static Supplier createStringSupplier()
    {
        return createStringSupplier(0);
    }

    /**
     * Create a string supplier which returns random UUIDs.
     * 
     * @return a string supplier
     */
    @SuppressWarnings("unchecked")
    public static Supplier createRandomUUIDStringSupplier()
    {
        return (Supplier & Serializable) () -> UUID.randomUUID().toString();
    }

    /**
     * Create a string supplier which returns unique strings. The returns strings are simply
     * integers starting from start.
     * 
     * @param start where to start the sequence
     * @return a string supplier
     */
    @SuppressWarnings("unchecked")
    public static Supplier createStringSupplier(int start)
    {
        int[] container = new int[] { start };
        return (Supplier & Serializable) () -> String.valueOf(container[0]++);
    }

    private static class ConstructorSupplier
        implements Supplier, Serializable
    {
        private final Constructor constructor;

        private static class SerializedForm
            implements Serializable
        {
            private static final long serialVersionUID = -2385289829144892760L;

            private final Class type;

            public SerializedForm(Class type)
            {
                this.type = type;
            }

            Object readResolve()
                throws ObjectStreamException
            {
                try {
                    return new ConstructorSupplier<>(type.getDeclaredConstructor());
                } catch (ReflectiveOperationException e) {
                    InvalidObjectException ex = new InvalidObjectException(
                        "Failed to get no-args constructor from " + type);
                    ex.initCause(e);
                    throw ex;
                }
            }
        }

        public ConstructorSupplier(Constructor constructor)
        {
            this.constructor = constructor;
        }

        @Override
        public T get()
        {
            try {
                return constructor.newInstance();
            } catch (ReflectiveOperationException ex) {
                throw new SupplierException("Supplier failed", ex);
            }
        }

        Object writeReplace()
            throws ObjectStreamException
        {
            return new SerializedForm<>(constructor.getDeclaringClass());
        }
    }
}