
io.github.imsejin.common.util.PathnameUtils Maven / Gradle / Ivy
package io.github.imsejin.common.util;
import io.github.imsejin.common.constant.DateType;
import io.github.imsejin.common.constant.OperatingSystem;
import io.github.imsejin.common.tool.OSDetector;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
import static io.github.imsejin.common.util.DateTimeUtils.today;
/**
* Pathname utilities
*/
public final class PathnameUtils {
/**
* Path separator of Microsoft Windows.
*/
private static final String WINDOWS_SEPARATOR = "\\\\";
/**
* Path separator of Unix.
*/
private static final String UNIX_SEPARATOR = "/";
private PathnameUtils() {
}
/**
* Gets the current path where the application is.
*
* {@code
* getCurrentPathname(); // /usr/local/repositories/common-utils
* }
*
* @return current path where the application is
*/
public static String getCurrentPathname() {
try {
// This code can be replaced with `System.getProperty("user.dir")`.
return Paths.get(".").toRealPath().toString();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
/**
* Removes all the path separators.
*
* {@code
* removeSeparators("C:\\Program Files\\Java"); // C:Program FilesJava
* removeSeparators("/usr/data/java"); // usrsdatajava
* }
*
* @param pathname pathname
* @return pathname removed all the path separators
*/
public static String removeSeparators(String pathname) {
return pathname.replaceAll(WINDOWS_SEPARATOR, "").replaceAll(UNIX_SEPARATOR, "");
}
/**
* 부적절한 경로명을 올바른 경로명으로 정정한다.
* OS가 Windows인 경우, 절대경로로 지정해도 앞에 구분자가 들어가지 않는다.
*
* {@code
* String pathname1 = "\\/ / C:\\ Program Files / \\/\\ \\ Java\\jdk8 /\\/ \\ ";
* correct(false, pathname1); // C:\\Program Files\\Java\\jdk8
*
* String pathname2 = "/ / \\ usr / data /java/jdk8 / ";
* correct(true, pathname2); // /usr/data/java/jdk8
*
* String pathname3 = "/ / \\ usr / data /java/jdk8 / ";
* correct(false, pathname3); // usr/data/java/jdk8
* }
*
* @param absolute whether path is absolute
* @param pathname pathname
* @return correct pathname
*/
public static String correct(boolean absolute, String pathname) {
String trimmed = Arrays.stream(pathname.split(WINDOWS_SEPARATOR)) // split with Windows separators.
.map(p -> String.join("", p.split(UNIX_SEPARATOR))) // split with Unix separators.
.filter(p -> !StringUtils.isNullOrBlank(p))
.map(String::trim)
.collect(Collectors.joining(File.separator));
return absolute && OSDetector.getOS() != OperatingSystem.WINDOWS
? UNIX_SEPARATOR + trimmed
: trimmed;
}
/**
* Concatenates pathnames.
*
* {@code
* concat(false, "C:\\", "Program Files", "Java"); // C:\\Program Files\\Java
* concat(true, "/usr/", "/data/", "java"); // /usr/data/java
* concat(false, "/usr/", "/data/", "java"); // usr/data/java
* }
*
* @param absolute whether paths are absolute
* @param pathnames pathnames
* @return concatenated pathname
*/
public static String concat(boolean absolute, String... pathnames) {
return correct(absolute, String.join(File.separator, pathnames));
}
/**
* 경로 끝에 현재의 연/월(yyyy/MM) 경로를 추가한다.
* Adds the current year/month (yyyy/MM) pathname to the end of the pathname.
*
* {@code
* DateTimeUtils.today(); // 20191231
* appendYearMonth("C:\\Program Files"); // C:\\Program Files\\2019\\12
* }
*
* @param pathname pathname
* @return pathname appended the current year and month
*/
public static String appendYearMonth(String pathname) {
return concat(false, pathname, today(DateType.YEAR), today(DateType.MONTH));
}
/**
* 경로 끝에 현재의 연/월/일(yyyy/MM/dd) 경로를 추가한다.
* Adds the current year/month/day (yyyy/MM/dd) pathname to the end of the pathname.
*
* {@code
* DateTimeUtils.today(); // 20191231
* appendDate("C:\\Program Files"); // C:\\Program Files\\2019\\12\\31
* }
*
* @param pathname pathname
* @return pathname appended the current year, month and day
*/
public static String appendDate(String pathname) {
return concat(false, pathname, today(DateType.YEAR), today(DateType.MONTH), today(DateType.DAY));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy