FileSystemMediaFolderFinder.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.model.impl;

  28. import javax.annotation.Nonnegative;
  29. import javax.annotation.Nonnull;
  30. import java.io.IOException;
  31. import java.nio.file.DirectoryStream;
  32. import java.nio.file.Files;
  33. import java.nio.file.Path;
  34. import java.util.List;
  35. import java.util.function.Function;
  36. import java.util.function.Predicate;
  37. import java.util.stream.Stream;
  38. import java.util.stream.StreamSupport;
  39. import it.tidalwave.util.spi.FinderSupport;
  40. import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
  41. import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;
  42. import lombok.RequiredArgsConstructor;
  43. import lombok.extern.slf4j.Slf4j;
  44. import static java.util.stream.Collectors.*;

  45. /***********************************************************************************************************************
  46.  *
  47.  * An {@link PathAwareFinder} for retrieving children of a {@link FileSystemMediaFolder}.
  48.  *
  49.  * @stereotype  Finder
  50.  *
  51.  * @author  Fabrizio Giudici
  52.  *
  53.  **********************************************************************************************************************/
  54. @RequiredArgsConstructor @Slf4j
  55. public class FileSystemMediaFolderFinder extends FinderSupport<PathAwareEntity, PathAwareFinder> implements PathAwareFinder
  56.   {
  57.     private static final long serialVersionUID = 7656309392185783930L;

  58.     @Nonnull
  59.     private final FileSystemMediaFolder mediaFolder;

  60.     @Nonnull
  61.     private final Path basePath;

  62.     // FIXME: implement a better filter looking at the file name suffix
  63.     private final Predicate<? super Path> fileFilter = path -> !path.toFile().getName().equals(".DS_Store");

  64.     public FileSystemMediaFolderFinder (@Nonnull final FileSystemMediaFolderFinder other, @Nonnull final Object override)
  65.       {
  66.         super(other, override);
  67.         final FileSystemMediaFolderFinder source = getSource(FileSystemMediaFolderFinder.class, other, override);
  68.         this.mediaFolder = source.mediaFolder;
  69.         this.basePath = source.basePath;
  70.       }

  71.     @Override @Nonnegative
  72.     public int count()
  73.       {
  74.         return evaluateDirectoryStream(stream -> stream.filter(fileFilter).count()).intValue();
  75.       }

  76.     @Override @Nonnull
  77.     public PathAwareFinder withPath (@Nonnull final Path path)
  78.       {
  79.         throw new UnsupportedOperationException("Not supported yet."); // TODO
  80.       }

  81.     @Override @Nonnull
  82.     protected List<? extends PathAwareEntity> computeResults()
  83.       {
  84.         return evaluateDirectoryStream(stream -> stream
  85.                                                 .filter(fileFilter)
  86.                                                 .map(child -> Files.isDirectory(child)
  87.                                                             ? new FileSystemMediaFolder(child, mediaFolder, basePath)
  88.                                                             : new FileSystemAudioFile(child, mediaFolder, basePath))
  89.                                                 .collect(toList()));
  90.       }

  91.     private <T> T evaluateDirectoryStream (@Nonnull final Function<Stream<Path>, T> function)
  92.       {
  93.         if (!Files.exists(mediaFolder.getPath()))
  94.           {
  95.             return function.apply(Stream.of());
  96.           }

  97.         try (final DirectoryStream<Path> stream = Files.newDirectoryStream(mediaFolder.getPath()))
  98.           {
  99.             return function.apply(StreamSupport.stream(stream.spliterator(), false));
  100.           }
  101.         catch (IOException e)
  102.           {
  103.             log.error("", e);
  104.             throw new RuntimeException(e);
  105.           }
  106.       }
  107.   }