package p.studio.compiler.utilities;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

@FunctionalInterface
public interface SourceProviderFactory {
    SourceProvider create(Path path);

    static SourceProviderFactory filesystem() {
        return path -> new SourceProvider() {

            private volatile byte[] cachedBytes = null;

            @Override
            public byte[] read() throws IOException {
                if (Objects.isNull(cachedBytes)) {
                    cachedBytes = Files.readAllBytes(path);
                }
                return cachedBytes;
            }
        };
    }

    // more providers can be added here, e.g., in-memory, etc.
}
