63c2933fe616d793d78ed901ff7be560eb88a702
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / core / DataDirectory.java
1 /*
2  * DemosceneMusic - DataDirectory.java - Copyright © 2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.core;
19
20 import java.io.File;
21
22 /**
23  * Helper for managing the data directory.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class DataDirectory {
28
29         /** The path of the data directory. */
30         private final String dataDirectory;
31
32         /**
33          * Creates a new data directory helper.
34          *
35          * @param dataDirectory
36          *            The path of the data directory
37          */
38         public DataDirectory(String dataDirectory) {
39                 this.dataDirectory = dataDirectory;
40         }
41
42         /**
43          * Returns the absolute path of the file with the given ID.
44          *
45          * @param id
46          *            The ID of the file
47          * @return The absolute path of the file
48          */
49         public String getPath(String id) {
50                 return new File(dataDirectory, id.toLowerCase().replace('-', '/')).getAbsolutePath();
51         }
52
53         /**
54          * Returns the file for the given ID. The returned {@link File} is ready to
55          * be read from or written to, all necessary directories have been created.
56          *
57          * @param id
58          *            The ID of the file
59          * @return The file
60          */
61         public File getFile(String id) {
62                 File file = new File(dataDirectory, id.toLowerCase().replace('-', '/'));
63                 file.mkdirs();
64                 return file;
65         }
66
67 }