Skip to content

Commit

Permalink
Split operating-specific functionality into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
soc committed Aug 2, 2024
1 parent 9e5e0e5 commit c9cf98b
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 477 deletions.
41 changes: 21 additions & 20 deletions src/main/java/dev/dirs/BaseDirectories.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.dirs;

import static dev.dirs.Util.*;
import dev.dirs.impl.Linux;
import dev.dirs.impl.Util;
import dev.dirs.impl.Windows;

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

private BaseDirectories() {
switch (operatingSystem) {
case LIN:
case BSD:
case SOLARIS:
case IBMI:
case AIX:
switch (Constants.operatingSystem) {
case Constants.LIN:
case Constants.BSD:
case Constants.SOLARIS:
case Constants.IBMI:
case Constants.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");
cacheDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_CACHE_HOME"), homeDir, "/.cache");
configDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_CONFIG_HOME"), homeDir, "/.config");
dataDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_DATA_HOME"), homeDir, "/.local/share");
dataLocalDir = dataDir;
executableDir = linuxExecutableDir(homeDir, dataDir);
executableDir = Linux.executableDir(homeDir, dataDir);
preferenceDir = configDir;
runtimeDir = linuxRuntimeDir(null);
runtimeDir = Linux.runtimeDir(null);
break;
case MAC:
case Constants.MAC:
homeDir = System.getProperty("user.home");
cacheDir = homeDir + "/Library/Caches/";
configDir = homeDir + "/Library/Application Support/";
Expand All @@ -272,25 +274,24 @@ private BaseDirectories() {
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];
case Constants.WIN:
homeDir = Windows.getProfileDir();;
dataDir = Windows.getRoamingAppDataDir();;
dataLocalDir = Windows.getLocalAppDataDir();;
configDir = dataDir;
cacheDir = dataLocalDir;
executableDir = null;
preferenceDir = configDir;
runtimeDir = null;
break;
default:
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + operatingSystemName);
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + Constants.operatingSystemName);
}
}

