Add data directory helper.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 05:48:37 +0000 (07:48 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 05:48:37 +0000 (07:48 +0200)
src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java b/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java
new file mode 100644 (file)
index 0000000..63c2933
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * DemosceneMusic - DataDirectory.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.core;
+
+import java.io.File;
+
+/**
+ * Helper for managing the data directory.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DataDirectory {
+
+       /** The path of the data directory. */
+       private final String dataDirectory;
+
+       /**
+        * Creates a new data directory helper.
+        *
+        * @param dataDirectory
+        *            The path of the data directory
+        */
+       public DataDirectory(String dataDirectory) {
+               this.dataDirectory = dataDirectory;
+       }
+
+       /**
+        * Returns the absolute path of the file with the given ID.
+        *
+        * @param id
+        *            The ID of the file
+        * @return The absolute path of the file
+        */
+       public String getPath(String id) {
+               return new File(dataDirectory, id.toLowerCase().replace('-', '/')).getAbsolutePath();
+       }
+
+       /**
+        * Returns the file for the given ID. The returned {@link File} is ready to
+        * be read from or written to, all necessary directories have been created.
+        *
+        * @param id
+        *            The ID of the file
+        * @return The file
+        */
+       public File getFile(String id) {
+               File file = new File(dataDirectory, id.toLowerCase().replace('-', '/'));
+               file.mkdirs();
+               return file;
+       }
+
+}