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

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

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

import lombok.RequiredArgsConstructor;
import org.rx.util.function.BiFunc;

import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@RequiredArgsConstructor
public class MapView implements AbstractMap, Serializable {
    private static final long serialVersionUID = 1937534858824236935L;
    final Map map;
    final BiFunc keyView;
    transient EntrySetView entrySet;

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

    @Override
    public V get(Object key) {
        Iterator> i = map.entrySet().iterator();
        if (key == null) {
            while (i.hasNext()) {
                Entry e = i.next();
                if (e.getKey() == null) {
                    return e.getValue();
                }
            }
        } else {
            while (i.hasNext()) {
                Entry e = i.next();
                if (key.equals(keyView.apply(e.getKey()))) {
                    return e.getValue();
                }
            }
        }
        return null;
    }

    @Override
    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V remove(Object key) {
        Iterator> i = map.entrySet().iterator();
        Entry correctEntry = null;
        if (key == null) {
            while (correctEntry == null && i.hasNext()) {
                Entry e = i.next();
                if (e.getKey() == null) {
                    correctEntry = e;
                }
            }
        } else {
            while (correctEntry == null && i.hasNext()) {
                Entry e = i.next();
                if (key.equals(e.getKey())) {
                    correctEntry = e;
                }
            }
        }

        V oldValue = null;
        if (correctEntry != null) {
            oldValue = correctEntry.getValue();
            i.remove();
        }
        return oldValue;
    }

    @Override
    public Set> entrySet() {
        EntrySetView es;
        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<>(map, keyView));
    }

    @RequiredArgsConstructor
    static final class EntrySetView extends AbstractSet> {
        final Map map;
        final BiFunc keyView;

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

        @Override
        public void clear() {
            map.clear();
        }

        @Override
        public Iterator> iterator() {
            Iterator> iterator = map.entrySet().iterator();
            return new Iterator>() {
                Entry entry;
                K key;

                @Override
                public boolean hasNext() {
                    while (iterator.hasNext()) {
                        if ((key = keyView.apply((entry = iterator.next()).getKey())) == null) {
                            continue;
                        }
                        return true;
                    }
                    return false;
                }

                @Override
                public Entry next() {
                    return new java.util.AbstractMap.SimpleEntry(key, entry.getValue()) {
                        private static final long serialVersionUID = -1039216080459017222L;

                        @Override
                        public V setValue(V value) {
                            V v = entry.getValue();
                            super.setValue(value);
                            map.put(entry.getKey(), value);
                            return v;
                        }
                    };
                }

                @Override
                public void remove() {
                    iterator.remove();
                }
            };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy