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

org.jboss.logmanager.CopyOnWriteWeakMap Maven / Gradle / Ivy

There is a newer version: 3.0.6.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.logmanager;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

final class CopyOnWriteWeakMap extends AbstractMap implements ConcurrentMap {

    private final Queue queue = new Queue();

    private static final FastCopyHashMap EMPTY = new FastCopyHashMap(32, 0.25f);

    @SuppressWarnings({ "unchecked" })
    private FastCopyHashMap> empty() { return (FastCopyHashMap>) EMPTY; }

    private volatile FastCopyHashMap> map = empty();

    private FastCopyHashMap> cleanCopyForRemove() {
        assert Thread.holdsLock(this);
        final Map> oldMap = map;
        final Queue queue = this.queue;
        if (oldMap.isEmpty()) {
            queue.clear();
            return empty();
        }
        final FastCopyHashMap> newMap = new FastCopyHashMap>(oldMap);
        Node ref;
        while ((ref = queue.poll()) != null) {
            final Object key = ref.getKey();
            if (newMap.get(key) == ref) {
                newMap.remove(key);
                if (newMap.isEmpty()) {
                    queue.clear();
                    return empty();
                }
            }
        }
        return newMap;
    }

    private FastCopyHashMap> cleanCopyForMod() {
        assert Thread.holdsLock(this);
        final Map> oldMap = map;
        final Queue queue = this.queue;
        if (oldMap.isEmpty()) {
            queue.clear();
            return empty().clone();
        }
        final FastCopyHashMap> newMap = new FastCopyHashMap>(oldMap);
        Node ref;
        while ((ref = queue.poll()) != null) {
            final Object key = ref.getKey();
            if (newMap.get(key) == ref) {
                newMap.remove(key);
                if (newMap.isEmpty()) {
                    queue.clear();
                    return empty().clone();
                }
            }
        }
        return newMap;
    }

    public V putIfAbsent(final K key, final V value) {
        if (key == null) {
            throw new IllegalArgumentException("key is null");
        }
        if (value == null) {
            throw new IllegalArgumentException("value is null");
        }
        synchronized (this) {
            final Node oldNode = map.get(key);
            if (oldNode != null) {
                final V val = oldNode.get();
                if (val != null) return val;
            }
            final FastCopyHashMap> newMap = cleanCopyForMod();
            newMap.put(key, new Node(key, value, queue));
            map = newMap;
            return null;
        }
    }

    public boolean remove(final Object key, final Object value) {
        synchronized (this) {
            final Node oldNode = map.get(key);
            final V existing = oldNode.get();
            if (existing != null && existing.equals(value)) {
                final FastCopyHashMap> newMap = cleanCopyForRemove();
                newMap.remove(key);
                map = newMap;
                return true;
            }
        }
        return false;
    }

    public boolean replace(final K key, final V oldValue, final V newValue) {
        if (newValue == null) {
            throw new IllegalArgumentException("newValue is null");
        }
        if (oldValue == null) {
            return false;
        }
        synchronized (this) {
            final Node oldNode = map.get(key);
            final V existing = oldNode.get();
            if (existing != null && existing.equals(oldValue)) {
                final FastCopyHashMap> newMap = cleanCopyForMod();
                map.put(key, new Node(key, newValue, queue));
                map = newMap;
                return true;
            }
        }
        return false;
    }

    public V replace(final K key, final V value) {
        if (value == null) {
            throw new IllegalArgumentException("value is null");
        }
        synchronized (this) {
            final Node oldNode = map.get(key);
            final V existing = oldNode.get();
            if (existing != null) {
                final FastCopyHashMap> newMap = cleanCopyForMod();
                map.put(key, new Node(key, value, queue));
                map = newMap;
            }
            return existing;
        }
    }

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

    public boolean isEmpty() {
        return map.isEmpty();
    }

    public boolean containsKey(final Object key) {
        return map.containsKey(key);
    }

    public boolean containsValue(final Object value) {
        if (value == null) return false;
        for (Node node : map.values()) {
            if (value.equals(node.get())) {
                return true;
            }
        }
        return false;
    }

    public V get(final Object key) {
        final Node node = map.get(key);
        return node == null ? null : node.get();
    }

    public V put(final K key, final V value) {
        if (key == null) {
            throw new IllegalArgumentException("key is null");
        }
        if (value == null) {
            throw new IllegalArgumentException("value is null");
        }
        synchronized (this) {
            final FastCopyHashMap> newMap = cleanCopyForMod();
            final Node old = newMap.put(key, new Node(key, value, queue));
            map = newMap;
            return old == null ? null : old.get();
        }
    }

    public V remove(final Object key) {
        if (key == null) return null;
        synchronized (this) {
            final FastCopyHashMap> newMap = cleanCopyForRemove();
            final Node old = newMap.remove(key);
            map = newMap;
            return old == null ? null : old.get();
        }
    }

    public void clear() {
        synchronized (this) {
            map = empty();
        }
    }

    public Set> entrySet() {
        final FastCopyHashMap> snapshot = map;
        final Map copyMap = new HashMap();
        for (Node node : snapshot.values()) {
            final V value = node.get();
            if (value == null) continue;
            final K key = node.getKey();
            copyMap.put(key, value);
        }
        return Collections.unmodifiableMap(copyMap).entrySet();
    }

    private static final class Node extends WeakReference {
        private final K key;

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

        K getKey() {
            return key;
        }
    }

    private static final class Queue extends ReferenceQueue {

        public Node poll() {
            return (Node) super.poll();
        }

        void clear() {
            while (poll() != null);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy