33import java .io .*;
44import java .nio .file .Files ;
55import java .nio .file .Path ;
6- import java .util .HashMap ;
7- import java .util .Objects ;
6+ import java .util .* ;
7+ import java .util .concurrent . ConcurrentHashMap ;
88
99/**
1010 * Represents a directory in the {@link FileHandler}. Applications do not use this class.
1111 *
1212 * @see FileHandler
1313 * @see FileEntry
1414 * @since 02.00.00
15- * @version 02.00 .00
15+ * @version 03.05 .00
1616 * @author Ktt Development
1717 */
1818class DirectoryEntry {
1919
20- private final boolean isWalkthrough ;
21- private final boolean isFilesPreloaded ;
22-
2320 private final File directory ;
24- private final HashMap <String ,FileEntry > files = new HashMap <>(); // preload only
25-
2621 private final FileHandlerAdapter adapter ;
22+ private final ByteLoadingOption loadingOption ;
23+ private final boolean isWalkthrough ;
2724
28- /**
29- * Creates a directory entry.
30- *
31- * @param directory directory to represent
32- * @param isFilesPreloaded whether to read bytes now or at runtime
33- * @param adapter how to process the file name for context and bytes for {@link #getBytes(String)}
34- * @param isWalkthrough if directory should contain inner folders
35- * @throws FileNotFoundException if file was not found or is not a directory
36- * @throws IOException if file walk failed for initial directory
37- *
38- * @see FileHandlerAdapter
39- * @since 02.00.00
40- * @author Ktt Development
41- */
42- DirectoryEntry (final File directory , final boolean isFilesPreloaded , final FileHandlerAdapter adapter , final boolean isWalkthrough ) throws IOException {
43- if (!directory .exists () || !directory .isDirectory ())
44- throw new FileNotFoundException ("Directory at " + directory .getAbsolutePath () + " was not found" );
25+ private final Map <String ,FileEntry > files = new ConcurrentHashMap <>(); // preload/watchload only
4526
27+
28+ DirectoryEntry (final File directory , final FileHandlerAdapter adapter , final ByteLoadingOption loadingOption , final boolean isWalkthrough ) throws IOException {
4629 this .directory = directory ;
47- this .isFilesPreloaded = isFilesPreloaded ;
48- this .isWalkthrough = isWalkthrough ;
4930 this .adapter = adapter ;
31+ this .loadingOption = loadingOption ;
32+ this .isWalkthrough = isWalkthrough ;
5033
51- if (isFilesPreloaded ){
52- if (!isWalkthrough ){
53- final File [] listFiles = directory .listFiles ();
54- for (final File file : (listFiles == null ) ? new File [0 ] : listFiles )
55- if (!file .isDirectory ())
56- files .put (
57- getContext (adapter .getName (file )),
58- new FileEntry (file , true , adapter )
59- );
60- }else {
61- final Path dirPath = directory .toPath ();
34+ if (loadingOption == ByteLoadingOption .WATCHLOAD ){
6235
63- Files .walk (dirPath ).filter (path -> path .toFile ().isDirectory ()).forEach (path -> {
64- final File pathFile = path .toFile ();
65- final String rel = dirPath .relativize (path ).toString ();
36+ }else if (loadingOption == ByteLoadingOption .PRELOAD ){
37+ if (!isWalkthrough ){
38+ final File [] listFiles = Objects .requireNonNullElse (directory .listFiles (),new File [0 ]);
39+ for (final File file : listFiles )
40+ if (!file .isDirectory ())
41+ files .put (
42+ getContext (adapter .getName (file )),
43+ new FileEntry (file ,adapter ,ByteLoadingOption .PRELOAD )
44+ );
45+ }else {
46+ final Path dirPath = directory .toPath ();
6647
67- final File [] listFiles = pathFile .listFiles ();
68- for (final File file : (listFiles == null ) ? new File [0 ] : listFiles ){
69- try {
48+ Files .walk (dirPath ).filter (path -> path .toFile ().isDirectory ()).forEach (path -> {
49+ final File p2f = path .toFile ();
50+ final String rel = dirPath .relativize (path ).toString ();
51+
52+ final File [] listFiles = Objects .requireNonNullElse (p2f .listFiles (), new File [0 ]);
53+ for (final File file : listFiles )
7054 files .put (
7155 (rel .isEmpty () || rel .equals ("/" ) || rel .equals ("\\ " ) ? "" : getContext (rel )) + getContext (adapter .getName (file )),
72- new FileEntry (file , true , adapter )
56+ new FileEntry (file ,adapter , loadingOption )
7357 );
74- }catch (final FileNotFoundException ignored ){
75- // #listFiles assume that all files exist, so this exception should never occur unless the user modified the directory mid-read.
76- }
77- }
78- });
79- }
58+ });
59+ }
8060 }
8161 }
8262
@@ -102,8 +82,9 @@ public final boolean isWalkthrough(){
10282 * @since 02.00.00
10383 * @author Ktt Development
10484 */
85+ @ Deprecated
10586 public final boolean isFilesPreloaded (){
106- return isFilesPreloaded ;
87+ return loadingOption != ByteLoadingOption . LIVELOAD ;
10788 }
10889
10990//
@@ -132,7 +113,7 @@ public final File getDirectory(){
132113 * @since 02.00.00
133114 * @author Ktt Development
134115 */
135- public HashMap <String , FileEntry > getFiles (){
116+ public Map <String , FileEntry > getFiles (){
136117 return files ;
137118 }
138119
@@ -149,7 +130,7 @@ public HashMap<String, FileEntry> getFiles(){
149130 */
150131 public final File getFile (final String path ){
151132 final String rel = getContext (path );
152- if (isFilesPreloaded ){
133+ if (loadingOption != ByteLoadingOption . LIVELOAD ){
153134 String match = "" ;
154135 for (final String key : files .keySet ())
155136 if (rel .startsWith (key ) && key .startsWith (match ))
@@ -188,7 +169,7 @@ public final File getFile(final String path){
188169 */
189170 public final byte [] getBytes (final String path ){
190171 final String rel = getContext (path );
191- if (isFilesPreloaded ){
172+ if (loadingOption != ByteLoadingOption . LIVELOAD ){
192173 String match = "" ;
193174 for (final String key : files .keySet ())
194175 if (rel .startsWith (key ) && key .startsWith (match ))
@@ -208,6 +189,19 @@ public final byte[] getBytes(final String path){
208189 }
209190 }
210191
192+ /**
193+ * Returns the file's byte loading option.
194+ *
195+ * @return file loading option
196+ *
197+ * @see ByteLoadingOption
198+ * @since 03.05.00
199+ * @author Ktt Development
200+ */
201+ public final ByteLoadingOption getLoadingOption (){
202+ return loadingOption ;
203+ }
204+
211205//
212206
213207 private static String getContext (final String path ){
@@ -220,18 +214,16 @@ private static String getContext(final String path){
220214//
221215
222216
223- @ SuppressWarnings ("StringBufferReplaceableByString" )
224217 @ Override
225218 public String toString (){
226- final StringBuilder OUT = new StringBuilder ();
227- OUT .append ("DirectoryEntry" ) .append ("{" );
228- OUT .append ("isWalkthrough" ) .append ("=" ) .append (isWalkthrough ) .append (", " );
229- OUT .append ("isFilePreloaded" ) .append ("=" ) .append (isFilesPreloaded ) .append (", " );
230- OUT .append ("directory" ) .append ("=" ) .append (directory ) .append (", " );
231- OUT .append ("(preloaded) files" ) .append ("=" ) .append (files ) .append (", " );
232- OUT .append ("adapter" ) .append ("=" ) .append (adapter );
233- OUT .append ("}" );
234- return OUT .toString ();
219+ return
220+ "DirectoryEntry" + '{' +
221+ "directory" + '=' + directory + ", " +
222+ "adapter" + '=' + adapter + ", " +
223+ "loadingOption" + '=' + loadingOption + ", " +
224+ "isWalkthrough" + '=' + isWalkthrough + ", " +
225+ "files" + '=' + files +
226+ '}' ;
235227 }
236228
237229}
0 commit comments