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

io.cucumber.cucumberexpressions.PatternCompilerProvider Maven / Gradle / Ivy

Go to download

Cucumber Expressions are simple patterns for matching Step Definitions with Gherkin steps

There is a newer version: 18.0.1
Show newest version
package io.cucumber.cucumberexpressions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

final class PatternCompilerProvider {
    // visible from tests
    static PatternCompiler service;

    private PatternCompilerProvider() {
    }

    static synchronized PatternCompiler getCompiler() {
        if (service == null) {
            ServiceLoader loader = ServiceLoader.load(PatternCompiler.class);
            Iterator iterator = loader.iterator();
            findPatternCompiler(iterator);
        }
        return service;
    }

    static void findPatternCompiler(Iterator iterator) {
        if (iterator.hasNext()) {
            service = iterator.next();
            if (iterator.hasNext()) {
                throwMoreThanOneCompilerException(iterator);
            }
        } else {
            service = new DefaultPatternCompiler();
        }
    }

    private static void throwMoreThanOneCompilerException(Iterator iterator) {
        List> allCompilers = new ArrayList<>();
        allCompilers.add(service.getClass());
        while (iterator.hasNext()) {
            allCompilers.add(iterator.next().getClass());
        }
        throw new IllegalStateException("More than one PatternCompiler: " + allCompilers);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy