    001package com.hammurapi.store;
    002
    003import java.util.Comparator;
    004import java.util.Iterator;
    005import java.util.concurrent.locks.Lock;
    006
    007import com.hammurapi.common.Util;
    008import com.hammurapi.extract.CommutativeAnd;
    009import com.hammurapi.extract.Extractor;
    010import com.hammurapi.extract.Predicate;
    011import com.hammurapi.store.Index.Type;
    012
    013public abstract class LiveView<T, PK, S extends Store<T,PK,S>> implements Store<T, PK, S> {
    014
    015        protected S master;
    016        private Predicate<T, S> selector;
    017
    018        public LiveView(S master, Predicate<T, S> selector) {
    019                this.master = master;
    020                this.selector = selector;
    021        }
    022
    023        public Iterator<T> iterator() {
    024                return master.get(selector).iterator();
    025        }
    026
    027        public Handle<T, PK, S> put(T obj, Predicate<T, S>... validators) {
    028                if (matches(obj)) {
    029                        return master.put(obj, validators);
    030                }
    031                throw new StoreException("Object does not match the view predicate.");
    032        }
    033
    034        public Iterable<T> getAll() {
    035                return master.get(selector);
    036        }
    037
    038        public int queryAll(QueryTask<T, PK, S> processor) {
    039                return master.query(selector, processor);
    040        }
    041
    042        public T getByPrimaryKey(PK primaryKey) {
    043                T ret = master.getByPrimaryKey(primaryKey);
    044                return matches(ret) ? ret : null;
    045        }
    046
    047        @SuppressWarnings("unchecked")
    048        private Boolean matches(T ret) {
    049                return selector.extract((S) this, null, Util.wrap(ret));
    050        }
    051
    052        public Iterable<T> get(Predicate<T, S> selector) {
    053                return master.get(and(selector));
    054        }
    055
    056        @SuppressWarnings("unchecked")
    057        private Predicate<T, S> and(Predicate<T, S> selector) {
    058                return selector==null ? this.selector : new CommutativeAnd<T, S>(-1, null, selector, this.selector);
    059        }
    060
    061        public int query(
    062                        Predicate<T, S> selector,
    063                        QueryTask<T, PK, S> processor) {
    064
    065                return master.query(and(selector), processor);
    066        }
    067
    068        public <V> int query(
    069                        Predicate<T, S> selector,
    070                        Extractor<T, V, S> extractor,
    071                        QueryTask<V, PK, S> processor) {
    072                return master.query(and(selector), extractor, processor);
    073        }
    074
    075        public <V> int queryMultiple(
    076                        Predicate<T, S> selector,
    077                        Extractor<T, Iterable<V>, S> extractor,
    078                        Predicate<V, S> valueSelector,
    079                        QueryTask<V, PK, S> processor) {
    080
    081                return master.queryMultiple(
    082                                and(selector),
    083                                extractor,
    084                                valueSelector,
    085                                processor);
    086        }
    087
    088        public Lock readLock() {
    089                return master.readLock();
    090        }
    091
    092        public Lock writeLock() {
    093                return master.writeLock();
    094        }
    095
    096        public <V> Iterable<V> get(
    097                        Predicate<T, S> selector,
    098                        Extractor<T, V, S> extractor,
    099                        boolean ordered,
    100                        Comparator<V> comparator) {
    101
    102                return master.get(
    103                                and(selector),
    104                                extractor,
    105                                ordered,
    106                                comparator);
    107        }
    108
    109        public <V> Iterable<V> getMultiple(
    110                        Predicate<T, S> selector,
    111                        Extractor<T, Iterable<V>, S> extractor,
    112                        Predicate<V, S> valueSelector,
    113                        boolean ordered,
    114                        Comparator<V> comparator) {
    115
    116                return master.getMultiple(
    117                                and(selector),
    118                                extractor,
    119                                valueSelector,
    120                                ordered,
    121                                comparator);
    122        }
    123
    124        public void clear() {
    125                master.remove(selector);
    126        }
    127
    128        public boolean remove(T obj) {
    129                return matches(obj) ? master.remove(obj) : false;
    130        }
    131
    132        public boolean removeByPrimaryKey(PK primaryKey) {
    133                T obj = master.getByPrimaryKey(primaryKey);
    134                if (obj==null) {
    135                        return false;
    136                }
    137                if (matches(obj)) {
    138                        return master.removeByPrimaryKey(primaryKey);
    139                }
    140                return false;
    141        }
    142
    143        public int remove(Predicate<T, S> selector) {
    144                return master.remove(and(selector));
    145        }
    146
    147        public int update(
    148                        Predicate<T, S> selector,
    149                        UpdateTask<T, PK, S> updater) {
    150
    151                return master.update(and(selector), updater);
    152        }
    153
    154        public <V, ST extends T> Index<T, ST, PK, V, S> addIndex(
    155                        Predicate<T, S> predicate,
    156                        Extractor<ST, V, S> extractor,
    157                        Type type,
    158                        boolean ordered, Comparator<V> comparator) {
    159
    160                return master.addIndex(
    161                                and(predicate),
    162                                extractor,
    163                                type,
    164                                ordered,
    165                                comparator);
    166        }
    167
    168        public Extractor<T, PK, S> getPrimaryKeyExtractor() {
    169                return master.getPrimaryKeyExtractor();
    170        }
    171
    172        public S createView(
    173                        Predicate<T, S> selector,
    174                        ViewType viewType) {
    175
    176                return master.createView(and(selector), viewType);
    177        }
    178
    179        protected S getMaster() {
    180                return master;
    181        }
    182
    183}