Package: DefaultFileEntity
DefaultFileEntity
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultFileEntity(Path, Predicate) |
|
|
|
|
|
||||||||||||||||||||
DefaultFileEntity(Path, Predicate, FileSystemProvider) |
|
|
|
|
|
||||||||||||||||||||
filesIn(Path, Predicate, FileSystemProvider) |
|
|
|
|
|
||||||||||||||||||||
getBasicFileAttributes() |
|
|
|
|
|
||||||||||||||||||||
getCreationDateTime() |
|
|
|
|
|
||||||||||||||||||||
getDisplayName() |
|
|
|
|
|
||||||||||||||||||||
getLastAccessDateTime() |
|
|
|
|
|
||||||||||||||||||||
getLastModifiedDateTime() |
|
|
|
|
|
||||||||||||||||||||
getMimeType() |
|
|
|
|
|
||||||||||||||||||||
getSize() |
|
|
|
|
|
||||||||||||||||||||
isDirectory() |
|
|
|
|
|
||||||||||||||||||||
isUnixHiddenFile() |
|
|
|
|
|
||||||||||||||||||||
lambda$filesIn$1(Path) |
|
|
|
|
|
||||||||||||||||||||
lambda$filesIn$2(Predicate, Path) |
|
|
|
|
|
||||||||||||||||||||
lambda$filesIn$3(FileEntity) |
|
|
|
|
|
||||||||||||||||||||
lambda$new$0(Path, Predicate, FileSystemProvider) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toZoneDateTime(FileTime) |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * blueMarine III: Semantic DAM
5: * http://tidalwave.it/projects/bluemarine3
6: *
7: * Copyright (C) 2024 - 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/bluemarine3-src
22: * git clone https://github.com/tidalwave-it/bluemarine3-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.dam.model.io.impl;
27:
28: import jakarta.annotation.Nonnull;
29: import java.time.ZoneId;
30: import java.time.ZonedDateTime;
31: import java.util.List;
32: import java.util.Objects;
33: import java.util.Optional;
34: import java.util.function.Predicate;
35: import java.util.stream.StreamSupport;
36: import java.io.IOException;
37: import java.nio.file.Files;
38: import java.nio.file.Path;
39: import java.nio.file.attribute.BasicFileAttributeView;
40: import java.nio.file.attribute.BasicFileAttributes;
41: import java.nio.file.attribute.FileTime;
42: import java.nio.file.spi.FileSystemProvider;
43: import it.tidalwave.dam.model.MimeType;
44: import it.tidalwave.dam.model.io.FileEntity;
45: import it.tidalwave.dam.model.spi.EntitySupport;
46: import it.tidalwave.util.Finder;
47: import it.tidalwave.role.SimpleComposite;
48: import lombok.EqualsAndHashCode;
49: import lombok.Getter;
50: import lombok.ToString;
51: import lombok.extern.slf4j.Slf4j;
52: import static java.util.Collections.emptyList;
53: import static java.util.Comparator.comparing;
54: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
55:
56: /***************************************************************************************************************************************************************
57: *
58: * The default implementation of {@link FileEntity}.
59: *
60: * @author Fabrizio Giudici
61: *
62: **************************************************************************************************************************************************************/
63: // @Immutable FIXME
64: @Getter @EqualsAndHashCode(of="path", callSuper = false) @ToString(of = "path") @Slf4j
65: public class DefaultFileEntity extends EntitySupport implements FileEntity
66: {
67: /** The path of the file.*/
68: @Nonnull
69: private final Path path;
70:
71: /***********************************************************************************************************************************************************
72: * Creates a new instance related to the given path.
73: * @param path the path
74: * @param filter a predicate to filter children
75: **********************************************************************************************************************************************************/
76: public DefaultFileEntity (@Nonnull final Path path, @Nonnull final Predicate<FileEntity> filter)
77: {
78: this(path, filter, path.getFileSystem().provider());
79: }
80:
81: /***********************************************************************************************************************************************************
82: * Creates a new instance related to the given path. This variant is for testing.
83: * @param path the path
84: * @param filter a predicate to filter children
85: * @param fsProvider the file system provider
86: **********************************************************************************************************************************************************/
87: protected DefaultFileEntity (@Nonnull final Path path, @Nonnull final Predicate<FileEntity> filter, @Nonnull final FileSystemProvider fsProvider)
88: {
89:• super(Files.isDirectory(path) ? List.of(SimpleComposite.of(Finder.ofSupplier(_s(() -> filesIn(path, filter, fsProvider))))) : emptyList());
90: this.path = path;
91: }
92:
93: /***********************************************************************************************************************************************************
94: * {@inheritDoc}
95: **********************************************************************************************************************************************************/
96: @Override @Nonnull
97: public String getDisplayName()
98: {
99: return Optional.ofNullable(path.getFileName()).map(Path::toString).orElse("/");
100: }
101:
102: /***********************************************************************************************************************************************************
103: * {@inheritDoc}
104: **********************************************************************************************************************************************************/
105: @Override @Nonnull
106: public ZonedDateTime getCreationDateTime()
107: throws IOException
108: {
109: return toZoneDateTime(getBasicFileAttributes().creationTime());
110: }
111:
112: /***********************************************************************************************************************************************************
113: * {@inheritDoc}
114: **********************************************************************************************************************************************************/
115: @Override @Nonnull
116: public ZonedDateTime getLastAccessDateTime()
117: throws IOException
118: {
119: return toZoneDateTime(getBasicFileAttributes().lastAccessTime());
120: }
121:
122: /***********************************************************************************************************************************************************
123: * {@inheritDoc}
124: **********************************************************************************************************************************************************/
125: @Override @Nonnull
126: public ZonedDateTime getLastModifiedDateTime()
127: throws IOException
128: {
129: return toZoneDateTime(getBasicFileAttributes().lastModifiedTime());
130: }
131:
132: /***********************************************************************************************************************************************************
133: * {@inheritDoc}
134: **********************************************************************************************************************************************************/
135: @Override // FIXME @Nonnegative
136: public long getSize()
137: throws IOException
138: {
139: return Files.size(path);
140: }
141:
142: /***********************************************************************************************************************************************************
143: * {@inheritDoc}
144: **********************************************************************************************************************************************************/
145: @Override
146: public boolean isDirectory()
147: {
148: return Files.isDirectory(path);
149: }
150:
151: /***********************************************************************************************************************************************************
152: * {@inheritDoc}
153: **********************************************************************************************************************************************************/
154: @Override
155: public boolean isUnixHiddenFile()
156: {
157: return Objects.requireNonNull(path.getFileName()).toString().startsWith(".");
158: }
159:
160: /***********************************************************************************************************************************************************
161: * {@inheritDoc}
162: **********************************************************************************************************************************************************/
163: @Override @Nonnull
164: public Optional<MimeType> getMimeType()
165: {
166: return findMimeType(this);
167: }
168:
169: /***********************************************************************************************************************************************************
170: * {@return the basic attributes} of the file.
171: * @throws IOException if the file does not exist or cannot be accessed
172: **********************************************************************************************************************************************************/
173: @Nonnull
174: private BasicFileAttributes getBasicFileAttributes()
175: throws IOException
176: {
177: return Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
178: }
179:
180: /***********************************************************************************************************************************************************
181: * {@return file entities in the given path}.
182: * @param path the path
183: * @param filter a predicate to filter children
184: * @param fsProvider the file system provider
185: * @throws IOException in case of I/O error
186: **********************************************************************************************************************************************************/
187: private static List<FileEntity> filesIn (@Nonnull final Path path, @Nonnull final Predicate<FileEntity> filter, @Nonnull final FileSystemProvider fsProvider)
188: throws IOException
189: {
190: try (final var dirStream = fsProvider.newDirectoryStream(path, p -> true);
191: final var stream = StreamSupport.stream(dirStream.spliterator(), false))
192: {
193: return stream.map(p -> FileEntity.of(p, filter)).filter(filter).sorted(comparing(f -> f.getPath().toString())).toList();
194: }
195: }
196:
197: /***********************************************************************************************************************************************************
198: *
199: **********************************************************************************************************************************************************/
200: @Nonnull
201: private static ZonedDateTime toZoneDateTime (@Nonnull final FileTime dateTime)
202: {
203: return ZonedDateTime.ofInstant(dateTime.toInstant(), ZoneId.systemDefault());
204: }
205: }