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 Oct 17, 2024
1 parent 9e5e0e5 commit fea41a7
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 273 deletions.
36 changes: 19 additions & 17 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,8 +274,8 @@ 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");
case Constants.WIN:
String[] winDirs = Windows.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];
Expand All @@ -284,13 +286,13 @@ private BaseDirectories() {
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);
}

}
65 changes: 35 additions & 30 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,30 +235,30 @@ 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");
case Constants.WIN:
String[] winDirs = Windows.getWinDirs("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
String appDataRoaming = winDirs[0] + '\\' + path;
String appDataLocal = winDirs[1] + '\\' + path;
dataDir = appDataRoaming + "\\data";
Expand All @@ -263,7 +268,7 @@ public static ProjectDirectories fromPath(String path) {
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 +294,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
32 changes: 17 additions & 15 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,20 +330,20 @@ 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(
case Constants.WIN:
String[] winDirs = Windows.getWinDirs(
"5E6C858F-0E22-4760-9AFE-EA3317B67173",
"4BD8D571-6D19-48D3-BE97-422220080E43",
"B4BFCC3A-DB2C-424C-B029-7FE99A87C641",
Expand All @@ -363,13 +365,13 @@ private UserDirectories() {
videoDir = winDirs[8];
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 fea41a7

Please sign in to comment.