Skip to content

Commit 1a499ec

Browse files
feat: Format Byte Algorithm to String package(issue #253) (#254)
1 parent da52a1f commit 1a499ec

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,43 @@ public class KMPSubstringSearchSnippet {
23022302
}
23032303
```
23042304

2305+
2306+
### Format Bytes
2307+
2308+
```java
2309+
public class FormatBytesSnippet {
2310+
2311+
/**
2312+
* Convert bytes into Human readable form.
2313+
*
2314+
* @param bytes The value in bytes needed to be converted
2315+
* @return String the converted human-readable form
2316+
*/
2317+
2318+
public static String formatBytes(long bytes) {
2319+
double kb = 1024;
2320+
double mb = kb * 1024;
2321+
double gb = mb * 1024;
2322+
double tb = gb * 1024;
2323+
if ((bytes >= 0) && (bytes < kb)) {
2324+
return bytes + " B";
2325+
} else if ((bytes >= kb) && (bytes < mb)) {
2326+
return String.format("%.2f KB", bytes / kb);
2327+
} else if ((bytes >= mb) && (bytes < gb)) {
2328+
return String.format("%.2f MB", bytes / mb);
2329+
} else if ((bytes >= gb) && (bytes < tb)) {
2330+
return String.format("%.2f GB", bytes / gb);
2331+
} else if (bytes >= tb) {
2332+
return String.format("%.2f TB", bytes / tb);
2333+
} else {
2334+
return "Invalid Input";
2335+
}
2336+
}
2337+
}
2338+
```
2339+
2340+
2341+
23052342
## Thread
23062343

23072344
### Thread Pool
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2024 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package string;
26+
27+
/**
28+
* FormatBytesSnippet.
29+
*/
30+
31+
public class FormatBytesSnippet {
32+
33+
/**
34+
* Convert bytes into Human readable form.
35+
*
36+
* @param bytes The value in bytes needed to be converted
37+
* @return String the converted human-readable form
38+
*/
39+
public static String formatBytes(long bytes) {
40+
double kb = 1024;
41+
double mb = kb * 1024;
42+
double gb = mb * 1024;
43+
double tb = gb * 1024;
44+
if ((bytes >= 0) && (bytes < kb)) {
45+
return bytes + " B";
46+
} else if ((bytes >= kb) && (bytes < mb)) {
47+
return String.format("%.2f KB", bytes / kb);
48+
} else if ((bytes >= mb) && (bytes < gb)) {
49+
return String.format("%.2f MB", bytes / mb);
50+
} else if ((bytes >= gb) && (bytes < tb)) {
51+
return String.format("%.2f GB", bytes / gb);
52+
} else if (bytes >= tb) {
53+
return String.format("%.2f TB", bytes / tb);
54+
} else {
55+
return "Invalid Input";
56+
}
57+
}
58+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2022 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package string;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
29+
import org.junit.jupiter.api.Test;
30+
31+
/*
32+
* Tests for 30 Seconds of Java code library
33+
*
34+
*/
35+
class FormatBytesSnippetTest {
36+
/**
37+
* * Tests for {@link FormatBytesSnippet#formatBytes(long)}.
38+
*/
39+
@Test
40+
void formatBytes() {
41+
assertEquals("1.46 MB", FormatBytesSnippet.formatBytes(1536000));
42+
assertEquals("4.00 GB", FormatBytesSnippet.formatBytes(4294967296L));
43+
assertEquals("3.00 TB", FormatBytesSnippet.formatBytes(3L * 1024 * 1024 * 1024 * 1024));
44+
assertEquals("1024.00 MB", FormatBytesSnippet.formatBytes(1024L * 1024 * 1024 - 1));
45+
assertEquals("Invalid Input", FormatBytesSnippet.formatBytes(-1024));
46+
assertEquals("8388608.00 TB", FormatBytesSnippet.formatBytes(Long.MAX_VALUE));
47+
}
48+
}

0 commit comments

Comments
 (0)