commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-544) ToStringStyle.registry ThreadLocal initialValue should return a Set that implements reference equality checking instead of object equality
Date Sat, 16 Jan 2010 08:50:54 GMT

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

Henri Yandell commented on LANG-544:
------------------------------------

If we're unable to move to JDK 1.6, a possibility is implementing Collections.newSetFromMap(Map)
in Lang.

Alternatively, implement an IdentityHashSet (rough draft):

{code:java}
package org.apache.commons.lang3.collections;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;

public class IdentityHashSet<E> extends AbstractSet<E> {

    private IdentityHashMap<E, E> decorated;

    public IdentityHashSet() {
        super();
    }

    public IdentityHashSet(Collection<E> collection) {
        if (collection != null) {
            HashMap<E, E> map = new HashMap<E, E>();
            for (E element : collection) {
                map.put(element, element);
            }
            decorated = new IdentityHashMap<E, E>(map);
        } else {
            decorated = new IdentityHashMap<E, E>();
        }
    }

    public int size() {
        return decorated.size();
    }

    public Iterator<E> iterator() {
        return decorated.keySet().iterator();
    }

    public boolean add(E element) {
        if (decorated.contains(element) ) {
            return false;
        } else {
            decorated.put(element, element);
            return true;
        }
    }
}
{code}

> ToStringStyle.registry ThreadLocal initialValue should return a Set that implements reference
equality checking instead of object equality
> ------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: LANG-544
>                 URL: https://issues.apache.org/jira/browse/LANG-544
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.builder.*
>    Affects Versions: 2.4
>            Reporter: Michael Richards
>             Fix For: 3.0
>
>
> ToStringStyle.registry ThreadLocal initialValue should return a Set that implements reference
equality checking instead of object equality.
> This change would still protect against cyclical object graphs, but also protect against
the following conditions:
> * Incorrect implementations of hashCode/equals for any objects in the object graph
> * Unintended side effects of the hashCode/equals methods for any objects in the object
graph
> Unfortunately, Java versions prior to 1.6 do not provide a simple built-in mechanism
to create such a Set.  This issue is discussed here in detail:
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4479578
> A possible work-around is to use an IdentityHashMap instead of a Set.  This class is
available in Java 1.4 and later.

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


Mime
View raw message