-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoryUnit.java
More file actions
147 lines (141 loc) · 4.33 KB
/
MemoryUnit.java
File metadata and controls
147 lines (141 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package dustin.utilities.memory;
/**
* Representation of basic memory units.
*/
public enum MemoryUnit
{
/** Smallest memory unit. */
BYTES,
/** "One thousand" (1024) bytes. */
KILOBYTES,
/** "One million" (1024x1024) bytes. */
MEGABYTES,
/** "One billion" (1024x1024x1024) bytes. */
GIGABYTES;
/** Number of bytes in a kilobyte. */
private final double BYTES_PER_KILOBYTE = 1024.0;
/** Number of kilobytes in a megabyte. */
private final double KILOBYTES_PER_MEGABYTE = 1024.0;
/** Number of megabytes per gigabyte. */
private final double MEGABYTES_PER_GIGABYTE = 1024.0;
/**
* Returns the number of bytes corresponding to the
* provided input for a particular unit of memory.
*
* @param input Number of units of memory.
* @return Number of bytes corresponding to the provided
* number of particular memory units.
*/
public double toBytes(final long input)
{
double bytes;
switch (this)
{
case BYTES:
bytes = input;
break;
case KILOBYTES:
bytes = input * BYTES_PER_KILOBYTE;
break;
case MEGABYTES:
bytes = input * BYTES_PER_KILOBYTE * KILOBYTES_PER_MEGABYTE;
break;
case GIGABYTES:
bytes = input * BYTES_PER_KILOBYTE * KILOBYTES_PER_MEGABYTE * MEGABYTES_PER_GIGABYTE;
break;
default :
throw new RuntimeException("No value '" + this + "' recognized for enum MemoryUnit.");
}
return bytes;
}
/**
* Returns the number of kilobytes corresponding to the
* provided input for a particular unit of memory.
*
* @param input Number of units of memory.
* @return Number of kilobytes corresponding to the provided
* number of particular memory units.
*/
public double toKiloBytes(final long input)
{
double kilobytes;
switch (this)
{
case BYTES:
kilobytes = input / BYTES_PER_KILOBYTE;
break;
case KILOBYTES:
kilobytes = input;
break;
case MEGABYTES:
kilobytes = input * KILOBYTES_PER_MEGABYTE;
break;
case GIGABYTES:
kilobytes = input * KILOBYTES_PER_MEGABYTE * MEGABYTES_PER_GIGABYTE;
break;
default:
throw new RuntimeException("No value '" + this + "' recognized for enum MemoryUnit.");
}
return kilobytes;
}
/**
* Returns the number of megabytes corresponding to the
* provided input for a particular unit of memory.
*
* @param input Number of units of memory.
* @return Number of megabytes corresponding to the provided
* number of particular memory units.
*/
public double toMegaBytes(final long input)
{
double megabytes;
switch (this)
{
case BYTES:
megabytes = input / BYTES_PER_KILOBYTE / KILOBYTES_PER_MEGABYTE;
break;
case KILOBYTES:
megabytes = input / KILOBYTES_PER_MEGABYTE;
break;
case MEGABYTES:
megabytes = input;
break;
case GIGABYTES:
megabytes = input * MEGABYTES_PER_GIGABYTE;
break;
default:
throw new RuntimeException("No value '" + this + "' recognized for enum MemoryUnit.");
}
return megabytes;
}
/**
* Returns the number of gigabytes corresponding to the
* provided input for a particular unit of memory.
*
* @param input Number of units of memory.
* @return Number of gigabytes corresponding to the provided
* number of particular memory units.
*/
public double toGigaBytes(final long input)
{
double gigabytes;
switch (this)
{
case BYTES:
gigabytes = input / BYTES_PER_KILOBYTE / KILOBYTES_PER_MEGABYTE / MEGABYTES_PER_GIGABYTE;
break;
case KILOBYTES:
gigabytes = input / KILOBYTES_PER_MEGABYTE / MEGABYTES_PER_GIGABYTE;
break;
case MEGABYTES:
gigabytes = input / MEGABYTES_PER_GIGABYTE;
break;
case GIGABYTES:
gigabytes = input;
break;
default:
throw new RuntimeException("No value '" + this + "' recognized for enum MemoryUnit.");
}
return gigabytes;
}
}