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

org.eclipse.sisu.inject.MildValues Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*
 * Copyright (c) 2010-2024 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 */
package org.eclipse.sisu.inject;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/**
 * NON-thread-safe {@link Map} whose values are kept alive by soft/weak {@link Reference}s.
 */
class MildValues
    implements Map
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    final ReferenceQueue queue = new ReferenceQueue();

    final Map> map;

    private final boolean soft;

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    MildValues( final Map> map, final boolean soft )
    {
        this.map = map;
        this.soft = soft;
    }

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    public final boolean containsKey( final Object key )
    {
        // skip compact for performance reasons

        return map.containsKey( key );
    }

    public final boolean containsValue( final Object value )
    {
        // skip compact for performance reasons

        return map.containsValue( tempValue( value ) );
    }

    public final V get( final Object key )
    {
        // skip compact for performance reasons

        final Reference ref = map.get( key );
        return null != ref ? ref.get() : null;
    }

    public final V put( final K key, final V value )
    {
        compact();

        final Reference ref = map.put( key, mildValue( key, value ) );
        return null != ref ? ref.get() : null;
    }

    public final void putAll( final Map m )
    {
        compact();

        for ( final Entry e : m.entrySet() )
        {
            map.put( e.getKey(), mildValue( e.getKey(), e.getValue() ) );
        }
    }

    public final V remove( final Object key )
    {
        compact();

        final Reference ref = map.remove( key );
        return null != ref ? ref.get() : null;
    }

    public final void clear()
    {
        map.clear();

        compact();
    }

    public final boolean isEmpty()
    {
        compact();

        return map.isEmpty();
    }

    public final int size()
    {
        compact();

        return map.size();
    }

    public final Set keySet()
    {
        compact();

        return map.keySet();
    }

    public final Collection values()
    {
        compact();

        return new AbstractCollection()
        {
            @Override
            public Iterator iterator()
            {
                return new ValueItr();
            }

            @Override
            public int size()
            {
                return map.size();
            }
        };
    }

    public final Set> entrySet()
    {
        compact();

        return new AbstractSet>()
        {
            @Override
            public Iterator> iterator()
            {
                return new EntryItr();
            }

            @Override
            public int size()
            {
                return map.size();
            }
        };
    }

    // ----------------------------------------------------------------------
    // Implementation methods
    // ----------------------------------------------------------------------

    /**
     * @return Soft or weak {@link Reference} for the given key-value mapping.
     */
    final Reference mildValue( final K key, final V value )
    {
        return soft ? new Soft( key, value, queue ) : new Weak( key, value, queue );
    }

    /**
     * @return Temporary {@link Reference} for the given value; used in queries.
     */
    static final  Reference tempValue( final V value )
    {
        return new Weak( null, value, null );
    }

    /**
     * Compacts the map by removing cleared values.
     */
    void compact()
    {
        for ( Reference ref; ( ref = queue.poll() ) != null; )
        {
            // only remove this specific key-value mapping
            final Object key = ( (InverseMapping) ref ).key();
            if ( map.get( key ) == ref )
            {
                map.remove( key );
            }
        }
    }

    // ----------------------------------------------------------------------
    // Implementation types
    // ----------------------------------------------------------------------

    /**
     * Represents an inverse mapping from a value to its key.
     */
    interface InverseMapping
    {
        Object key();
    }

    /**
     * Soft value with an {@link InverseMapping} back to its key.
     */
    private static final class Soft
        extends MildKeys.Soft
        implements InverseMapping
    {
        // ----------------------------------------------------------------------
        // Implementation fields
        // ----------------------------------------------------------------------

        private final K key;

        // ----------------------------------------------------------------------
        // Constructors
        // ----------------------------------------------------------------------

        Soft( final K key, final V value, final ReferenceQueue queue )
        {
            super( value, queue );
            this.key = key;
        }

        // ----------------------------------------------------------------------
        // Public methods
        // ----------------------------------------------------------------------

        public Object key()
        {
            return key;
        }
    }

    /**
     * Weak value with an {@link InverseMapping} back to its key.
     */
    private static final class Weak
        extends MildKeys.Weak
        implements InverseMapping
    {
        // ----------------------------------------------------------------------
        // Implementation fields
        // ----------------------------------------------------------------------

        private final K key;

        // ----------------------------------------------------------------------
        // Constructors
        // ----------------------------------------------------------------------

        Weak( final K key, final V value, final ReferenceQueue queue )
        {
            super( value, queue );
            this.key = key;
        }

        // ----------------------------------------------------------------------
        // Public methods
        // ----------------------------------------------------------------------

        public Object key()
        {
            return key;
        }
    }

    /**
     * {@link Iterator} that iterates over reachable values in the map.
     */
    final class ValueItr
        implements Iterator
    {
        // ----------------------------------------------------------------------
        // Implementation fields
        // ----------------------------------------------------------------------

        private final Iterator> itr = map.values().iterator();

        private V nextValue;

        // ----------------------------------------------------------------------
        // Public methods
        // ----------------------------------------------------------------------

        public boolean hasNext()
        {
            // find next value that is still reachable
            while ( null == nextValue && itr.hasNext() )
            {
                nextValue = itr.next().get();
            }
            return null != nextValue;
        }

        public V next()
        {
            if ( hasNext() )
            {
                // populated by hasNext()
                final V value = nextValue;
                nextValue = null;
                return value;
            }
            throw new NoSuchElementException();
        }

        public void remove()
        {
            itr.remove();
        }
    }

    /**
     * {@link Iterator} that iterates over reachable entries in the map.
     */
    final class EntryItr
        implements Iterator>
    {
        // ----------------------------------------------------------------------
        // Implementation fields
        // ----------------------------------------------------------------------

        private final Iterator>> itr = map.entrySet().iterator();

        private Entry> nextEntry;

        private V nextValue;

        // ----------------------------------------------------------------------
        // Public methods
        // ----------------------------------------------------------------------

        public boolean hasNext()
        {
            // find next entry that is still reachable
            while ( null == nextValue && itr.hasNext() )
            {
                nextEntry = itr.next();
                nextValue = nextEntry.getValue().get();
            }
            return null != nextValue;
        }

        public Entry next()
        {
            if ( hasNext() )
            {
                // populated by hasNext()
                final Entry entry = new StrongEntry( nextEntry, nextValue );
                nextEntry = null;
                nextValue = null;
                return entry;
            }
            throw new NoSuchElementException();
        }

        public void remove()
        {
            itr.remove();
        }
    }

    /**
     * {@link Entry} that delegates to the original entry, but maintains a strong reference to the value.
     */
    final class StrongEntry
        implements Entry
    {
        // ----------------------------------------------------------------------
        // Implementation fields
        // ----------------------------------------------------------------------

        private final Entry> entry;

        private V value;

        // ----------------------------------------------------------------------
        // Constructors
        // ----------------------------------------------------------------------

        StrongEntry( final Entry> entry, final V value )
        {
            this.entry = entry;
            this.value = value;
        }

        // ----------------------------------------------------------------------
        // Public methods
        // ----------------------------------------------------------------------

        public K getKey()
        {
            return entry.getKey();
        }

        public V getValue()
        {
            return value;
        }

        public V setValue( final V newValue )
        {
            final V oldValue = value;
            entry.setValue( mildValue( getKey(), newValue ) );
            value = newValue;
            return oldValue;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy