<commit_before>package freactive;

import clojure.lang.*;

public class EntityCursorMap {
    class EntityCursor implements IReactiveAtom, IEntityCursor {
        private final Object entityKey;

        EntityCursor(Object entityKey)
        {
            this.entityKey = entityKey;
        }

        @Override
        public Object entityKey() {
            return entityKey;
        }

        @Override
        public Object swap(IFn f) {
            coll.update(entityKey, f);
            return deref();
        }

        @Override
        public Object swap(IFn f, Object arg) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object o) {
                    return f.invoke(o, arg);
                }
            });
            return deref();
        }

        @Override
        public Object swap(IFn f, Object arg1, Object arg2) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object o) {
                    return f.invoke(o, arg1, arg2);
                }
            });
            return deref();
        }

        @Override
        public Object swap(IFn f, Object x, Object y, ISeq args) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object o) {
                    return f.applyTo(RT.listStar(x, y, args));
                }
            });
            return deref();
        }

        @Override
        public boolean compareAndSet(Object oldv, Object newv) {
            throw new UnsupportedOperationException("compareAndSet not supported for entity cursors");
        }

        @Override
        public Object reset(Object newval) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object arg1) {
                    return newval;
               }
            });
            return deref();
        }

        @Override
        public IInvalidates addInvalidationWatch(Object key, IFn callback) {
            return null;
        }

        @Override
        public IInvalidates removeInvalidationWatch(Object key) {
            return null;
        }

        @Override
        public void setValidator(IFn vf) {

        }

        @Override
        public IFn getValidator() {
            return null;
        }

        @Override
        public IPersistentMap getWatches() {
            return null;
        }

        @Override
        public IRef addWatch(Object key, IFn callback) {
            return null;
        }

        @Override
        public IRef removeWatch(Object key) {
            return null;
        }

        @Override
        public Object deref() {
            return null;
        }
    }

    private final IObservableCollection coll;
    private final Atom cursorMap = new Atom(PersistentHashMap.EMPTY);

    public EntityCursorMap(IObservableCollection coll) {
        this.coll = coll;
        coll.subscribe(this, new AFn() {
            @Override
            public Object invoke(Object key, Object coll, Object changes) {
                return null;
            }
        });
    }

    public IEntityCursor getEntityCursor(Object key) {
        Object cursor = ((IPersistentMap)cursorMap.deref()).valAt(key);
        if(cursor != null)
            return (IEntityCursor)cursor;
        return null;
    }
}<commit_msg>Work on EntityCursorMap for Java.<commit_after>package freactive;

import clojure.lang.*;

import java.util.Map;

public class EntityCursorMap {
    class EntityCursor implements IReactiveAtom, IEntityCursor {
        private final Object entityKey;
        private Object entityValue;
        private CallbackSet watches;
        private CallbackSet iwatches;

        private EntityCursor(Object entityKey, Object entityValue)
        {
            this.entityKey = entityKey;
            this.entityValue = entityValue;
        }

        private void update(Object newValue)
        {
            Object oldValue = entityValue;
            entityValue = newValue;
            if(watches != null)
                watches.invokeAll(oldValue, newValue);
            if(iwatches != null)
                iwatches.invokeAll();
        }

        @Override
        public Object entityKey() {
            return entityKey;
        }

        @Override
        public Object swap(IFn f) {
            coll.update(entityKey, f);
            return deref();
        }

        @Override
        public Object swap(IFn f, Object arg) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object o) {
                    return f.invoke(o, arg);
                }
            });
            return deref();
        }

        @Override
        public Object swap(IFn f, Object arg1, Object arg2) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object o) {
                    return f.invoke(o, arg1, arg2);
                }
            });
            return deref();
        }

        @Override
        public Object swap(IFn f, Object x, Object y, ISeq args) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object o) {
                    return f.applyTo(RT.listStar(x, y, args));
                }
            });
            return deref();
        }

        @Override
        public boolean compareAndSet(Object oldv, Object newv) {
            throw new UnsupportedOperationException("compareAndSet not supported for entity cursors");
        }

        @Override
        public Object reset(Object newval) {
            coll.update(entityKey, new AFn() {
                @Override
                public Object invoke(Object arg1) {
                    return newval;
               }
            });
            return deref();
        }

        @Override
        public IInvalidates addInvalidationWatch(Object key, IFn callback) {
            if(iwatches == null)
                iwatches = new CallbackSet(this);
            iwatches.add(key, callback);
            return this;
        }

        @Override
        public IInvalidates removeInvalidationWatch(Object key) {
            if(iwatches != null)
                iwatches.remove(key);
            return this;
        }

        @Override
        public void setValidator(IFn vf) {
            throw new UnsupportedOperationException();
        }

        @Override
        public IFn getValidator() {
            return null;
        }

        @Override
        public IPersistentMap getWatches() {
            if(watches != null)
                return watches.getCallbacks();
            return null;
        }

        @Override
        public IRef addWatch(Object key, IFn callback) {
            if(watches == null)
                watches = new CallbackSet(this);
            watches.add(key, callback);
            return this;
        }

        @Override
        public IRef removeWatch(Object key) {
            if(watches != null)
                watches.remove(key);
            return this;
        }

        @Override
        public Object deref() {
            return entityValue;
        }
    }

    private final IObservableCollection coll;

    private final Atom cursorMap = new Atom(PersistentHashMap.EMPTY);

    public EntityCursorMap(IObservableCollection coll) {
        this.coll = coll;
        coll.subscribe(this, new AFn() {
            @Override
            public Object invoke(Object key, Object coll, Object changes) {
                for(ISeq cs = RT.seq(changes); cs != null; cs = cs.next())
                {
                    Object first = cs.first();
                    if(first instanceof Map.Entry)
                    {
                        Map.Entry me = (Map.Entry)first;
                        final Object k = me.getKey();
                        Object v = me.getValue();
                        Object cursor = ((IPersistentMap)cursorMap.deref()).valAt(k);
                        if(cursor != null)
                        {
                            ((EntityCursor)cursor).update(v);
                            if(v == null)
                                cursorMap.swap(new AFn() {
                                    @Override
                                    public Object invoke(Object state) {
                                        return ((IPersistentMap)state).without(k);
                                    }
                                });
                        }
                        else {
                            if(v != null)
                            {
                                final EntityCursor newCursor = new EntityCursor(k, v);
                                cursorMap.swap(new AFn() {
                                    @Override
                                    public Object invoke(Object state) {
                                        return ((IPersistentMap)state).assoc(k, newCursor);
                                    }
                                });
                            }
                        }
                    }
                }
                return null;
            }
        });
    }

    public IEntityCursor getEntityCursor(Object key) {
        Object cursor = ((IPersistentMap)cursorMap.deref()).valAt(key);
        if(cursor != null)
            return (IEntityCursor)cursor;
        else return null;
    }
}
{'fraction_non_alphanumeric': 0.07887588800668617, 'fraction_numerical': 0.0010447137484329294, 'mean_word_length': 1.2200834879406308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6801699', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2247, 'n_words': 587}