[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-dev
Subject:    Re: [groovy-dev] WeakHashMap: is it right tool?
From:       "Alex Tkachman" <alex.tkachman () gmail ! com>
Date:       2008-05-29 13:00:58
Message-ID: 4cf0f24c0805290600v699850d4sc50899c75b42558d () mail ! gmail ! com
[Download RAW message or body]

Debugger shows that it is not correct as well :(

On Thu, May 29, 2008 at 4:11 PM, Alex Tkachman <alex.tkachman@gmail.com> wrote:
> In you code obj will be never collected.
>
> Just for record here is correct way
>
>    HashMap<InstanceRef,T>
>
>    private static class InstanceRef extends WeakReference {
>        final int hash;
>
>        public InstanceRef(Object referent) {
>            super(referent);
>            hash = referent.hashCode();
>        }
>
>        public InstanceRef(Object referent, ReferenceQueue q) {
>            super(referent, q);
>            hash = referent.hashCode();
>        }
>
>        public int hashCode () {
>            return hash;
>        }
>
>        public boolean equals(Object obj) {
>            return get () == obj;
>        }
>    }
>
>
> On Thu, May 29, 2008 at 4:05 PM, Martin C. Martin
> <martin@martincmartin.com> wrote:
>>
>>
>> Alex Tkachman wrote:
>>>>
>>>> So you can use it of course, but for what? It depends on the context if
>>>> it
>>>> does what you need. So if you explain what you want to sue it for and
>>>> what
>>>> the keys and values would be then I can help you in answering this.
>>>>
>>>
>>> I experiment with per instance meta class and as you understand it
>>> doesn't work at all. The reason for my question is I can't imagine ANY
>>> situation when it can be useful :)
>>
>> Essentially, its a way to simulate adding extra fields to objects, without
>> changing the objects.  So if you wanted per-instance metaclasses for Java
>> objects, you could use the POJO as the key, and the metaclass as the value.
>>
>> Of course, that only works for POJOs whose equals() is the same as ==. To
>> get around that, you could create a small wrapper class:
>>
>> class ObjectIdentityEquals {
>>   public Object obj;
>>
>>   ObjectIdentityEquals(Object obj) {
>>      this.obj = obj;
>>   }
>>
>>   public boolean equals(Object other) {
>>      assert other isinstance ObjectIdentityEquals;
>>      return obj == other.obj;
>>   }
>> }
>>
>> (Well I'm a little rusty on my Java syntax, but you get the idea.)
>>
>> Just a random thought,
>> Martin
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic