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

Commit 4679113

Browse files
committed
Split operating-specific functionality into separate files
1 parent 68b72ab commit 4679113

File tree

9 files changed

+334
-273
lines changed

9 files changed

+334
-273
lines changed

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

Lines changed: 19 additions & 17 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,8 +274,8 @@ 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+
case Constants.WIN:
278+
String[] winDirs = Windows.getWinDirs("5E6C858F-0E22-4760-9AFE-EA3317B67173", "3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
277279
homeDir = winDirs[0];
278280
dataDir = winDirs[1];
279281
dataLocalDir = winDirs[2];
@@ -284,13 +286,13 @@ private BaseDirectories() {
284286
runtimeDir = null;
285287
break;
286288
default:
287-
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + operatingSystemName);
289+
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + Constants.operatingSystemName);
288290
}
289291
}
290292

291293
@Override
292294
public String toString() {
293-
return "BaseDirectories (" + operatingSystemName + "):\n" +
295+
return "BaseDirectories (" + Constants.operatingSystemName + "):\n" +
294296
" homeDir = '" + homeDir + "'\n" +
295297
" cacheDir = '" + cacheDir + "'\n" +
296298
" configDir = '" + configDir + "'\n" +

src/main/java/dev/dirs/Constants.java

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: 35 additions & 30 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,30 +235,30 @@ 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");
260+
case Constants.WIN:
261+
String[] winDirs = Windows.getWinDirs("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
257262
String appDataRoaming = winDirs[0] + '\\' + path;
258263
String appDataLocal = winDirs[1] + '\\' + path;
259264
dataDir = appDataRoaming + "\\data";
@@ -263,7 +268,7 @@ public static ProjectDirectories fromPath(String path) {
263268
preferenceDir = configDir;
264269
break;
265270
default:
266-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
271+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
267272
}
268273
return new ProjectDirectories(path, cacheDir, configDir, dataDir, dataLocalDir, preferenceDir, runtimeDir);
269274
}
@@ -289,32 +294,32 @@ public static ProjectDirectories fromPath(String path) {
289294
* {@code qualifier}, {@code organization} and {@code application} arguments.
290295
*/
291296
public static ProjectDirectories from(String qualifier, String organization, String application) {
292-
if (isNullOrEmpty(organization) && isNullOrEmpty(application))
297+
if (Util.isNullOrEmpty(organization) && Util.isNullOrEmpty(application))
293298
throw new UnsupportedOperationException("organization and application arguments cannot both be null/empty");
294299
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);
300+
switch (Constants.operatingSystem) {
301+
case Constants.LIN:
302+
case Constants.BSD:
303+
case Constants.SOLARIS:
304+
case Constants.IBMI:
305+
case Constants.AIX:
306+
path = Util.trimLowercaseReplaceWhitespace(application, "", true);
302307
break;
303-
case MAC:
304-
path = macOSApplicationPath(qualifier, organization, application);
308+
case Constants.MAC:
309+
path = MacOs.applicationPath(qualifier, organization, application);
305310
break;
306-
case WIN:
307-
path = windowsApplicationPath(qualifier, organization, application);
311+
case Constants.WIN:
312+
path = Windows.applicationPath(qualifier, organization, application);
308313
break;
309314
default:
310-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
315+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
311316
}
312317
return fromPath(path);
313318
}
314319

315320
@Override
316321
public String toString() {
317-
return "ProjectDirectories (" + operatingSystemName + "):\n" +
322+
return "ProjectDirectories (" + Constants.operatingSystemName + "):\n" +
318323
" projectPath = '" + projectPath + "'\n" +
319324
" cacheDir = '" + cacheDir + "'\n" +
320325
" configDir = '" + configDir + "'\n" +

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

Lines changed: 17 additions & 15 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,20 +330,20 @@ 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+
case Constants.WIN:
346+
String[] winDirs = Windows.getWinDirs(
345347
"5E6C858F-0E22-4760-9AFE-EA3317B67173",
346348
"4BD8D571-6D19-48D3-BE97-422220080E43",
347349
"B4BFCC3A-DB2C-424C-B029-7FE99A87C641",
@@ -363,13 +365,13 @@ private UserDirectories() {
363365
videoDir = winDirs[8];
364366
break;
365367
default:
366-
throw new UnsupportedOperatingSystemException("User directories are not supported on " + operatingSystemName);
368+
throw new UnsupportedOperatingSystemException("User directories are not supported on " + Constants.operatingSystemName);
367369
}
368370
}
369371

370372
@Override
371373
public String toString() {
372-
return "UserDirectories (" + operatingSystemName + "):\n" +
374+
return "UserDirectories (" + Constants.operatingSystemName + "):\n" +
373375
" homeDir = '" + homeDir + "'\n" +
374376
" audioDir = '" + audioDir + "'\n" +
375377
" fontDir = '" + fontDir + "'\n" +

0 commit comments

Comments
 (0)