Skip to content

Commit d9b382b

Browse files
committed
nu
1 parent 368541d commit d9b382b

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

common/src/main/java/dev/felnull/fnjl/util/FNDataUtil.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.*;
2323
import java.util.concurrent.ThreadFactory;
2424
import java.util.concurrent.atomic.AtomicInteger;
25+
import java.util.concurrent.atomic.AtomicReference;
2526
import java.util.function.BiConsumer;
2627
import java.util.function.Consumer;
2728
import java.util.function.Function;
29+
import java.util.function.Supplier;
2830
import java.util.stream.Stream;
2931
import java.util.zip.GZIPInputStream;
3032
import java.util.zip.GZIPOutputStream;
@@ -507,6 +509,32 @@ public M apply(T t) {
507509
};
508510
}
509511

512+
/**
513+
* メモ化
514+
*
515+
* @param supplier Supplier
516+
* @param <T> 結果
517+
* @return メモ化済みSupplier
518+
*/
519+
@NotNull
520+
public static <T> Supplier<T> memoize(@NotNull final Supplier<T> supplier) {
521+
return new Supplier<T>() {
522+
private final AtomicReference<T> cache = new AtomicReference<>();
523+
524+
@Override
525+
public T get() {
526+
synchronized (cache) {
527+
T ret = cache.get();
528+
if (ret == null) {
529+
ret = supplier.get();
530+
cache.set(ret);
531+
}
532+
return ret;
533+
}
534+
}
535+
};
536+
}
537+
510538
/**
511539
* リソースディレクトリ内のファイルの一覧を表示
512540
*
@@ -515,6 +543,7 @@ public M apply(T t) {
515543
* @return エントリ
516544
*/
517545
@NotNull
546+
518547
public static List<ResourceEntry> resourceExtractEntry(@NotNull Class<?> clazz, @NotNull String path) {
519548
if (!path.startsWith("/")) path = "/" + path;
520549
URL url = clazz.getResource(path);
@@ -551,4 +580,29 @@ private static List<ResourceEntry> getResourceEntry(Path path, Path targetPath,
551580
return Collections.unmodifiableList(entries);
552581
}
553582

583+
/**
584+
* フォルダが存在しなければを作成する
585+
* 作成できない場合は例外を投げる
586+
*
587+
* @param file ファイル
588+
*/
589+
public static void wishMkdir(@NotNull File file) {
590+
if (!file.exists() && !file.mkdirs())
591+
throw new RuntimeException("Failed to create fold: " + file.getAbsolutePath());
592+
}
593+
594+
/**
595+
* フォルダが存在しなければを作成する
596+
*
597+
* @param file ファイル
598+
* @param failedConsumer 作成失敗時に呼び出される
599+
* @return 作成できたか、もしくはそもそも存在したか
600+
*/
601+
public static boolean wishMkdir(@NotNull File file, Consumer<File> failedConsumer) {
602+
if (!file.exists() && !file.mkdirs()) {
603+
failedConsumer.accept(file);
604+
return false;
605+
}
606+
return true;
607+
}
554608
}

common/src/main/java/dev/felnull/fnjl/util/FNURLUtil.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ public static URL newURL(@Nullable String url) {
4747
*/
4848
@NotNull
4949
public static String getUserAgent() {
50-
String jv = System.getProperty("java.version");
51-
String jvn = System.getProperty("java.vm.name");
52-
String jvv = System.getProperty("java.vm.version");
53-
return String.format("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36 %s %s", "Java/" + jv + " (" + jvn + "; " + jvv + ")", "FelNullJavaLibrary/" + FelNullJavaLibrary.getVersion());
50+
String jv = System.getProperty("java.specification.version");
51+
52+
return String.format("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 %s %s", "Java/" + jv, "FelNullJavaLibrary/" + FelNullJavaLibrary.getVersion());
5453
}
5554

5655
/**

0 commit comments

Comments
 (0)