1 package org
.argeo
.eclipse
.ui
.fs
;
3 import java
.io
.IOException
;
4 import java
.nio
.file
.DirectoryIteratorException
;
5 import java
.nio
.file
.DirectoryStream
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Arrays
;
10 import java
.util
.List
;
12 /** Centralise additional utilitary methods to manage Java7 NIO files */
13 public class FsUiUtils
{
17 * http://programming.guide/java/formatting-byte-size-to-human-readable-format.html
19 public static String
humanReadableByteCount(long bytes
, boolean si
) {
20 int unit
= si ?
1000 : 1024;
23 int exp
= (int) (Math
.log(bytes
) / Math
.log(unit
));
24 String pre
= (si ?
"kMGTPE" : "KMGTPE").charAt(exp
- 1) + (si ?
"" : "i");
25 return String
.format("%.1f %sB", bytes
/ Math
.pow(unit
, exp
), pre
);
28 public static Path
[] getChildren(Path parent
, String filter
, boolean showHiddenItems
, boolean folderFirst
,
29 String orderProperty
, boolean reverseOrder
) {
30 if (!Files
.isDirectory(parent
))
32 List
<Pair
> pairs
= new ArrayList
<>();
33 try (DirectoryStream
<Path
> stream
= Files
.newDirectoryStream(parent
, filter
)) {
34 loop
: for (Path entry
: stream
) {
36 if (Files
.isHidden(entry
))
38 switch (orderProperty
) {
39 case FsUiConstants
.PROPERTY_SIZE
:
41 pairs
.add(new LPair(entry
, Files
.size(entry
), Files
.isDirectory(entry
)));
43 pairs
.add(new LPair(entry
, Files
.size(entry
)));
45 case FsUiConstants
.PROPERTY_LAST_MODIFIED
:
47 pairs
.add(new LPair(entry
, Files
.getLastModifiedTime(entry
).toMillis(),
48 Files
.isDirectory(entry
)));
50 pairs
.add(new LPair(entry
, Files
.getLastModifiedTime(entry
).toMillis()));
52 case FsUiConstants
.PROPERTY_NAME
:
54 pairs
.add(new SPair(entry
, entry
.getFileName().toString(), Files
.isDirectory(entry
)));
56 pairs
.add(new SPair(entry
, entry
.getFileName().toString()));
59 throw new FsUiException("Unable to prepare sort for property " + orderProperty
);
62 Pair
[] rows
= pairs
.toArray(new Pair
[0]);
64 Path
[] results
= new Path
[rows
.length
];
66 int j
= rows
.length
- 1;
67 for (int i
= 0; i
< rows
.length
; i
++)
68 results
[i
] = rows
[j
- i
].p
;
70 for (int i
= 0; i
< rows
.length
; i
++)
71 results
[i
] = rows
[i
].p
;
73 } catch (IOException
| DirectoryIteratorException e
) {
74 throw new FsUiException("Unable to filter " + parent
+ " children with filter " + filter
, e
);
78 static abstract class Pair
implements Comparable
<Object
> {
83 static class LPair
extends Pair
{
86 public LPair(Path path
, long propValue
) {
91 public LPair(Path path
, long propValue
, boolean isDir
) {
97 public int compareTo(Object o
) {
99 Boolean j
= ((LPair
) o
).i
;
100 if (i
.booleanValue() != j
.booleanValue())
101 return i
.booleanValue() ?
-1 : 1;
103 long u
= ((LPair
) o
).v
;
104 return v
< u ?
-1 : v
== u ?
0 : 1;
108 static class SPair
extends Pair
{
111 public SPair(Path path
, String propValue
) {
116 public SPair(Path path
, String propValue
, boolean isDir
) {
122 public int compareTo(Object o
) {
124 Boolean j
= ((SPair
) o
).i
;
125 if (i
.booleanValue() != j
.booleanValue())
126 return i
.booleanValue() ?
-1 : 1;
128 String u
= ((SPair
) o
).v
;
129 return v
.compareTo(u
);