FileEntity.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2023 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/steelblue-src
  23.  * git clone https://github.com/tidalwave-it/steelblue-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.javafx.example.large.data.impl;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.concurrent.Immutable;
  30. import java.time.ZoneId;
  31. import java.time.ZonedDateTime;
  32. import java.util.ArrayList;
  33. import java.util.Collections;
  34. import java.util.Comparator;
  35. import java.util.List;
  36. import java.util.Optional;
  37. import java.io.IOException;
  38. import java.nio.file.Files;
  39. import java.nio.file.Path;
  40. import java.nio.file.attribute.BasicFileAttributeView;
  41. import java.nio.file.attribute.BasicFileAttributes;
  42. import java.nio.file.attribute.FileTime;
  43. import it.tidalwave.util.As;
  44. import it.tidalwave.util.spi.SimpleFinderSupport;
  45. import it.tidalwave.role.SimpleComposite;
  46. import it.tidalwave.role.ui.Displayable;
  47. import lombok.EqualsAndHashCode;
  48. import lombok.RequiredArgsConstructor;
  49. import lombok.ToString;
  50. import lombok.experimental.Delegate;
  51. import static java.util.stream.Collectors.*;

  52. /***********************************************************************************************************************
  53.  *
  54.  * @author  Fabrizio Giudici
  55.  *
  56.  **********************************************************************************************************************/
  57. @Immutable @EqualsAndHashCode @ToString
  58. public class FileEntity implements As, Displayable
  59.   {
  60.     @RequiredArgsConstructor
  61.     public static class FileEntityFinder extends SimpleFinderSupport<FileEntity>
  62.       {
  63.         @Nonnull
  64.         private final Path path;

  65.         public FileEntityFinder (@Nonnull final FileEntityFinder other, @Nonnull final Object override)
  66.           {
  67.             super(other, override);
  68.             final var source = getSource(FileEntityFinder.class, other, override);
  69.             this.path = source.path;
  70.           }

  71.         @Nonnull @Override
  72.         protected List<FileEntity> computeNeededResults()
  73.           {
  74.             try
  75.               {
  76.                 return Files.list(path).sorted(Comparator.comparing(Path::toString)).map(FileEntity::of).collect(toList());
  77.               }
  78.             catch (Exception e)
  79.               {
  80.                 System.err.println(e);
  81.                 return Collections.emptyList();
  82.                 // throw new UncheckedIOException(e);
  83.               }
  84.           }
  85.       }

  86.     @Nonnull
  87.     private final Path path;

  88.     @Delegate
  89.     private final As delegate;

  90.     private FileEntity (@Nonnull final Path path)
  91.       {
  92.         this.path = path;
  93.         final List<Object> roles = new ArrayList<>();

  94.         if (Files.isDirectory(path))
  95.           {
  96.             roles.add(SimpleComposite.of(new FileEntityFinder(path)));
  97.           }

  98.         delegate = As.forObject(this, roles);
  99.       }

  100.     @Nonnull
  101.     public static FileEntity of (@Nonnull final Path path)
  102.       {
  103.         return new FileEntity(path);
  104.       }

  105.     @Override @Nonnull
  106.     public String getDisplayName()
  107.       {
  108.         return Optional.ofNullable(path.getFileName()).map(Path::toString).orElse("/");
  109.       }

  110.     @Nonnull
  111.     public ZonedDateTime getCreationDateTime()
  112.             throws IOException
  113.       {
  114.         return toZoneDateTime(getBasicFileAttributes().creationTime());
  115.       }

  116.     @Nonnull
  117.     public ZonedDateTime getLastAccessDateTime()
  118.             throws IOException
  119.       {
  120.         return toZoneDateTime(getBasicFileAttributes().lastAccessTime());
  121.       }

  122.     @Nonnull
  123.     public ZonedDateTime getLastModifiedDateTime()
  124.             throws IOException
  125.       {
  126.         return toZoneDateTime(getBasicFileAttributes().lastModifiedTime());
  127.       }

  128.     @Nonnull
  129.     public long getSize()
  130.             throws IOException
  131.       {
  132.         return Files.size(path);
  133.       }

  134.     @Nonnull
  135.     private BasicFileAttributes getBasicFileAttributes()
  136.             throws IOException
  137.       {
  138.         return Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
  139.       }

  140.     @Nonnull
  141.     private static ZonedDateTime toZoneDateTime (@Nonnull final FileTime dateTime)
  142.       {
  143.         return ZonedDateTime.ofInstant(dateTime.toInstant(), ZoneId.systemDefault());
  144.       }
  145.   }