| 1 | package fr.sii.ogham.core.builder.resolution; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.Collections; | |
| 5 | import java.util.Comparator; | |
| 6 | import java.util.HashMap; | |
| 7 | import java.util.List; | |
| 8 | import java.util.Map; | |
| 9 | ||
| 10 | import fr.sii.ogham.core.builder.Builder; | |
| 11 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 12 | import fr.sii.ogham.core.builder.env.EnvironmentBuilder; | |
| 13 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
| 14 | ||
| 15 | /** | |
| 16 | * Resource resolution is used many times. This implementation helps configure | |
| 17 | * resource resolution. | |
| 18 | * | |
| 19 | * @author Aurélien Baudet | |
| 20 | * | |
| 21 | * @param <FLUENT> | |
| 22 | * The type of the helped instance. This is needed to have the right | |
| 23 | * return type for fluent chaining | |
| 24 | */ | |
| 25 | @SuppressWarnings("squid:S00119") | |
| 26 | public class ResourceResolutionBuilderHelper<FLUENT extends ResourceResolutionBuilder<FLUENT>> implements ResourceResolutionBuilder<FLUENT> { | |
| 27 | private final BuildContext buildContext; | |
| 28 | private ClassPathResolutionBuilder<FLUENT> classPath; | |
| 29 | private FileResolutionBuilder<FLUENT> file; | |
| 30 | private StringResolutionBuilder<FLUENT> string; | |
| 31 | private List<ResourceResolver> customResolvers; | |
| 32 | private FLUENT fluent; | |
| 33 | ||
| 34 | /** | |
| 35 | * Initializes the helper with the fluent instance and the | |
| 36 | * {@link EnvironmentBuilder}. The fluent instance is used for chaining. It | |
| 37 | * indicates which type is returned. The {@link EnvironmentBuilder} is used | |
| 38 | * by sub-builders ( {@link ClassPathResolutionBuilder} and | |
| 39 | * {@link FileResolutionBuilder}) to evaluate properties when their build | |
| 40 | * methods are called. | |
| 41 | * | |
| 42 | * @param fluent | |
| 43 | * the instance used for chaining calls | |
| 44 | * @param buildContext | |
| 45 | * for property resolution | |
| 46 | */ | |
| 47 | public ResourceResolutionBuilderHelper(FLUENT fluent, BuildContext buildContext) { | |
| 48 | super(); | |
| 49 | this.fluent = fluent; | |
| 50 | this.buildContext = buildContext; | |
| 51 | customResolvers = new ArrayList<>(); | |
| 52 | } | |
| 53 | ||
| 54 | @Override | |
| 55 | public ClassPathResolutionBuilder<FLUENT> classpath() { | |
| 56 |
1
1. classpath : negated conditional → RUN_ERROR |
if (classPath == null) { |
| 57 | classPath = new ClassPathResolutionBuilder<>(fluent, buildContext); | |
| 58 | } | |
| 59 |
1
1. classpath : replaced return value with null for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper::classpath → RUN_ERROR |
return classPath; |
| 60 | } | |
| 61 | ||
| 62 | @Override | |
| 63 | public FileResolutionBuilder<FLUENT> file() { | |
| 64 |
1
1. file : negated conditional → RUN_ERROR |
if (file == null) { |
| 65 | file = new FileResolutionBuilder<>(fluent, buildContext); | |
| 66 | } | |
| 67 |
1
1. file : replaced return value with null for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper::file → RUN_ERROR |
return file; |
| 68 | } | |
| 69 | ||
| 70 | @Override | |
| 71 | public StringResolutionBuilder<FLUENT> string() { | |
| 72 |
1
1. string : negated conditional → RUN_ERROR |
if (string == null) { |
| 73 | string = new StringResolutionBuilder<>(fluent, buildContext); | |
| 74 | } | |
| 75 |
1
1. string : replaced return value with null for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper::string → RUN_ERROR |
return string; |
| 76 | } | |
| 77 | ||
| 78 | @Override | |
| 79 | public FLUENT resolver(ResourceResolver resolver) { | |
| 80 | customResolvers.add(resolver); | |
| 81 |
1
1. resolver : replaced return value with null for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper::resolver → NO_COVERAGE |
return fluent; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * For each kind of lookup, stores the list of registered lookups. | |
| 86 | * | |
| 87 | * @return map of lookups indexed by lookup type. | |
| 88 | */ | |
| 89 | public Map<String, List<String>> getAllLookups() { | |
| 90 | Map<String, List<String>> all = new HashMap<>(); | |
| 91 |
1
1. getAllLookups : negated conditional → RUN_ERROR |
if (string != null) { |
| 92 | all.put("string", string.getLookups()); | |
| 93 | } | |
| 94 |
1
1. getAllLookups : negated conditional → RUN_ERROR |
if (file != null) { |
| 95 | all.put("file", file.getLookups()); | |
| 96 | } | |
| 97 |
1
1. getAllLookups : negated conditional → RUN_ERROR |
if (classPath != null) { |
| 98 | all.put("classpath", classPath.getLookups()); | |
| 99 | } | |
| 100 |
1
1. getAllLookups : replaced return value with Collections.emptyMap for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper::getAllLookups → RUN_ERROR |
return all; |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Build the list of resource resolvers. | |
| 105 | * | |
| 106 | * <p> | |
| 107 | * The list is ordered to ensure that empty string lookup is always the last | |
| 108 | * registered. | |
| 109 | * </p> | |
| 110 | * | |
| 111 | * <p> | |
| 112 | * If some custom resolvers are registered, they are used before default | |
| 113 | * ones in the order they were registered. | |
| 114 | * </p> | |
| 115 | * | |
| 116 | * @return the list of resource resolvers | |
| 117 | */ | |
| 118 | public List<ResourceResolver> buildResolvers() { | |
| 119 | List<ResourceResolver> resolvers = new ArrayList<>(); | |
| 120 | resolvers.addAll(customResolvers); | |
| 121 | // ensure that default lookup is always the last registered | |
| 122 | List<ResolverHelper> helpers = new ArrayList<>(); | |
| 123 |
1
1. buildResolvers : negated conditional → RUN_ERROR |
if (classPath != null) { |
| 124 | helpers.add(new ResolverHelper(classPath.getLookups(), classPath)); | |
| 125 | } | |
| 126 |
1
1. buildResolvers : negated conditional → RUN_ERROR |
if (file != null) { |
| 127 | helpers.add(new ResolverHelper(file.getLookups(), file)); | |
| 128 | } | |
| 129 |
1
1. buildResolvers : negated conditional → RUN_ERROR |
if (string != null) { |
| 130 | helpers.add(new ResolverHelper(string.getLookups(), string)); | |
| 131 | } | |
| 132 |
1
1. buildResolvers : removed call to java/util/Collections::sort → RUN_ERROR |
Collections.sort(helpers, new PrefixComparator()); |
| 133 | for (ResolverHelper helper : helpers) { | |
| 134 |
1
1. buildResolvers : removed call to fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper$ResolverHelper::register → RUN_ERROR |
helper.register(resolvers); |
| 135 | } | |
| 136 |
1
1. buildResolvers : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper::buildResolvers → RUN_ERROR |
return resolvers; |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * Order prefixes in order to ensure that empty string lookup is registered | |
| 141 | * at the end. | |
| 142 | * | |
| 143 | * @author Aurélien Baudet | |
| 144 | * | |
| 145 | */ | |
| 146 | private static class PrefixComparator implements Comparator<ResolverHelper> { | |
| 147 | @Override | |
| 148 | public int compare(ResolverHelper o1, ResolverHelper o2) { | |
| 149 | StringBuilder concat1 = new StringBuilder(); | |
| 150 | for (String prefix : o1.getPrefixes()) { | |
| 151 |
1
1. compare : negated conditional → RUN_ERROR |
if (prefix.isEmpty()) { |
| 152 |
1
1. compare : replaced int return with 0 for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper$PrefixComparator::compare → NO_COVERAGE |
return 1; |
| 153 | } | |
| 154 | concat1.append(prefix); | |
| 155 | } | |
| 156 | StringBuilder concat2 = new StringBuilder(); | |
| 157 | for (String prefix : o2.getPrefixes()) { | |
| 158 |
1
1. compare : negated conditional → RUN_ERROR |
if (prefix.isEmpty()) { |
| 159 |
1
1. compare : replaced int return with 0 for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper$PrefixComparator::compare → RUN_ERROR |
return -1; |
| 160 | } | |
| 161 | concat2.append(prefix); | |
| 162 | } | |
| 163 |
1
1. compare : replaced int return with 0 for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper$PrefixComparator::compare → RUN_ERROR |
return concat1.toString().compareTo(concat2.toString()); |
| 164 | } | |
| 165 | ||
| 166 | } | |
| 167 | ||
| 168 | private static class ResolverHelper { | |
| 169 | private final List<String> prefixes; | |
| 170 | private final Builder<ResourceResolver> builder; | |
| 171 | ||
| 172 | public ResolverHelper(List<String> prefixes, Builder<ResourceResolver> builder) { | |
| 173 | super(); | |
| 174 | this.prefixes = prefixes; | |
| 175 | this.builder = builder; | |
| 176 | } | |
| 177 | ||
| 178 | public void register(List<ResourceResolver> resolvers) { | |
| 179 | resolvers.add(builder.build()); | |
| 180 | } | |
| 181 | ||
| 182 | public List<String> getPrefixes() { | |
| 183 |
1
1. getPrefixes : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/resolution/ResourceResolutionBuilderHelper$ResolverHelper::getPrefixes → RUN_ERROR |
return prefixes; |
| 184 | } | |
| 185 | } | |
| 186 | } | |
Mutations | ||
| 56 |
1.1 |
|
| 59 |
1.1 |
|
| 64 |
1.1 |
|
| 67 |
1.1 |
|
| 72 |
1.1 |
|
| 75 |
1.1 |
|
| 81 |
1.1 |
|
| 91 |
1.1 |
|
| 94 |
1.1 |
|
| 97 |
1.1 |
|
| 100 |
1.1 |
|
| 123 |
1.1 |
|
| 126 |
1.1 |
|
| 129 |
1.1 |
|
| 132 |
1.1 |
|
| 134 |
1.1 |
|
| 136 |
1.1 |
|
| 151 |
1.1 |
|
| 152 |
1.1 |
|
| 158 |
1.1 |
|
| 159 |
1.1 |
|
| 163 |
1.1 |
|
| 183 |
1.1 |