package org.implab.gradle.variants.sources.internal;

import org.implab.gradle.variants.sources.VariantSourcesExtension.NamingPolicySpec;

public class DefaultCompileUnitNamingPolicy implements NamingPolicySpec {
    private NameCollisionPolicy policy = NameCollisionPolicy.FAIL;
    private boolean policyApplied = false;

    public NameCollisionPolicy policy() {
        finalizePolicy();
        return policy;
    }

    public void finalizePolicy() {
        policyApplied = true;
    }

    @Override
    public void failOnNameCollision() {
        assertApplyOnce();
        policy = NameCollisionPolicy.FAIL;
    }

    @Override
    public void resolveNameCollision() {
        assertApplyOnce();
        policy = NameCollisionPolicy.RESOLVE;
    }

    private void assertApplyOnce() {
        if (policyApplied)
            throw new IllegalStateException("Naming policy already applied");
        policyApplied = true;

    }
}
