-
Notifications
You must be signed in to change notification settings - Fork 26
/
BaseDirectories.java
343 lines (326 loc) · 10.9 KB
/
BaseDirectories.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package dev.dirs;
import static dev.dirs.Util.*;
/** {@code BaseDirectories} provides paths of user-invisible standard directories, following the conventions of the operating system the library is running on.
* <p>
* To compute the location of cache, config or data directories for individual projects or applications, use {@link ProjectDirectories} instead.
*
* <h2>Examples</h2>
* <p>
* All examples on this page are computed with a user named <em>Alice</em>.
* <p>
* Example of {@link ProjectDirectories#configDir} value in different operating systems:
* <ul>
* <li><b>Linux/BSD:</b> {@code /home/alice/.config}</li>
* <li><b>macOS:</b> {@code /Users/Alice/Library/Preferences}</li>
* <li><b>Windows:</b> {@code C:\Users\Alice\AppData\Roaming}</li>
* </ul>
*/
public final class BaseDirectories {
/** Returns the path to the user's home directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $HOME}</td>
* <td>/home/alice</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}</td>
* <td>/Users/Alice</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Profile}}</td>
* <td>C:\Users\Alice\</td>
* </tr>
* </table>
*/
public final String homeDir;
/** Returns the path to the user's cache directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_CACHE_HOME} or {@code $HOME}/.cache</td>
* <td>/home/alice/.cache</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Library/Caches</td>
* <td>/Users/Alice/Library/Caches</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_LocalAppData}}\cache</td>
* <td>C:\Users\Alice\AppData\Local</td>
* </tr>
* </table>
*/
public final String cacheDir;
/** Returns the path to the user's configuration directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_CONFIG_HOME} or {@code $HOME}/.config</td>
* <td>/home/alice/.config</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Library/Application Support</td>
* <td>/Users/Alice/Library/Application Support</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_RoamingAppData}}</td>
* <td>C:\Users\Alice\AppData\Roaming</td>
* </tr>
* </table>
*/
public final String configDir;
/** Returns the path to the user's data directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_DATA_HOME} or {@code $HOME}/.local/share</td>
* <td>/home/alice/.local/share</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Library/Application Support</td>
* <td>/Users/Alice/Library/Application Support</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_RoamingAppData}}</td>
* <td>C:\Users\Alice\AppData\Roaming</td>
* </tr>
* </table>
*/
public final String dataDir;
/** Returns the path to the user's local data directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_DATA_HOME} or {@code $HOME}/.local/share</td>
* <td>/home/alice/.local/share</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Library/Application Support</td>
* <td>/Users/Alice/Library/Application Support</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_LocalAppData}}</td>
* <td>C:\Users\Alice\AppData\Local</td>
* </tr>
* </table>
*/
public final String dataLocalDir;
/** Returns the path to the user's executable directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_BIN_HOME} or {@code $XDG_DATA_HOME}/../bin or {@code $HOME}/.local/bin</td>
* <td>/home/alice/.local/bin</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>–</td>
* <td>{@code null}</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>–</td>
* <td>{@code null}</td>
* </tr>
* </table>
*/
public final String executableDir;
/** Returns the path to the user's preference directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_CONFIG_HOME} or {@code $HOME}/.config</td>
* <td>/home/alice/.config</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Library/Preferences</td>
* <td>/Users/Alice/Library/Preferences</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_RoamingAppData}}</td>
* <td>C:\Users\Alice\AppData\Roaming</td>
* </tr>
* </table>
*/
public final String preferenceDir;
/** Returns the path to the user's runtime directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_RUNTIME_DIR}</td>
* <td>/run/user/1001/</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>–</td>
* <td>{@code null}</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>–</td>
* <td>{@code null}</td>
* </tr>
* </table>
*/
public final String runtimeDir;
/** Creates a new {@code BaseDirectories} instance.
* <p>
* The instance is an immutable snapshot of the state of the system at the time this method is invoked.
* Subsequent changes to the state of the system are not reflected in instances created prior to such a change.
*
* @return A new {@code BaseDirectories} instance.
*/
public static BaseDirectories get() {
return new BaseDirectories();
}
private BaseDirectories() {
switch (operatingSystem) {
case LIN:
case BSD:
case SOLARIS:
case IBMI:
case AIX:
homeDir = System.getProperty("user.home");
cacheDir = defaultIfNullOrEmpty(System.getenv("XDG_CACHE_HOME"), homeDir, "/.cache");
configDir = defaultIfNullOrEmpty(System.getenv("XDG_CONFIG_HOME"), homeDir, "/.config");
dataDir = defaultIfNullOrEmpty(System.getenv("XDG_DATA_HOME"), homeDir, "/.local/share");
dataLocalDir = dataDir;
executableDir = linuxExecutableDir(homeDir, dataDir);
preferenceDir = configDir;
runtimeDir = linuxRuntimeDir(null);
break;
case MAC:
homeDir = System.getProperty("user.home");
cacheDir = homeDir + "/Library/Caches/";
configDir = homeDir + "/Library/Application Support/";
dataDir = configDir;
dataLocalDir = configDir;
executableDir = null;
preferenceDir = homeDir + "/Library/Preferences/";
runtimeDir = null;
break;
case WIN:
String[] winDirs = getWinDirs("5E6C858F-0E22-4760-9AFE-EA3317B67173", "3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
homeDir = winDirs[0];
dataDir = winDirs[1];
dataLocalDir = winDirs[2];
configDir = dataDir;
cacheDir = dataLocalDir;
executableDir = null;
preferenceDir = configDir;
runtimeDir = null;
break;
default:
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + operatingSystemName);
}
}
@Override
public String toString() {
return "BaseDirectories (" + operatingSystemName + "):\n" +
" homeDir = '" + homeDir + "'\n" +
" cacheDir = '" + cacheDir + "'\n" +
" configDir = '" + configDir + "'\n" +
" dataDir = '" + dataDir + "'\n" +
" dataLocalDir = '" + dataLocalDir + "'\n" +
" executableDir = '" + executableDir + "'\n" +
" preferenceDir = '" + preferenceDir + "'\n" +
" runtimeDir = '" + runtimeDir + "'\n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseDirectories that = (BaseDirectories) o;
if (homeDir != null ? !homeDir .equals(that.homeDir) : that.homeDir != null)
return false;
if (cacheDir != null ? !cacheDir .equals(that.cacheDir) : that.cacheDir != null)
return false;
if (configDir != null ? !configDir .equals(that.configDir) : that.configDir != null)
return false;
if (dataDir != null ? !dataDir .equals(that.dataDir) : that.dataDir != null)
return false;
if (dataLocalDir != null ? !dataLocalDir .equals(that.dataLocalDir) : that.dataLocalDir != null)
return false;
if (executableDir != null ? !executableDir.equals(that.executableDir) : that.executableDir != null)
return false;
if (preferenceDir != null ? !preferenceDir.equals(that.preferenceDir) : that.preferenceDir != null)
return false;
if (runtimeDir != null ? !runtimeDir .equals(that.runtimeDir) : that.runtimeDir != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = 0;
result = 31 * result + (homeDir != null ? homeDir .hashCode() : 0);
result = 31 * result + (cacheDir != null ? cacheDir .hashCode() : 0);
result = 31 * result + (configDir != null ? configDir .hashCode() : 0);
result = 31 * result + (dataDir != null ? dataDir .hashCode() : 0);
result = 31 * result + (dataLocalDir != null ? dataLocalDir .hashCode() : 0);
result = 31 * result + (executableDir != null ? executableDir.hashCode() : 0);
result = 31 * result + (preferenceDir != null ? preferenceDir.hashCode() : 0);
result = 31 * result + (runtimeDir != null ? runtimeDir .hashCode() : 0);
return result;
}
}