@Override
public String toString() {
return "BaseDirectories (" + operatingSystemName + "):\n" +
return "BaseDirectories (" + Constants.operatingSystemName + "):\n" +
" homeDir = '" + homeDir + "'\n" +
" cacheDir = '" + cacheDir + "'\n" +
" configDir = '" + configDir + "'\n" +
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/dev/dirs/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.dirs;

import java.util.Locale;

public class Constants {

static final String operatingSystemName = System.getProperty("os.name");
public static final char operatingSystem;
static final char LIN = 'l';
static final char MAC = 'm';
static final char WIN = 'w';
static final char BSD = 'b';
static final char SOLARIS = 's';
static final char IBMI = 'i';
static final char AIX = 'a';

static {
final String os = operatingSystemName.toLowerCase(Locale.ROOT);
if (os.contains("linux"))
operatingSystem = LIN;
else if (os.contains("mac"))
operatingSystem = MAC;
else if (os.contains("windows"))
operatingSystem = WIN;
else if (os.contains("bsd"))
operatingSystem = BSD;
else if (os.contains("sunos"))
operatingSystem = SOLARIS;
else if (os.contains("os/400") || os.contains("os400"))
operatingSystem = IBMI;
else if (os.contains("aix"))
operatingSystem = AIX;
else
throw new UnsupportedOperatingSystemException("directories are not supported on " + operatingSystemName);
}

}
68 changes: 36 additions & 32 deletions src/main/java/dev/dirs/ProjectDirectories.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package dev.dirs;

import static dev.dirs.Util.*;
import dev.dirs.impl.Linux;
import dev.dirs.impl.MacOs;
import dev.dirs.impl.Util;
import dev.dirs.impl.Windows;

import java.util.Objects;

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

requireNonNull(projectPath);
Objects.requireNonNull(projectPath);

this.projectPath = projectPath;
this.cacheDir = cacheDir;
Expand Down Expand Up @@ -230,40 +235,39 @@ public static ProjectDirectories fromPath(String path) {
String dataLocalDir;
String preferenceDir;
String runtimeDir = null;
switch (operatingSystem) {
case LIN:
case BSD:
case SOLARIS:
case IBMI:
case AIX:
switch (Constants.operatingSystem) {
case Constants.LIN:
case Constants.BSD:
case Constants.SOLARIS:
case Constants.IBMI:
case Constants.AIX:
homeDir = System.getProperty("user.home");
cacheDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
configDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
dataDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
cacheDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
configDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
dataDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
dataLocalDir = dataDir;
preferenceDir = configDir;
runtimeDir = linuxRuntimeDir(path);
runtimeDir = Linux.runtimeDir(path);
break;
case MAC:
case Constants.MAC:
homeDir = System.getProperty("user.home");
cacheDir = homeDir + "/Library/Caches/" + path;
configDir = homeDir + "/Library/Application Support/" + path;
dataDir = homeDir + "/Library/Application Support/" + path;
dataLocalDir = dataDir;
preferenceDir = homeDir + "/Library/Preferences/" + path;
break;
case WIN:
String[] winDirs = getWinDirs("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
String appDataRoaming = winDirs[0] + '\\' + path;
String appDataLocal = winDirs[1] + '\\' + path;
case Constants.WIN:
String appDataRoaming = Windows.getRoamingAppDataDir() + '\\' + path;
String appDataLocal = Windows.getLocalAppDataDir() + '\\' + path;
dataDir = appDataRoaming + "\\data";
dataLocalDir = appDataLocal + "\\data";
configDir = appDataRoaming + "\\config";
cacheDir = appDataLocal + "\\cache";
preferenceDir = configDir;
break;
default:
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
}
return new ProjectDirectories(path, cacheDir, configDir, dataDir, dataLocalDir, preferenceDir, runtimeDir);
}
Expand All @@ -289,32 +293,32 @@ public static ProjectDirectories fromPath(String path) {
* {@code qualifier}, {@code organization} and {@code application} arguments.
*/
public static ProjectDirectories from(String qualifier, String organization, String application) {
if (isNullOrEmpty(organization) && isNullOrEmpty(application))
if (Util.isNullOrEmpty(organization) && Util.isNullOrEmpty(application))
throw new UnsupportedOperationException("organization and application arguments cannot both be null/empty");
String path;
switch (operatingSystem) {
case LIN:
case BSD:
case SOLARIS:
case IBMI:
case AIX:
path = trimLowercaseReplaceWhitespace(application, "", true);
switch (Constants.operatingSystem) {
case Constants.LIN:
case Constants.BSD:
case Constants.SOLARIS:
case Constants.IBMI:
case Constants.AIX:
path = Util.trimLowercaseReplaceWhitespace(application, "", true);
break;
case MAC:
path = macOSApplicationPath(qualifier, organization, application);
case Constants.MAC:
path = MacOs.applicationPath(qualifier, organization, application);
break;
case WIN:
path = windowsApplicationPath(qualifier, organization, application);
case Constants.WIN:
path = Windows.applicationPath(qualifier, organization, application);
break;
default:
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
}
return fromPath(path);
}

@Override
public String toString() {
return "ProjectDirectories (" + operatingSystemName + "):\n" +
return "ProjectDirectories (" + Constants.operatingSystemName + "):\n" +
" projectPath = '" + projectPath + "'\n" +
" cacheDir = '" + cacheDir + "'\n" +
" configDir = '" + configDir + "'\n" +
Expand Down
58 changes: 25 additions & 33 deletions src/main/java/dev/dirs/UserDirectories.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.dirs;

import static dev.dirs.Util.*;
import dev.dirs.impl.Linux;
import dev.dirs.impl.Util;
import dev.dirs.impl.Windows;

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

private UserDirectories() {
switch (operatingSystem) {
case LIN:
case BSD:
case SOLARIS:
case AIX:
String[] userDirs = getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
switch (Constants.operatingSystem) {
case Constants.LIN:
case Constants.BSD:
case Constants.SOLARIS:
case Constants.AIX:
String[] userDirs = Linux.getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
homeDir = System.getProperty("user.home");
audioDir = userDirs[0];
desktopDir = userDirs[1];
documentDir = userDirs[2];
downloadDir = userDirs[3];
fontDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
pictureDir = userDirs[4];
publicDir = userDirs[5];
templateDir = userDirs[6];
videoDir = userDirs[7];
break;
case MAC:
case Constants.MAC:
homeDir = System.getProperty("user.home");
audioDir = homeDir + "/Music";
desktopDir = homeDir + "/Desktop";
Expand All @@ -328,48 +330,38 @@ private UserDirectories() {
templateDir = null;
videoDir = homeDir + "/Movies";
break;
case IBMI:
case Constants.IBMI:
homeDir = System.getProperty("user.home");
audioDir = homeDir + "/Music";
desktopDir = homeDir + "/Desktop";
documentDir = homeDir + "/Documents";
downloadDir = homeDir + "/Downloads";
fontDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
pictureDir = homeDir + "/Pictures";
publicDir = homeDir + "/Public";
templateDir = null;
videoDir = homeDir + "/Movies";
break;
case WIN:
String[] winDirs = getWinDirs(
"5E6C858F-0E22-4760-9AFE-EA3317B67173",
"4BD8D571-6D19-48D3-BE97-422220080E43",
"B4BFCC3A-DB2C-424C-B029-7FE99A87C641",
"FDD39AD0-238F-46AF-ADB4-6C85480369C7",
"374DE290-123F-4565-9164-39C4925E467B",
"33E28130-4E1E-4676-835A-98395C3BC3BB",
"DFDF76A2-C82A-4D63-906A-5644AC457385",
"A63293E8-664E-48DB-A079-DF759E0509F7",
"18989B1D-99B5-455B-841C-AB7C74E4DDFC");
homeDir = winDirs[0];
audioDir = winDirs[1];
case Constants.WIN:
homeDir = Windows.getProfileDir();
audioDir = Windows.getMusicDir();
fontDir = null;
desktopDir = winDirs[2];
documentDir = winDirs[3];
downloadDir = winDirs[4];
pictureDir = winDirs[5];
publicDir = winDirs[6];
templateDir = winDirs[7];
videoDir = winDirs[8];
desktopDir = Windows.getDesktopDir();
documentDir = Windows.getDocumentsDir();
downloadDir = Windows.getDownloadsDir();
pictureDir = Windows.getPicturesDir();
publicDir = Windows.getPublicDir();
templateDir = Windows.getTemplatesDir();
videoDir = Windows.getVideosDir();
break;
default:
throw new UnsupportedOperatingSystemException("User directories are not supported on " + operatingSystemName);
throw new UnsupportedOperatingSystemException("User directories are not supported on " + Constants.operatingSystemName);
}
}

@Override
public String toString() {
return "UserDirectories (" + operatingSystemName + "):\n" +
return "UserDirectories (" + Constants.operatingSystemName + "):\n" +
" homeDir = '" + homeDir + "'\n" +
" audioDir = '" + audioDir + "'\n" +
" fontDir = '" + fontDir + "'\n" +
Expand Down
Loading

0 comments on commit c9cf98b

Please sign in to comment.