package com.github.qrokodial.sparkle.utilities.casting;

import java.util.Optional;

public class SafeCast {
    /**
     * Attempts to cast an object to a given type.
     *
     * @param object
     * @param typeClass
     * @param <T>
     * @return the casted object, if successful
     */
    public static <T> Optional<T> toType(Object object, Class<T> typeClass) {
        if (object == null || !typeClass.isInstance(object)) {
            return Optional.empty();
        }

        try {
            return Optional.of(typeClass.cast(object));
        } catch (ClassCastException e) {
            return Optional.empty();
        }
    }

    /**
     * Attempts to cast an object to a string.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<String> toString(Object object) {
        if (object == null) {
            return Optional.empty();
        }

        if (object instanceof String) {
            return Optional.of((String)object);
        }

        return Optional.of(object.toString());
    }

    /**
     * Attempts to cast an object to a byte.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Byte> toByte(Object object) {
        if (object instanceof String) {
            try {
                return Optional.of(Byte.parseByte((String)object));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        }

        return toType(object, Byte.class);
    }

    /**
     * Attempts to cast an object to a short.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Short> toShort(Object object) {
        if (object instanceof String) {
            try {
                return Optional.of(Short.parseShort((String)object));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        }

        return toType(object, Short.class);
    }

    /**
     * Attempts to cast an object to an integer.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Integer> toInteger(Object object) {
        if (object instanceof String) {
            try {
                return Optional.of(Integer.parseInt((String)object));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        }

        return toType(object, Integer.class);
    }

    /**
     * Attempts to cast an object to a long.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Long> toLong(Object object) {
        if (object instanceof String) {
            try {
                return Optional.of(Long.parseLong((String)object));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        }

        return toType(object, Long.class);
    }

    /**
     * Attempts to cast an object to a float.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Float> toFloat(Object object) {
        if (object instanceof String) {
            try {
                return Optional.of(Float.parseFloat((String)object));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        }

        return toType(object, Float.class);
    }

    /**
     * Attempts to cast an object to a double.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Double> toDouble(Object object) {
        if (object instanceof String) {
            try {
                return Optional.of(Double.parseDouble((String)object));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        }

        return toType(object, Double.class);
    }

    /**
     * Attempts to cast an object to a boolean.
     *
     * @param object
     * @return the casted object, if successful
     */
    public static Optional<Boolean> toBoolean(Object object) {
        if (object instanceof String) {
            return Optional.of(Boolean.parseBoolean((String)object));
        }

        return toType(object, Boolean.class);
    }
}

@@ -279,32 +279,67 @@
 cessful%0A     */%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public stati
@@ -430,12 +430,1047 @@
 null
- %7C%7C 
+) %7B%0A            return Optional.empty();%0A        %7D%0A%0A        if (object instanceof String) %7B%0A            if (typeClass.isInstance(Number.class)) %7B%0A                if (typeClass.isInstance(Byte.class)) %7B%0A                    return (Optional%3CT%3E)toByte(object);%0A                %7D else if (typeClass.isInstance(Short.class)) %7B%0A                    return (Optional%3CT%3E)toShort(object);%0A                %7D else if (typeClass.isInstance(Integer.class)) %7B%0A                    return (Optional%3CT%3E)toInteger(object);%0A                %7D else if (typeClass.isInstance(Long.class)) %7B%0A                    return (Optional%3CT%3E)toLong(object);%0A                %7D else if (typeClass.isInstance(Float.class)) %7B%0A                    return (Optional%3CT%3E)toFloat(object);%0A                %7D else if (typeClass.isInstance(Double.class)) %7B%0A                    return (Optional%3CT%3E)toDouble(object);%0A                %7D%0A            %7D else if (typeClass.isInstance(Boolean.class)) %7B%0A                return (Optional%3CT%3E)toBoolean(object);%0A            %7D%0A        %7D%0A%0A        if (
 !typ

Improve the accuracy of SafeCast#toType