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

net.tascalate.concurrent.var.ContextualObject Maven / Gradle / Ivy

/**
 * Copyright 2015-2020 Valery Silaev (http://vsilaev.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.tascalate.concurrent.var;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

abstract class ContextualObject {
    private final List> contextVars;
    private final ContextTrampoline.Propagation propagation;
    private final List capturedContext;
    
    protected ContextualObject(List> contextVars, 
                               ContextTrampoline.Propagation propagation, 
                               List capturedContext) {
        
        this.contextVars = null == contextVars ? 
            Collections.emptyList() : 
            Collections.unmodifiableList(contextVars);
            
        this.propagation = propagation == null ? ContextTrampoline.Propagation.OPTIMIZED : propagation;
        
        this.capturedContext = null == capturedContext ?
            Collections.emptyList() : 
            Collections.unmodifiableList(capturedContext);        
    }
    
    protected final List applyCapturedContext() {
        List originalContext = ContextTrampoline.Propagation.STRICT.equals(propagation) ? 
            ContextTrampoline.captureContext(contextVars) : Collections.nCopies(contextVars.size(), null);
        restoreContext(capturedContext);
        return originalContext;
    }
    
    protected final void restoreContext(List contextState) {
        Iterator> vars = contextVars.iterator();
        Iterator values = contextState.iterator();
        while (vars.hasNext() && values.hasNext()) {
            @SuppressWarnings("unchecked")
            ContextVar contextVar = (ContextVar)vars.next();
            Object contextVal = values.next();
            if (null == contextVal) {
                contextVar.remove();
            } else {
                contextVar.set(contextVal);
            }
        }
    }    

}