class EntropyInjector {

	@VisibleForTesting
	static Path resolveEntropy(Path path, EntropyInjectingFileSystem efs, boolean injectEntropy) throws IOException {
		final String entropyInjectionKey = efs.getEntropyInjectionKey();

		if (entropyInjectionKey == null) {
			return path;
		}
		else {
			final URI originalUri = path.toUri();
			final String checkpointPath = originalUri.getPath();

			final int indexOfKey = checkpointPath.indexOf(entropyInjectionKey);
			if (indexOfKey == -1) {
				return path;
			}
			else {
				final StringBuilder buffer = new StringBuilder(checkpointPath.length());
				buffer.append(checkpointPath, 0, indexOfKey);

				if (injectEntropy) {
					buffer.append(efs.generateEntropy());
				}

				buffer.append(checkpointPath, indexOfKey + entropyInjectionKey.length(), checkpointPath.length());

				final String rewrittenPath = buffer.toString();
				try {
					return new Path(new URI(
							originalUri.getScheme(),
							originalUri.getAuthority(),
							rewrittenPath,
							originalUri.getQuery(),
							originalUri.getFragment()).normalize());
				}
				catch (URISyntaxException e) {
					// this could only happen if the injected entropy string contains invalid characters
					throw new IOException("URI format error while processing path for entropy injection", e);
				}
			}
		}
	}

	private  EntropyInjector();

}