Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
adapt
ace
Commits
3f8662d8
Commit
3f8662d8
authored
Apr 04, 2017
by
Michael Ritter
Browse files
Add formatter to handle file sizes from ingest summary queries
parent
1504d7dc
Changes
1
Hide whitespace changes
Inline
Side-by-side
ace-am/src/main/java/edu/umiacs/ace/util/FileSizeFormatter.java
0 → 100644
View file @
3f8662d8
package
edu.umiacs.ace.util
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
/**
* Similar to the {@link FileSizeHandler}, but for KiB/MiB/etc when
* we may not have access to the tlds
*
* Created by shake on 4/4/17.
*/
public
class
FileSizeFormatter
{
private
Unit
unit
;
public
FileSizeFormatter
(
String
unit
)
{
this
.
unit
=
Unit
.
fromString
(
unit
);
}
public
String
format
(
BigDecimal
decimal
)
{
BigDecimal
result
=
decimal
.
divide
(
unit
.
divisor
,
5
,
RoundingMode
.
HALF_UP
);
String
displayUnit
=
unit
==
Unit
.
B
?
""
:
unit
.
name
();
return
result
.
stripTrailingZeros
().
toPlainString
()
+
" "
+
displayUnit
;
}
public
enum
Unit
{
B
(
0
),
KiB
(
1
),
MiB
(
2
),
GiB
(
3
),
TiB
(
4
);
private
BigDecimal
divisor
;
Unit
(
int
pow
)
{
this
.
divisor
=
new
BigDecimal
(
Math
.
pow
(
1024
,
pow
));
}
private
static
Unit
fromString
(
String
unit
)
{
if
(
unit
==
null
)
{
return
B
;
}
if
(
unit
.
equalsIgnoreCase
(
"kib"
))
{
return
KiB
;
}
else
if
(
unit
.
equalsIgnoreCase
(
"mib"
))
{
return
MiB
;
}
else
if
(
unit
.
equalsIgnoreCase
(
"gib"
))
{
return
GiB
;
}
else
if
(
unit
.
equalsIgnoreCase
(
"tib"
))
{
return
TiB
;
}
return
B
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment