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            @Override
037            protected TideInvocation initialValue() {
038                return new TideInvocation();
039            }
040        };
041        
042        public static TideInvocation get() {
043            return invocation.get();
044        }
045        
046        public static TideInvocation init() {
047            TideInvocation ti = new TideInvocation();
048            invocation.set(ti);
049            return ti;
050        }
051        
052        public static void remove() {
053            invocation.remove();
054        }
055        
056        private boolean locked = false;
057        private boolean enabled = false;
058        private boolean updated = false;
059        private boolean evaluated = false;
060        private final List<ContextUpdate> updates = new ArrayList<ContextUpdate>();
061        private final List<ContextUpdate> results = new ArrayList<ContextUpdate>();
062        private final List<ContextEvent> events = new ArrayList<ContextEvent>();
063    
064        
065        public List<ContextUpdate> getUpdates() {
066            return updates;
067        }
068        
069        public List<ContextUpdate> getResults() {
070            return results;
071        }
072        
073        public List<ContextEvent> getEvents() {
074            return events;
075        }
076        
077        public void update(List<ContextUpdate> updates) {
078            this.enabled = true;
079            this.updated = false;
080            this.updates.clear();
081            if (updates != null)
082                this.updates.addAll(updates);
083        }
084        public void updated() {
085            this.updated = true;
086            this.updates.clear();
087        }
088        public boolean isUpdated() {
089            return this.updated;
090        }
091        
092        public void evaluate() {
093            this.evaluated = false;
094            this.results.clear();
095        }
096        public void evaluated(List<ContextUpdate> results) {
097            this.evaluated = true;
098    //        this.results.clear();
099            this.results.addAll(results);
100            this.updated = false;
101            this.updates.clear();
102        }
103        public boolean isEvaluated() {
104            return this.evaluated;
105        }
106        
107        public void addEvent(ContextEvent event) {
108            events.add(event);
109        }
110        
111        public void lock() {
112            this.locked = true;
113        }
114        public void unlock() {
115            this.locked = false;
116        }
117        public boolean isLocked() {
118            return this.locked;
119        }
120        
121        public boolean isEnabled() {
122            return enabled;
123        }
124    }