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

org.jclarion.clarion.primative.ThreadStateGetter Maven / Gradle / Ivy

There is a newer version: 1.86
Show newest version
package org.jclarion.clarion.primative;


public class ThreadStateGetter extends AbstractStateGetter 
{
    private AbstractStateFactory factory;
    private ActiveThreadMap threadedState;
    private AbstractStateGetter state;
    
    public ThreadStateGetter(AbstractStateFactory aFactory,AbstractStateGetter aState)
    {
        this.factory=aFactory;
        this.state=aState;
        threadedState=new ActiveThreadMap();
    }
    
    @Override
    public T get() {
        return get(Thread.currentThread());
    }
    
    @Override
    public T get(Thread t) {
        T result;
        synchronized(threadedState) {
            result=threadedState.get(t);
            if (result==null) {
                result=factory.cloneState(state.get());
                threadedState.put(t,result);
            }
        }
        return result;
    }
    
    @Override
    public boolean isThreaded() {
        return true;
    }

    @Override
    public AbstractStateGetter getLockedGetter(Thread t) {
        return new LockedStateGetter(get(t));
    }

    @Override
    public void reset() {
        synchronized(threadedState) {
            state.reset();
            threadedState.clear();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy