[jira] Created: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

14 messages Options
Embed this post
Permalink
JIRA jira@apache.org

[jira] Created: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
ClassUtils.getAllInterfaces(...) could be more efficient
--------------------------------------------------------

                 Key: LANG-500
                 URL: https://issues.apache.org/jira/browse/LANG-500
             Project: Commons Lang
          Issue Type: Improvement
    Affects Versions: 3.x
            Reporter: Pino Silvaggio
            Priority: Trivial


This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }


--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pino Silvaggio updated LANG-500:
--------------------------------

    Description:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

<pre>
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
</pre>

  was:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }



> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> <pre>
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> </pre>

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pino Silvaggio updated LANG-500:
--------------------------------

    Description:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

{{
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
}}

  was:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

<pre>
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
</pre>


> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {{
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> }}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pino Silvaggio updated LANG-500:
--------------------------------

    Description:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

{{monospaced}}
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
{{monospaced}}

  was:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

{{
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
}}


> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {{monospaced}}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {{monospaced}}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pino Silvaggio updated LANG-500:
--------------------------------

    Description:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

{code}
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
{code}

  was:
This could seem like a very minor thing but why not improve
the code once in a while...

Something like this could replace the current inefficient code:

{{monospaced}}
    public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
        LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();

        getAllInterfaces(clazz, interfacesSet, interfacesList);

        return interfacesList;
    }

    private static void getAllInterfaces(
            Class<?> clazz,
            HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
    {
        while (clazz != null)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesSet.add(i))
                {
                    interfacesList.add(i);
                    getAllInterfaces(i, interfacesSet, interfacesList);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
{{monospaced}}


> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Commented: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12703994#action_12703994 ]

Joerg Schaible commented on LANG-500:
-------------------------------------

You might improve this even more:

{code}
public static List<Class<?>> getAllInterfaces(Class<?> clazz)
    {
        if (clazz == null)
        {
            return null;
        }

        Map<Class<?>, Object> interfacesMap = new LinkedHashMap<Class<?>, Object>();

        getAllInterfaces(clazz, interfacesMap);

        return new ArrayList<Class<?>>(interfacesMap.getKeys());
    }

    private static void getAllInterfaces(Class<?> clazz, Map<Class<?>, Object> interfacesMap)
    {
        while (clazz != Object.class)
        {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (Class<?> i : interfaces)
            {
                if (!interfacesMap.put(i, null))
                {
                    getAllInterfaces(i, interfacesMap);
                }
            }

            clazz = clazz.getSuperclass();
        }
    }
{code}

Remember, the HasSet is using a HashMap internally anyway ...

However, as always with "more efficient" implementations: It would actually be nice to have some numbers from a real profiler.

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Commented: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12704217#action_12704217 ]

Pino Silvaggio commented on LANG-500:
-------------------------------------

You are absolutely right.

My first tought was to only use a LinkedHashSet and return a list by copying the elements.

After some profiling I noticed a small (need I say) negligeable performance gain
by not copying the elements and directly returning a list.

However, it's less memory efficient obviously.

Anyhow, there is no doubt that your way or my way is more efficient
than the actual code. Firstly because it doesn't go through
the list of found interfaces on every iteration and secondly because
it doesn't try to rediscover the already found interfaces.

I will try to attach some profiling if I find the time.

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Commented: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12704220#action_12704220 ]

Pino Silvaggio commented on LANG-500:
-------------------------------------

Oh and by the way this wouldn't work

<code>
!interfacesMap.put(i, null)
<code>

because put returns the previous object which in turn
would alway be null in your case and therefore
we wouldn't know if there was a previous element there.

So we would need to use a dummy object which
is what HashSet does :)

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Commented: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12704304#action_12704304 ]

Joerg Schaible commented on LANG-500:
-------------------------------------

Hehe, good catch. Take *interfaceMap* ;-)

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pino Silvaggio updated LANG-500:
--------------------------------

    Attachment: patch-ClassUtils-02.txt
                patch-ClassUtils-01.txt

Patch 01 is my original version where I use an HashSet and a List.

Patch 02 uses a LinkedHashSet where a List is constructed on the return.

They are both good.

02 is a bit more memory efficient and theoretically slower (negligible) O(2N) = O(N).
01 is O(N).


> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>         Attachments: patch-ClassUtils-01.txt, patch-ClassUtils-02.txt
>
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-500:
-------------------------------

    Attachment: LANG-500.patch

Patch #2 didn't work, so I did it by hand. Here's the patch against the current trunk.

Tests fail however. I've not dug in enough yet to work out why.

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>         Attachments: LANG-500.patch, patch-ClassUtils-01.txt, patch-ClassUtils-02.txt
>
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Updated: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-500:
-------------------------------

    Fix Version/s: 3.0

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: LANG-500.patch, patch-ClassUtils-01.txt, patch-ClassUtils-02.txt
>
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Commented: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12736214#action_12736214 ]

Pino Silvaggio commented on LANG-500:
-------------------------------------

Oups...

Correction:

 if (!interfacesFound.add(i))

becomes

 if (interfacesFound.add(i))

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: LANG-500.patch, patch-ClassUtils-01.txt, patch-ClassUtils-02.txt
>
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

JIRA jira@apache.org

[jira] Closed: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient

Reply Threaded More More options
Print post
Permalink
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell closed LANG-500.
------------------------------

    Resolution: Fixed

svn ci -m "Improving performance of getAllInterfaces per LANG-500's patch from Pino Silvaggio"
Sending        src/java/org/apache/commons/lang/ClassUtils.java
Transmitting file data .
Committed revision 802477.

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: LANG-500.patch, patch-ClassUtils-01.txt, patch-ClassUtils-02.txt
>
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.