br.com.objectos.way.etc.Resource Maven / Gradle / Ivy
The newest version!
/*
* Resource.java criado em 04/01/2014
*
* Propriedade de Objectos Fábrica de Software LTDA.
* Reprodução parcial ou total proibida.
*/
package br.com.objectos.way.etc;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import br.com.objectos.way.core.io.Directory;
import com.google.common.base.Objects;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.Resources;
/**
* @author [email protected] (Marcio Endo)
*/
class Resource {
private final String name;
private final String path;
private final String dest;
private Resource(String name, String path, String dest) {
this.name = name;
this.path = path;
this.dest = dest;
}
private Resource(Resource temp, String dest) {
this.name = temp.name;
this.path = temp.path;
this.dest = dest;
}
public static Resource add(String dir, String name) {
name = name.startsWith("/") ? name : "/" + name;
String path = dir + name;
path = path.replaceFirst("//", "/");
return new Resource(name, path, name);
}
public static Resource map(String dir, String source, String dest) {
Resource temp = add(dir, source);
return new Resource(temp, dest);
}
public String getName() {
return name;
}
public ByteSource open(Class> contextClass) {
URL url = Resources.getResource(contextClass, path);
return Resources.asByteSource(url);
}
public File targetFile(Directory targetDir) throws IOException {
File file = targetDir.fileAt(dest);
Files.createParentDirs(file);
return file;
}
@Override
public final int hashCode() {
return name.hashCode();
}
@Override
public final boolean equals(final Object obj) {
if (obj == this) {
return true;
}
final Resource that = (Resource) obj;
return Objects.equal(this.name, that.name);
}
}