com.softicar.platform.common.io.resource.static_.StaticResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.io.resource.static_;
import com.softicar.platform.common.io.mime.IMimeType;
import com.softicar.platform.common.io.mime.MimeType;
import com.softicar.platform.common.io.resource.hash.AbstractHashableResource;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Optional;
/**
* Default implementation of {@link IStaticResource}.
*
* @author Alexander Schmidt
* @author Oliver Richers
*/
public class StaticResource extends AbstractHashableResource implements IStaticResource {
private final Class anchorClass;
private final String filename;
private final Charset charset;
/**
* Constructs a new {@link StaticResource}.
*
* @param anchorClass
* a {@link Class} that resides in the package in which the
* resource file is located (never null)
* @param filename
* the name of the resource file (never null)
* @param charset
* the character set of the resource file, if applicable (may be
* null)
*/
public StaticResource(Class anchorClass, String filename, Charset charset) {
this.anchorClass = Objects.requireNonNull(anchorClass);
this.filename = Objects.requireNonNull(filename);
this.charset = charset;
}
@Override
public Class getAnchorClass() {
return anchorClass;
}
@Override
public InputStream getResourceAsStream() {
return anchorClass.getResourceAsStream(filename);
}
@Override
public IMimeType getMimeType() {
return MimeType.getByFilenameOrDefault(filename);
}
@Override
public Optional getCharset() {
return Optional.ofNullable(charset);
}
@Override
public Optional getFilename() {
return Optional.of(filename);
}
}