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

org.rx.bean.AbstractMap Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.bean;

import java.util.*;
import java.util.concurrent.ConcurrentMap;

public interface AbstractMap extends ConcurrentMap {
    @Override
    default V putIfAbsent(K key, V value) {
        synchronized (this) {
            V v = get(key);
            return v == null ? put(key, value) : v;
        }
    }

    @Override
    default boolean remove(Object key, Object value) {
        synchronized (this) {
            V v = get(key);
            if (v != null && Objects.equals(v, value)) {
                remove(key);
                return true;
            }
            return false;
        }
    }

    @Override
    default boolean replace(K key, V oldValue, V newValue) {
        synchronized (this) {
            V v = get(key);
            if (v != null && Objects.equals(v, oldValue)) {
                put(key, newValue);
                return true;
            }
            return false;
        }
    }

    @Override
    default V replace(K key, V value) {
        synchronized (this) {
            V v = get(key);
            return v != null ? put(key, value) : null;
        }
    }

    @Override
    default boolean isEmpty() {
        return size() == 0;
    }

    @Override
    default boolean containsKey(Object key) {
        return get(key) != null;
    }

    @Override
    default boolean containsValue(Object value) {
        Iterator> i = entrySet().iterator();
        if (value == null) {
            while (i.hasNext()) {
                Entry e = i.next();
                if (e.getValue() == null) {
                    return true;
                }
            }
        } else {
            while (i.hasNext()) {
                Entry e = i.next();
                if (value.equals(e.getValue())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    default void putAll(Map m) {
        for (Map.Entry e : m.entrySet()) {
            put(e.getKey(), e.getValue());
        }
    }

    @Override
    default void clear() {
        entrySet().clear();
    }

    @Override
    default Set keySet() {
        return new AbstractSet() {
            public Iterator iterator() {
                return new Iterator() {
                    final Iterator> i = entrySet().iterator();

                    public boolean hasNext() {
                        return i.hasNext();
                    }

                    public K next() {
                        return i.next().getKey();
                    }

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

            public int size() {
                return AbstractMap.this.size();
            }

            public boolean isEmpty() {
                return AbstractMap.this.isEmpty();
            }

            public void clear() {
                AbstractMap.this.clear();
            }

            public boolean contains(Object k) {
                return AbstractMap.this.containsKey(k);
            }
        };
    }

    @Override
    default Collection values() {
        return new AbstractCollection() {
            public Iterator iterator() {
                return new Iterator() {
                    final Iterator> i = entrySet().iterator();

                    public boolean hasNext() {
                        return i.hasNext();
                    }

                    public V next() {
                        return i.next().getValue();
                    }

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

            public int size() {
                return AbstractMap.this.size();
            }

            public boolean isEmpty() {
                return AbstractMap.this.isEmpty();
            }

            public void clear() {
                AbstractMap.this.clear();
            }

            public boolean contains(Object v) {
                return AbstractMap.this.containsValue(v);
            }
        };
    }

    @Override
    default Set> entrySet() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy