Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit c9cf98b

Browse files
committed
Split operating-specific functionality into separate files
1 parent 9e5e0e5 commit c9cf98b

File tree

9 files changed

+316
-477
lines changed

9 files changed

+316
-477
lines changed

src/main/java/dev/dirs/BaseDirectories.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.dirs;
22

3-
import static dev.dirs.Util.*;
3+
import dev.dirs.impl.Linux;
4+
import dev.dirs.impl.Util;
5+
import dev.dirs.impl.Windows;
46

57
/** {@code BaseDirectories} provides paths of user-invisible standard directories, following the conventions of the operating system the library is running on.
68
* <p>
@@ -247,22 +249,22 @@ public static BaseDirectories get() {
247249
}
248250

249251
private BaseDirectories() {
250-
switch (operatingSystem) {
251-
case LIN:
252-
case BSD:
253-
case SOLARIS:
254-
case IBMI:
255-
case AIX:
252+
switch (Constants.operatingSystem) {
253+
case Constants.LIN:
254+
case Constants.BSD:
255+
case Constants.SOLARIS:
256+
case Constants.IBMI:
257+
case Constants.AIX:
256258
homeDir = System.getProperty("user.home");
257-
cacheDir = defaultIfNullOrEmpty(System.getenv("XDG_CACHE_HOME"), homeDir, "/.cache");
258-
configDir = defaultIfNullOrEmpty(System.getenv("XDG_CONFIG_HOME"), homeDir, "/.config");
259-
dataDir = defaultIfNullOrEmpty(System.getenv("XDG_DATA_HOME"), homeDir, "/.local/share");
259+
cacheDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_CACHE_HOME"), homeDir, "/.cache");
260+
configDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_CONFIG_HOME"), homeDir, "/.config");
261+
dataDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_DATA_HOME"), homeDir, "/.local/share");
260262
dataLocalDir = dataDir;
261-
executableDir = linuxExecutableDir(homeDir, dataDir);
263+
executableDir = Linux.executableDir(homeDir, dataDir);
262264
preferenceDir = configDir;
263-
runtimeDir = linuxRuntimeDir(null);
265+
runtimeDir = Linux.runtimeDir(null);
264266
break;
265-
case MAC:
267+
case Constants.MAC:
266268
homeDir = System.getProperty("user.home");
267269
cacheDir = homeDir + "/Library/Caches/";
268270
configDir = homeDir + "/Library/Application Support/";
@@ -272,25 +274,24 @@ private BaseDirectories() {
272274
preferenceDir = homeDir + "/Library/Preferences/";
273275
runtimeDir = null;
274276
break;
275-
case WIN:
276-
String[] winDirs = getWinDirs("5E6C858F-0E22-4760-9AFE-EA3317B67173", "3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
277-
homeDir = winDirs[0];
278-
dataDir = winDirs[1];
279-
dataLocalDir = winDirs[2];
277+
case Constants.WIN:
278+
homeDir = Windows.getProfileDir();;
279+
dataDir = Windows.getRoamingAppDataDir();;
280+
dataLocalDir = Windows.getLocalAppDataDir();;
280281
configDir = dataDir;
281282
cacheDir = dataLocalDir;
282283
executableDir = null;
283284
preferenceDir = configDir;
284285
runtimeDir = null;
285286
break;
286287
default:
287-
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + operatingSystemName);
288+
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + Constants.operatingSystemName);
288289
}
289290
}
290291

291292
@Override
292293
public String toString() {
293-
return "BaseDirectories (" + operatingSystemName + "):\n" +
294+
return "BaseDirectories (" + Constants.operatingSystemName + "):\n" +
294295
" homeDir = '" + homeDir + "'\n" +
295296
" cacheDir = '" + cacheDir + "'\n" +
296297
" configDir = '" + configDir + "'\n" +
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.dirs;
2+
3+
import java.util.Locale;
4+
5+
public class Constants {
6+
7+
static final String operatingSystemName = System.getProperty("os.name");
8+
public static final char operatingSystem;
9+
static final char LIN = 'l';
10+
static final char MAC = 'm';
11+
static final char WIN = 'w';
12+
static final char BSD = 'b';
13+
static final char SOLARIS = 's';
14+
static final char IBMI = 'i';
15+
static final char AIX = 'a';
16+
17+
static {
18+
final String os = operatingSystemName.toLowerCase(Locale.ROOT);
19+
if (os.contains("linux"))
20+
operatingSystem = LIN;
21+
else if (os.contains("mac"))
22+
operatingSystem = MAC;
23+
else if (os.contains("windows"))
24+
operatingSystem = WIN;
25+
else if (os.contains("bsd"))
26+
operatingSystem = BSD;
27+
else if (os.contains("sunos"))
28+
operatingSystem = SOLARIS;
29+
else if (os.contains("os/400") || os.contains("os400"))
30+
operatingSystem = IBMI;
31+
else if (os.contains("aix"))
32+
operatingSystem = AIX;
33+
else
34+
throw new UnsupportedOperatingSystemException("directories are not supported on " + operatingSystemName);
35+
}
36+
37+
}

src/main/java/dev/dirs/ProjectDirectories.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package dev.dirs;
22

3-
import static dev.dirs.Util.*;
3+
import dev.dirs.impl.Linux;
4+
import dev.dirs.impl.MacOs;
5+
import dev.dirs.impl.Util;
6+
import dev.dirs.impl.Windows;
7+
8+
import java.util.Objects;
49

510
/** {@code ProjectDirectories} computes the location of cache, config or data directories for a specific application,
611
* which are derived from the standard directories and the name of the project/organization.
@@ -28,7 +33,7 @@ private ProjectDirectories(
2833
final String preferenceDir,
2934
final String runtimeDir) {
3035

31-
requireNonNull(projectPath);
36+
Objects.requireNonNull(projectPath);
3237

3338
this.projectPath = projectPath;
3439
this.cacheDir = cacheDir;
@@ -230,40 +235,39 @@ public static ProjectDirectories fromPath(String path) {
230235
String dataLocalDir;
231236
String preferenceDir;
232237
String runtimeDir = null;
233-
switch (operatingSystem) {
234-
case LIN:
235-
case BSD:
236-
case SOLARIS:
237-
case IBMI:
238-
case AIX:
238+
switch (Constants.operatingSystem) {
239+
case Constants.LIN:
240+
case Constants.BSD:
241+
case Constants.SOLARIS:
242+
case Constants.IBMI:
243+
case Constants.AIX:
239244
homeDir = System.getProperty("user.home");
240-
cacheDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
241-
configDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
242-
dataDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
245+
cacheDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
246+
configDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
247+
dataDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
243248
dataLocalDir = dataDir;
244249
preferenceDir = configDir;
245-
runtimeDir = linuxRuntimeDir(path);
250+
runtimeDir = Linux.runtimeDir(path);
246251
break;
247-
case MAC:
252+
case Constants.MAC:
248253
homeDir = System.getProperty("user.home");
249254
cacheDir = homeDir + "/Library/Caches/" + path;
250255
configDir = homeDir + "/Library/Application Support/" + path;
251256
dataDir = homeDir + "/Library/Application Support/" + path;
252257
dataLocalDir = dataDir;
253258
preferenceDir = homeDir + "/Library/Preferences/" + path;
254259
break;
255-
case WIN:
256-
String[] winDirs = getWinDirs("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
257-
String appDataRoaming = winDirs[0] + '\\' + path;
258-
String appDataLocal = winDirs[1] + '\\' + path;
260+
case Constants.WIN:
261+
String appDataRoaming = Windows.getRoamingAppDataDir() + '\\' + path;
262+
String appDataLocal = Windows.getLocalAppDataDir() + '\\' + path;
259263
dataDir = appDataRoaming + "\\data";
260264
dataLocalDir = appDataLocal + "\\data";
261265
configDir = appDataRoaming + "\\config";
262266
cacheDir = appDataLocal + "\\cache";
263267
preferenceDir = configDir;
264268
break;
265269
default:
266-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
270+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
267271
}
268272
return new ProjectDirectories(path, cacheDir, configDir, dataDir, dataLocalDir, preferenceDir, runtimeDir);
269273
}
@@ -289,32 +293,32 @@ public static ProjectDirectories fromPath(String path) {
289293
* {@code qualifier}, {@code organization} and {@code application} arguments.
290294
*/
291295
public static ProjectDirectories from(String qualifier, String organization, String application) {
292-
if (isNullOrEmpty(organization) && isNullOrEmpty(application))
296+
if (Util.isNullOrEmpty(organization) && Util.isNullOrEmpty(application))
293297
throw new UnsupportedOperationException("organization and application arguments cannot both be null/empty");
294298
String path;
295-
switch (operatingSystem) {
296-
case LIN:
297-
case BSD:
298-
case SOLARIS:
299-
case IBMI:
300-
case AIX:
301-
path = trimLowercaseReplaceWhitespace(application, "", true);
299+
switch (Constants.operatingSystem) {
300+
case Constants.LIN:
301+
case Constants.BSD:
302+
case Constants.SOLARIS:
303+
case Constants.IBMI:
304+
case Constants.AIX:
305+
path = Util.trimLowercaseReplaceWhitespace(application, "", true);
302306
break;
303-
case MAC:
304-
path = macOSApplicationPath(qualifier, organization, application);
307+
case Constants.MAC:
308+
path = MacOs.applicationPath(qualifier, organization, application);
305309
break;
306-
case WIN:
307-
path = windowsApplicationPath(qualifier, organization, application);
310+
case Constants.WIN:
311+
path = Windows.applicationPath(qualifier, organization, application);
308312
break;
309313
default:
310-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
314+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
311315
}
312316
return fromPath(path);
313317
}
314318

315319
@Override
316320
public String toString() {
317-
return "ProjectDirectories (" + operatingSystemName + "):\n" +
321+
return "ProjectDirectories (" + Constants.operatingSystemName + "):\n" +
318322
" projectPath = '" + projectPath + "'\n" +
319323
" cacheDir = '" + cacheDir + "'\n" +
320324
" configDir = '" + configDir + "'\n" +

src/main/java/dev/dirs/UserDirectories.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.dirs;
22

3-
import static dev.dirs.Util.*;
3+
import dev.dirs.impl.Linux;
4+
import dev.dirs.impl.Util;
5+
import dev.dirs.impl.Windows;
46

57
/** {@code UserDirectories} provides paths of user-facing standard directories, following the conventions of the operating system the library is running on.
68
*
@@ -299,24 +301,24 @@ public static UserDirectories get() {
299301
}
300302

301303
private UserDirectories() {
302-
switch (operatingSystem) {
303-
case LIN:
304-
case BSD:
305-
case SOLARIS:
306-
case AIX:
307-
String[] userDirs = getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
304+
switch (Constants.operatingSystem) {
305+
case Constants.LIN:
306+
case Constants.BSD:
307+
case Constants.SOLARIS:
308+
case Constants.AIX:
309+
String[] userDirs = Linux.getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
308310
homeDir = System.getProperty("user.home");
309311
audioDir = userDirs[0];
310312
desktopDir = userDirs[1];
311313
documentDir = userDirs[2];
312314
downloadDir = userDirs[3];
313-
fontDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
315+
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
314316
pictureDir = userDirs[4];
315317
publicDir = userDirs[5];
316318
templateDir = userDirs[6];
317319
videoDir = userDirs[7];
318320
break;
319-
case MAC:
321+
case Constants.MAC:
320322
homeDir = System.getProperty("user.home");
321323
audioDir = homeDir + "/Music";
322324
desktopDir = homeDir + "/Desktop";
@@ -328,48 +330,38 @@ private UserDirectories() {
328330
templateDir = null;
329331
videoDir = homeDir + "/Movies";
330332
break;
331-
case IBMI:
333+
case Constants.IBMI:
332334
homeDir = System.getProperty("user.home");
333335
audioDir = homeDir + "/Music";
334336
desktopDir = homeDir + "/Desktop";
335337
documentDir = homeDir + "/Documents";
336338
downloadDir = homeDir + "/Downloads";
337-
fontDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
339+
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
338340
pictureDir = homeDir + "/Pictures";
339341
publicDir = homeDir + "/Public";
340342
templateDir = null;
341343
videoDir = homeDir + "/Movies";
342344
break;
343-
case WIN:
344-
String[] winDirs = getWinDirs(
345-
"5E6C858F-0E22-4760-9AFE-EA3317B67173",
346-
"4BD8D571-6D19-48D3-BE97-422220080E43",
347-
"B4BFCC3A-DB2C-424C-B029-7FE99A87C641",
348-
"FDD39AD0-238F-46AF-ADB4-6C85480369C7",
349-
"374DE290-123F-4565-9164-39C4925E467B",
350-
"33E28130-4E1E-4676-835A-98395C3BC3BB",
351-
"DFDF76A2-C82A-4D63-906A-5644AC457385",
352-
"A63293E8-664E-48DB-A079-DF759E0509F7",
353-
"18989B1D-99B5-455B-841C-AB7C74E4DDFC");
354-
homeDir = winDirs[0];
355-
audioDir = winDirs[1];
345+
case Constants.WIN:
346+
homeDir = Windows.getProfileDir();
347+
audioDir = Windows.getMusicDir();
356348
fontDir = null;
357-
desktopDir = winDirs[2];
358-
documentDir = winDirs[3];
359-
downloadDir = winDirs[4];
360-
pictureDir = winDirs[5];
361-
publicDir = winDirs[6];
362-
templateDir = winDirs[7];
363-
videoDir = winDirs[8];
349+
desktopDir = Windows.getDesktopDir();
350+
documentDir = Windows.getDocumentsDir();
351+
downloadDir = Windows.getDownloadsDir();
352+
pictureDir = Windows.getPicturesDir();
353+
publicDir = Windows.getPublicDir();
354+
templateDir = Windows.getTemplatesDir();
355+
videoDir = Windows.getVideosDir();
364356
break;
365357
default:
366-
throw new UnsupportedOperatingSystemException("User directories are not supported on " + operatingSystemName);
358+
throw new UnsupportedOperatingSystemException("User directories are not supported on " + Constants.operatingSystemName);
367359
}
368360
}
369361

370362
@Override
371363
public String toString() {
372-
return "UserDirectories (" + operatingSystemName + "):\n" +
364+
return "UserDirectories (" + Constants.operatingSystemName + "):\n" +
373365
" homeDir = '" + homeDir + "'\n" +
374366
" audioDir = '" + audioDir + "'\n" +
375367
" fontDir = '" + fontDir + "'\n" +

0 commit comments

Comments
 (0)