From e50bbeb15381782c91e6cd58e0f1167617741807 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Fri, 1 May 2020 22:02:06 +0100 Subject: [PATCH] added splitExtension function --- src/main/java/net/woggioni/jwo/JWO.java | 28 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/woggioni/jwo/JWO.java b/src/main/java/net/woggioni/jwo/JWO.java index 3c4fcdc..db87ab9 100644 --- a/src/main/java/net/woggioni/jwo/JWO.java +++ b/src/main/java/net/woggioni/jwo/JWO.java @@ -2,6 +2,7 @@ package net.woggioni.jwo; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import net.woggioni.jwo.tuple.Tuple2; import javax.net.ssl.*; import java.io.*; @@ -214,9 +215,9 @@ public class JWO { * * @param list the input list * @param the type parameter of the {@link List} + * @return the input list * @throws IndexOutOfBoundsException if the list is empty * @throws UnsupportedOperationException if the remove operation is not supported by this list - * @return the input list */ public static T pop(List list) { return list.remove(list.size() - 1); @@ -272,9 +273,9 @@ public class JWO { } /** - * @param reader the reader reading the template text + * @param reader the reader reading the template text * @param valuesMap a map containing the values to replace in the template - * {@link #renderTemplate(String, Map)} + * {@link #renderTemplate(String, Map)} */ @SneakyThrows public static String renderTemplate(Reader reader, Map valuesMap) { @@ -333,7 +334,7 @@ public class JWO { } public static Optional cast(T value, Class cls) { - if(cls.isInstance(value)) { + if (cls.isInstance(value)) { return Optional.of((U) value); } else { return Optional.empty(); @@ -355,9 +356,20 @@ public class JWO { public static Optional which(String command) { return Stream.of(System.getenv("PATH").split(Pattern.quote(File.pathSeparator))) - .map(path -> Paths.get(path, command)) - .filter(Files::exists) - .filter(Files::isExecutable) - .findFirst(); + .map(path -> Paths.get(path, command)) + .filter(Files::exists) + .filter(Files::isExecutable) + .findFirst(); + } + + public static Optional> splitExtension(Path file) { + String fileName = file.getFileName().toString(); + int index = fileName.lastIndexOf('.'); + if (index == -1) { + return Optional.empty(); + } else { + return Optional.of( + new Tuple2<>(fileName.substring(0, index), fileName.substring(index))); + } } }