/
ByteSizeParser.java
214 lines (183 loc) · 6.63 KB
/
ByteSizeParser.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* dCache - http://www.dcache.org/
*
* Copyright (C) 2018 Deutsches Elektronen-Synchrotron
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dcache.util;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dcache.util.ByteUnits.Representation;
import static java.util.Objects.requireNonNull;
/**
* This class provides a fluent interface for parsing String representations
* of byte sizes. Examples include parsing {@literal "20 MiB"} to
* {@literal 20_971_520} and {@literal "3GB"} to either {@literal 3_000_000_000}
* or {@literal 3_221_225_472} depending on the representation (ISO or JEDEC,
* respectively).
*/
public class ByteSizeParser
{
/**
* Allowed input types.
*/
public enum NumericalInput
{
INTEGER("[-+]?\\d*") {
@Override
protected long convert(String givenValue, ByteUnit givenUnits, ByteUnit targetUnits, Coercion coersion) {
return targetUnits.convert(Long.parseLong(givenValue), givenUnits);
}
},
FLOATING_POINT("[-+]?\\d*(?:\\.\\d*)?") {
@Override
protected long convert(String givenValue, ByteUnit givenUnits, ByteUnit targetUnits, Coercion coersion) {
double value = Double.parseDouble(givenValue);
return coersion.toLong(targetUnits.convert(value, givenUnits));
}
};
private final String regularExpression;
private NumericalInput(String re)
{
this.regularExpression = re;
}
abstract long convert(String value, ByteUnit givenUnits, ByteUnit targetUnits, Coercion coersion);
}
/**
* How to convert a floating point value to an integer value.
*/
public enum Coercion
{
CEIL {
@Override
public long toLong(double value) {
return (long) Math.ceil(value);
}
},
FLOOR {
@Override
public long toLong(double value) {
return (long) Math.floor(value);
}
},
ROUND {
@Override
public long toLong(double value) {
return (long) Math.round(value);
}
};
abstract long toLong(double value);
}
/**
* Whether the units are required or are optional. If optional, a default
* of ByteUnit.BYTES is used.
*/
public enum UnitPresence
{
OPTIONAL,
REQUIRED
}
/**
* How to parse whitespace between the numerical part and the units.
*/
public enum Whitespace
{
REQUIRED(" "), OPTIONAL(" ?"), NOT_ALLOWED("");
private final String regularExpression;
private Whitespace(String re)
{
this.regularExpression = re;
}
}
public static class Builder
{
private final Representation representation;
private NumericalInput input = NumericalInput.FLOATING_POINT;
private Coercion coersion = Coercion.ROUND;
private Whitespace whitespace = Whitespace.OPTIONAL;
private UnitPresence unitPresence = UnitPresence.OPTIONAL;
private ByteUnit defaultUnits = ByteUnit.BYTES;
private Builder(Representation representation)
{
this.representation = requireNonNull(representation);
}
public Builder withCoersion(Coercion coersion)
{
this.coersion = requireNonNull(coersion);
return this;
}
public Builder withInput(NumericalInput input)
{
this.input = requireNonNull(input);
return this;
}
public Builder withWhitespace(Whitespace whitespace)
{
this.whitespace = requireNonNull(whitespace);
return this;
}
public Builder withUnits(UnitPresence presence)
{
this.unitPresence = presence;
return this;
}
/**
* Which ByteUnit to use if units are optional and the parse value
* does not specify any units.
*/
public Builder withDefaultUnits(ByteUnit units)
{
defaultUnits = units;
return this;
}
public long parse(String value, ByteUnit targetUnits) throws NumberFormatException
{
String whitespaceAndUnit = whitespace.regularExpression + "(?<unit>\\p{Alpha}+)";
String whitespaceAndUnitWithPresence = unitPresence == UnitPresence.REQUIRED
? whitespaceAndUnit
: ("(?:" + whitespaceAndUnit + ")?");
Pattern pattern = Pattern.compile("(?<number>" + input.regularExpression + ")"
+ whitespaceAndUnitWithPresence);
Matcher m = pattern.matcher(value);
if (!m.matches()) {
throw new NumberFormatException("Bad input \"" + value + "\" does not match " + pattern);
}
ByteUnit givenUnits = Optional.ofNullable(m.group("unit"))
.map(s -> representation.parse(s).orElseThrow(() -> new NumberFormatException("Unknown unit \"" + s + "\"")))
.orElse(defaultUnits);
return input.convert(m.group("number"), givenUnits, targetUnits, coersion);
}
public long parse(String value) throws NumberFormatException
{
return parse(value, ByteUnit.BYTES);
}
}
private ByteSizeParser()
{
// prevent instantiation
}
/**
* Create a new Builder that parses the input with the specified
* representation. By default, the parser accepts floating-point values,
* a space between numerical value and units is optional, and the numerical
* value is rounded to the nearest integer value.
* @param representation How a ByteUnit is represented.
* @return a Builder that may be configured before use in parsing a String.
*/
public static Builder using(Representation representation)
{
return new Builder(representation);
}
}