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

com.redis.riot.file.AbstractRegistry Maven / Gradle / Ivy

There is a newer version: 4.1.3
Show newest version
package com.redis.riot.file;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.core.io.Resource;

public abstract class AbstractRegistry {

	private final Map extensions = new HashMap<>();
	private final Map factories = new HashMap<>();

	public void register(FileType fileType, T factory) {
		factories.put(fileType, factory);
		if (fileType != null) {
			for (String extension : fileType.getExtensions()) {
				extensions.put(extension, fileType);
			}
		}
	}

	protected T factory(Resource resource, FileOptions options) throws IOException {
		FileType fileType = fileType(resource, options);
		if (fileType == null) {
			throw new UnknownFileTypeException(resource.getFilename());
		}
		T factory = factories.get(fileType);
		if (factory == null) {
			new UnsupportedFileTypeException(fileType);
		}
		return factory;
	}

	private FileType fileType(Resource resource, FileOptions options) {
		if (options.getFileType() == null) {
			return extensions.get(Files.extension(resource.getFilename()));
		}
		return options.getFileType();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy