2222import java .util .*;
2323import java .util .concurrent .ThreadFactory ;
2424import java .util .concurrent .atomic .AtomicInteger ;
25+ import java .util .concurrent .atomic .AtomicReference ;
2526import java .util .function .BiConsumer ;
2627import java .util .function .Consumer ;
2728import java .util .function .Function ;
29+ import java .util .function .Supplier ;
2830import java .util .stream .Stream ;
2931import java .util .zip .GZIPInputStream ;
3032import 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}
0 commit comments