org.protelis.lang.interpreter.impl.AbstractPersistedTree Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protelis-interpreter Show documentation
Show all versions of protelis-interpreter Show documentation
The Protelis language interpreter
/*
* Copyright (C) 2021, Danilo Pianini and contributors listed in the project's build.gradle.kts or pom.xml file.
*
* This file is part of Protelis, and is distributed under the terms of the GNU General Public License,
* with a linking exception, as described in the file LICENSE.txt in this project's top directory.
*/
package org.protelis.lang.interpreter.impl;
import java.util.List;
import java.util.function.Supplier;
import org.protelis.lang.interpreter.ProtelisAST;
import org.protelis.lang.loading.Metadata;
import org.protelis.vm.ExecutionContext;
/**
* Basic implementation of a persisted tree.
*
* @param
* Superscript type
* @param
* Annotation type
*/
public abstract class AbstractPersistedTree extends AbstractProtelisAST {
private static final long serialVersionUID = 1;
/**
* @param metadata
* A {@link Metadata} object containing information about the code that generated this AST node.
* @param branches
* branches of this {@link AbstractPersistedTree}
*/
protected AbstractPersistedTree(final Metadata metadata, final ProtelisAST>... branches) {
super(metadata, branches);
}
/**
* @param metadata
* A {@link Metadata} object containing information about the code that generated this AST node.
* @param branches
* branches of this {@link AbstractPersistedTree}
*/
protected AbstractPersistedTree(final Metadata metadata, final List> branches) {
super(metadata, branches);
}
/**
* Retrieves the function state from last round, or produces a new state otherwise.
*
* @param context the {@link ExecutionContext}
* @param ifAbsent a 0-ary function producing the value if none is stored
* @return the previous state, if present, or the state computed by ifAbsent otherwise
*/
protected final S loadState(final ExecutionContext context, final Supplier ifAbsent) {
return context.getPersistent(ifAbsent);
}
/**
* Stores the function state for the next round.
*
* @param context the {@link ExecutionContext}
* @param obj the state
*/
protected final void saveState(final ExecutionContext context, final S obj) {
context.setPersistent(obj);
}
/**
* {@inheritDoc}
*/
@Override
public abstract String toString();
}