001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.cdi;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.granite.tide.invocation.ContextEvent;
027    import org.granite.tide.invocation.ContextUpdate;
028    
029    
030    /**
031     * @author William DRAI
032     */
033    public class TideInvocation {
034        
035        private static ThreadLocal<TideInvocation> invocation = new ThreadLocal<TideInvocation>();
036        
037        public static TideInvocation get() {
038            return invocation.get();
039        }
040        
041        public static TideInvocation init() {
042            TideInvocation ti = new TideInvocation();
043            invocation.set(ti);
044            return ti;
045        }
046        
047        public static void remove() {
048            invocation.remove();
049        }
050        
051        private boolean locked = false;
052        private boolean enabled = false;
053        private boolean updated = false;
054        private boolean evaluated = false;
055        private final List<ContextUpdate> updates = new ArrayList<ContextUpdate>();
056        private final List<ContextUpdate> results = new ArrayList<ContextUpdate>();
057        private final List<ContextEvent> events = new ArrayList<ContextEvent>();
058    
059        
060        public List<ContextUpdate> getUpdates() {
061            return updates;
062        }
063        
064        public List<ContextUpdate> getResults() {
065            return results;
066        }
067        
068        public List<ContextEvent> getEvents() {
069            return events;
070        }
071        
072        public void update(List<ContextUpdate> updates) {
073            this.enabled = true;
074            this.updated = false;
075            this.updates.clear();
076            if (updates != null)
077                this.updates.addAll(updates);
078        }
079        public void updated() {
080            this.updated = true;
081            this.updates.clear();
082        }
083        public boolean isUpdated() {
084            return this.updated;
085        }
086        
087        public void evaluate() {
088            this.evaluated = false;
089            this.results.clear();
090        }
091        public void evaluated(List<ContextUpdate> results) {
092            this.evaluated = true;
093    //        this.results.clear();
094            this.results.addAll(results);
095            this.updated = false;
096            this.updates.clear();
097        }
098        public boolean isEvaluated() {
099            return this.evaluated;
100        }
101        
102        public void addEvent(ContextEvent event) {
103            events.add(event);
104        }
105        
106        public void lock() {
107            this.locked = true;
108        }
109        public void unlock() {
110            this.locked = false;
111        }
112        public boolean isLocked() {
113            return this.locked;
114        }
115        
116        public boolean isEnabled() {
117            return enabled;
118        }
119    }