必要に応じてインスタンスを生成するようにした

This commit is contained in:
r-ca 2024-01-09 15:27:42 +09:00
parent 65a25ad228
commit 01e111bdd5
No known key found for this signature in database
GPG Key ID: 6A72911AC73464A9

View File

@ -183,26 +183,31 @@ public class FileManagerImpl implements FileManager {
return this.setPath(resolvedPath); return this.setPath(resolvedPath);
} }
@Override // Internal
public FileManager createFile() throws IOException { private void createFileInternal(Path path) throws IOException {
try { try {
if (this.autoCreateParent) { if (this.autoCreateParent) {
if (!this.path.getParent().toFile().exists()) { if (!path.getParent().toFile().exists()) {
Files.createDirectories(this.path.getParent()); Files.createDirectories(path.getParent());
} }
} }
Files.createFile(this.path); Files.createFile(path);
} catch (Exception e) { } catch (Exception e) {
logger.error("createFile", e.getMessage()); logger.error("createFileInternal", e.getMessage());
throw new IOException("Failed to create file"); throw new IOException("Failed to create file");
} }
}
@Override
public FileManager createFile() throws IOException {
this.createFileInternal(this.path);
return this; return this;
} }
@Override @Override
public FileManager createFile(String fileName) throws IOException { // pathが書き換わってしまうのは想像できない挙動かも public FileManager createFile(String fileName) throws IOException { // pathが書き換わってしまうのは想像できない挙動かも
this.resolve(fileName); this.createFileInternal(this.resolveStringPath(fileName));
return this.createFile(); return this;
} }
@Override @Override
@ -215,29 +220,37 @@ public class FileManagerImpl implements FileManager {
@Override @Override
public FileManager createFileIfNotExist(String fileName) throws IOException { public FileManager createFileIfNotExist(String fileName) throws IOException {
this.resolve(fileName); if (!this.isExist(fileName)) {
return this.createFileIfNotExist(); this.createFile(fileName);
}
return this;
}
// Internal
private void createDirectoryInternal(Path path) throws IOException {
try {
if (this.autoCreateParent) {
if (!path.getParent().toFile().exists()) {
Files.createDirectories(path.getParent());
}
}
Files.createDirectory(path);
} catch (Exception e) {
logger.error("createDirectoryInternal", e.getMessage());
throw new IOException("Failed to create directory");
}
} }
@Override @Override
public FileManager createDirectory() throws IOException { public FileManager createDirectory() throws IOException {
try { this.createDirectoryInternal(this.path);
if (this.autoCreateParent) { // configとして管理する必要はないかも
Files.createDirectories(this.path);
} else {
Files.createDirectory(this.path);
}
} catch (Exception e) {
logger.error("createDirectory", e.getMessage());
throw new IOException("Failed to create directory");
}
return this; return this;
} }
@Override @Override
public FileManager createDirectory(String directoryName) throws IOException { public FileManager createDirectory(String directoryName) throws IOException {
this.resolve(directoryName); this.createDirectoryInternal(this.resolveStringPath(directoryName));
return this.createDirectory(); return this;
} }
@Override @Override
@ -250,18 +263,20 @@ public class FileManagerImpl implements FileManager {
@Override @Override
public FileManager createDirectoryIfNotExist(String directoryName) throws IOException { public FileManager createDirectoryIfNotExist(String directoryName) throws IOException {
this.resolve(directoryName); if (!this.isExist(directoryName)) {
return this.createDirectoryIfNotExist(); this.createDirectory(directoryName);
}
return this;
} }
// Internal // Internal
private void saveXmlInternal(Document document) throws IOException { private void saveXmlInternal(Document document, Path path) throws IOException {
try { try {
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document); DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(this.path.toFile()); StreamResult result = new StreamResult(path.toFile());
transformer.transform(source, result); transformer.transform(source, result);
} catch (Exception e) { } catch (Exception e) {
@ -270,9 +285,9 @@ public class FileManagerImpl implements FileManager {
throw new IOException("Failed to save xml"); throw new IOException("Failed to save xml");
} }
} }
private void saveBitmapInternal(Bitmap bitmap) throws IOException { private void saveBitmapInternal(Bitmap bitmap, Path path) throws IOException {
try { try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Files.newOutputStream(this.path)); bitmap.compress(Bitmap.CompressFormat.PNG, 100, Files.newOutputStream(path));
} catch (Exception e) { } catch (Exception e) {
logger.error("saveBitmapInternal", e.getMessage()); logger.error("saveBitmapInternal", e.getMessage());
throw new IOException("Failed to save bitmap"); throw new IOException("Failed to save bitmap");
@ -281,40 +296,38 @@ public class FileManagerImpl implements FileManager {
@Override @Override
public void saveXml(Document document, String fileName) throws IOException { public void saveXml(Document document, String fileName) throws IOException {
this.resolve(fileName); this.saveXmlInternal(document, this.resolveStringPath(fileName));
this.saveXmlInternal(document);
} }
@Override @Override
public void saveXml(Document document) throws IOException { public void saveXml(Document document) throws IOException {
this.saveXmlInternal(document); this.saveXmlInternal(document, this.path);
} }
@Override @Override
public void saveBitmap(Bitmap bitmap, String fileName) throws IOException { public void saveBitmap(Bitmap bitmap, String fileName) throws IOException {
this.resolve(fileName); this.saveBitmapInternal(bitmap, this.resolveStringPath(fileName));
this.saveBitmapInternal(bitmap);
} }
@Override @Override
public void saveBitmap(Bitmap bitmap) throws IOException { public void saveBitmap(Bitmap bitmap) throws IOException {
this.saveBitmapInternal(bitmap); this.saveBitmapInternal(bitmap, this.path);
} }
// Internal // Internal
private Document loadXmlInternal() throws IOException { private Document loadXmlInternal(Path path) throws IOException {
try { try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(Files.newInputStream(this.path)); return builder.parse(Files.newInputStream(path));
} catch (Exception e) { } catch (Exception e) {
logger.error("loadXmlInternal", e.getMessage()); logger.error("loadXmlInternal", e.getMessage());
throw new IOException("Failed to load xml"); throw new IOException("Failed to load xml");
} }
} }
private Bitmap loadBitmapInternal() throws IOException { private Bitmap loadBitmapInternal(Path path) throws IOException {
try { try {
return BitmapFactory.decodeFile(this.path.toString()); return BitmapFactory.decodeFile(path.toString());
} catch (Exception e) { } catch (Exception e) {
logger.error("loadBitmapInternal", e.getMessage()); logger.error("loadBitmapInternal", e.getMessage());
throw new IOException("Failed to load bitmap"); throw new IOException("Failed to load bitmap");
@ -323,23 +336,21 @@ public class FileManagerImpl implements FileManager {
@Override @Override
public Document loadXml(String fileName) throws IOException { public Document loadXml(String fileName) throws IOException {
this.resolve(fileName); return this.loadXmlInternal(this.resolveStringPath(fileName));
return this.loadXmlInternal();
} }
@Override @Override
public Document loadXml() throws IOException { public Document loadXml() throws IOException {
return this.loadXmlInternal(); return this.loadXmlInternal(this.path);
} }
@Override @Override
public Bitmap loadBitmap(String fileName) throws IOException { public Bitmap loadBitmap(String fileName) throws IOException {
this.resolve(fileName); return this.loadBitmapInternal(this.resolveStringPath(fileName));
return this.loadBitmapInternal();
} }
@Override @Override
public Bitmap loadBitmap() throws IOException { public Bitmap loadBitmap() throws IOException {
return this.loadBitmapInternal(); return this.loadBitmapInternal(this.path);
} }
} }