FileEntity.java

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

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

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

  68.         public FileEntityFinder (@Nonnull final FileEntityFinder other, @Nonnull final Object override)
  69.           {
  70.             super(other, override);
  71.             final FileEntityFinder source = getSource(FileEntityFinder.class, other, override);
  72.             this.path = source.path;
  73.           }

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

  89.     @Nonnull
  90.     private final Path path;

  91.     @Delegate
  92.     private final AsSupport delegate;

  93.     private FileEntity (@Nonnull final Path path)
  94.       {
  95.         this.path = path;
  96.         final List<Object> roles = new ArrayList<>();

  97.         if (Files.isDirectory(path))
  98.           {
  99.             roles.add(SimpleComposite.of(new FileEntityFinder(path)));
  100.           }

  101.         delegate = new AsSupport(this, roles);
  102.       }

  103.     @Nonnull
  104.     public static FileEntity of (@Nonnull final Path path)
  105.       {
  106.         return new FileEntity(path);
  107.       }

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

  113.     @Nonnull
  114.     public ZonedDateTime getCreationDateTime()
  115.             throws IOException
  116.       {
  117.         return toZoneDateTime(getBasicFileAttributes().creationTime());
  118.       }

  119.     @Nonnull
  120.     public ZonedDateTime getLastAccessDateTime()
  121.             throws IOException
  122.       {
  123.         return toZoneDateTime(getBasicFileAttributes().lastAccessTime());
  124.       }

  125.     @Nonnull
  126.     public ZonedDateTime getLastModifiedDateTime()
  127.             throws IOException
  128.       {
  129.         return toZoneDateTime(getBasicFileAttributes().lastModifiedTime());
  130.       }

  131.     @Nonnull
  132.     public long getSize()
  133.             throws IOException
  134.       {
  135.         return Files.size(path);
  136.       }

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

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