<commit_before>/*
 *
 *  Copyright 2012-2014 Eurocommercial Properties NV
 *
 *
 *  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 org.apache.isis.applib.fixturescripts;

import org.apache.isis.applib.annotation.Programmatic;

public abstract class BuilderScriptAbstract<T,F extends BuilderScriptAbstract<T,F>>
        extends FixtureScript implements FixtureScriptWithExecutionStrategy {

    private final FixtureScripts.MultipleExecutionStrategy executionStrategy;

    /**
     * Typically we expect builders to have value semantics, so this is provided as a convenience.
     */
    protected BuilderScriptAbstract() {
        this(FixtureScripts.MultipleExecutionStrategy.EXECUTE_ONCE_BY_VALUE);
    }

    protected BuilderScriptAbstract(final FixtureScripts.MultipleExecutionStrategy executionStrategy) {
        this.executionStrategy = executionStrategy;
    }

    @Override
    public FixtureScripts.MultipleExecutionStrategy getMultipleExecutionStrategy() {
        return executionStrategy;
    }

    @Programmatic
    public F build(
            final FixtureScript parentFixtureScript,
            ExecutionContext executionContext) {

        // returns the fixture script that is run
        // (either this one, or possibly one previously executed).
        return (F)executionContext.executeChildT(parentFixtureScript, this);
    }

    public abstract T getObject();

    protected <E extends EnumWithBuilderScript<T, F>, T, F extends BuilderScriptAbstract<T,F>> T objectFor(
            final E datum,
            final FixtureScript.ExecutionContext ec) {
        if(datum == null) {
            return null;
        }
        final F fixtureScript = datum.toFixtureScript();
        return ec.executeChildT(this, fixtureScript).getObject();
    }


}
<commit_msg>ISIS-1784: extends BuilderScriptAbstract further, with new set(...) method to allow prereqs to be run within the execute<commit_after>/*
 *
 *  Copyright 2012-2014 Eurocommercial Properties NV
 *
 *
 *  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 org.apache.isis.applib.fixturescripts;

import java.util.List;

import com.google.common.collect.Lists;

import org.apache.isis.applib.annotation.Programmatic;

public abstract class BuilderScriptAbstract<T,F extends BuilderScriptAbstract<T,F>>
        extends FixtureScript implements FixtureScriptWithExecutionStrategy {

    private final FixtureScripts.MultipleExecutionStrategy executionStrategy;

    /**
     * Typically we expect builders to have value semantics, so this is provided as a convenience.
     */
    protected BuilderScriptAbstract() {
        this(FixtureScripts.MultipleExecutionStrategy.EXECUTE_ONCE_BY_VALUE);
    }

    protected BuilderScriptAbstract(final FixtureScripts.MultipleExecutionStrategy executionStrategy) {
        this.executionStrategy = executionStrategy;
    }

    @Override
    public FixtureScripts.MultipleExecutionStrategy getMultipleExecutionStrategy() {
        return executionStrategy;
    }

    @Programmatic
    public F build(
            final FixtureScript parentFixtureScript,
            ExecutionContext executionContext) {

        // returns the fixture script that is run
        // (either this one, or possibly one previously executed).
        return (F)executionContext.executeChildT(parentFixtureScript, this);
    }

    @Override
    protected final void execute(final ExecutionContext executionContext) {

        final F onFixture = (F) BuilderScriptAbstract.this;
        for (final Block<T,F> prereq : prereqs) {
            prereq.execute(onFixture, executionContext);
        }

        doExecute(executionContext);
    }

    protected abstract void doExecute(final ExecutionContext executionContext);

    public abstract T getObject();

    public <E extends EnumWithBuilderScript<T, F>, T, F extends BuilderScriptAbstract<T,F>> T objectFor(
            final E datum,
            final FixtureScript.ExecutionContext ec) {
        if(datum == null) {
            return null;
        }
        final F fixtureScript = datum.toFixtureScript();
        return ec.executeChildT(this, fixtureScript).getObject();
    }

    private final List<Block> prereqs = Lists.newArrayList();
    public  F set(Block<T,F> prereq) {
        prereqs.add(prereq);
        return (F)this;
    }

    public interface Block<T,F extends BuilderScriptAbstract<T,F>> {
        void execute(final F onFixture, final ExecutionContext executionContext);
    }

}

{'fraction_non_alphanumeric': 0.07242440702516748, 'fraction_numerical': 0.005069708491761723, 'mean_word_length': 3.295489891135303, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7325489', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1391, 'n_words': 452}