Skip to contentMethod: toZoneDateTime(FileTime)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 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 the License.
12: * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.example.model;
27:
28: import javax.annotation.Nonnegative;
29: import javax.annotation.concurrent.Immutable;
30: import jakarta.annotation.Nonnull;
31: import java.time.ZoneId;
32: import java.time.ZonedDateTime;
33: import java.util.List;
34: import java.util.Optional;
35: import java.io.IOException;
36: import java.nio.file.Files;
37: import java.nio.file.Path;
38: import java.nio.file.attribute.BasicFileAttributeView;
39: import java.nio.file.attribute.BasicFileAttributes;
40: import java.nio.file.attribute.FileTime;
41: import it.tidalwave.ui.core.role.Displayable;
42: import it.tidalwave.util.As;
43: import it.tidalwave.util.Finder;
44: import it.tidalwave.role.SimpleComposite;
45: import lombok.EqualsAndHashCode;
46: import lombok.ToString;
47: import lombok.experimental.Delegate;
48: import lombok.extern.slf4j.Slf4j;
49: import static java.util.Collections.emptyList;
50: import static java.util.Comparator.comparing;
51: import static java.util.stream.Collectors.*;
52: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
53:
54: /***************************************************************************************************************************************************************
55: *
56: * A class that models a file with its attributes and children.
57: *
58: * @author Fabrizio Giudici
59: *
60: **************************************************************************************************************************************************************/
61: @Immutable @EqualsAndHashCode @ToString @Slf4j
62: public class FileEntity implements As, Displayable
63: {
64: /** The path of the file.*/
65: @Nonnull
66: private final Path path;
67:
68: /** Support object for implementing {@link As} functions.*/
69: @Delegate
70: private final As delegate;
71:
72: /***********************************************************************************************************************************************************
73: * Creates a new instance related to the given path.
74: * @param path the path
75: **********************************************************************************************************************************************************/
76: // START SNIPPET: constructor
77: private FileEntity (@Nonnull final Path path)
78: {
79: this.path = path;
80: delegate = As.forObject(this, Files.isDirectory(path) ? List.of(SimpleComposite.of(Finder.ofSupplier(_s(() -> filesIn(path))))) : emptyList());
81: }
82: // END SNIPPET: constructor
83:
84: /***********************************************************************************************************************************************************
85: * {@return a new instance} related to the given path.
86: * @param path the path
87: **********************************************************************************************************************************************************/
88: // START SNIPPET: constructor
89: @Nonnull
90: public static FileEntity of (@Nonnull final Path path)
91: {
92: return new FileEntity(path);
93: }
94: // END SNIPPET: constructor
95:
96: /***********************************************************************************************************************************************************
97: * {@return the display name}. It's a static implementation of the {@link Displayable} role.
98: **********************************************************************************************************************************************************/
99: @Override @Nonnull
100: public String getDisplayName()
101: {
102: return Optional.ofNullable(path.getFileName()).map(Path::toString).orElse("/");
103: }
104:
105: /***********************************************************************************************************************************************************
106: * {@return the creation date-time} of the file.
107: * @throws IOException if the file does not exist or cannot be accessed
108: **********************************************************************************************************************************************************/
109: @Nonnull
110: public ZonedDateTime getCreationDateTime()
111: throws IOException
112: {
113: return toZoneDateTime(getBasicFileAttributes().creationTime());
114: }
115:
116: /***********************************************************************************************************************************************************
117: * {@return the last access date-time} of the file.
118: * @throws IOException if the file does not exist or cannot be accessed
119: **********************************************************************************************************************************************************/
120: @Nonnull
121: public ZonedDateTime getLastAccessDateTime()
122: throws IOException
123: {
124: return toZoneDateTime(getBasicFileAttributes().lastAccessTime());
125: }
126:
127: /***********************************************************************************************************************************************************
128: * {@return the last modified date-time} of the file.
129: * @throws IOException if the file does not exist or cannot be accessed
130: **********************************************************************************************************************************************************/
131: @Nonnull
132: public ZonedDateTime getLastModifiedDateTime()
133: throws IOException
134: {
135: return toZoneDateTime(getBasicFileAttributes().lastModifiedTime());
136: }
137:
138: /***********************************************************************************************************************************************************
139: * {@return the size} of the file.
140: * @throws IOException if the file does not exist or cannot be accessed
141: **********************************************************************************************************************************************************/
142: @Nonnegative
143: public long getSize()
144: throws IOException
145: {
146: return Files.size(path);
147: }
148:
149: /***********************************************************************************************************************************************************
150: * {@return the basic attributes} of the file.
151: * @throws IOException if the file does not exist or cannot be accessed
152: **********************************************************************************************************************************************************/
153: @Nonnull
154: private BasicFileAttributes getBasicFileAttributes()
155: throws IOException
156: {
157: return Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
158: }
159:
160: /***********************************************************************************************************************************************************
161: *
162: **********************************************************************************************************************************************************/
163: @Nonnull
164: private static List<FileEntity> filesIn (@Nonnull final Path path)
165: throws IOException
166: {
167: try (final var stream = Files.list(path))
168: {
169: return stream.sorted(comparing(Path::toString)).map(FileEntity::of).collect(toList());
170: }
171: }
172:
173: /***********************************************************************************************************************************************************
174: *
175: **********************************************************************************************************************************************************/
176: @Nonnull
177: private static ZonedDateTime toZoneDateTime (@Nonnull final FileTime dateTime)
178: {
179: return ZonedDateTime.ofInstant(dateTime.toInstant(), ZoneId.systemDefault());
180: }
181: }