0
# Dropwizard Util
1
2
Dropwizard Util provides a comprehensive set of utility classes designed for Java applications, particularly those using the Dropwizard framework. It includes utilities for handling data sizes with human-readable parsing, duration parsing and manipulation, enum utilities with flexible string conversion, generic type utilities for reflection, resource management utilities, exception handling utilities, JAR location utilities, and a direct executor service implementation.
3
4
## Package Information
5
6
- **Package Name**: dropwizard-util
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Group ID**: io.dropwizard
10
- **Artifact ID**: dropwizard-util
11
- **Version**: 4.0.14
12
- **Installation**: Add to your Maven `pom.xml`:
13
14
```xml
15
<dependency>
16
<groupId>io.dropwizard</groupId>
17
<artifactId>dropwizard-util</artifactId>
18
<version>4.0.14</version>
19
</dependency>
20
```
21
22
## Core Imports
23
24
```java
25
import io.dropwizard.util.DataSize;
26
import io.dropwizard.util.DataSizeUnit;
27
import io.dropwizard.util.Duration;
28
import io.dropwizard.util.Enums;
29
import io.dropwizard.util.Generics;
30
import io.dropwizard.util.Resources;
31
import io.dropwizard.util.Throwables;
32
import io.dropwizard.util.JarLocation;
33
import io.dropwizard.util.DirectExecutorService;
34
```
35
36
## Basic Usage
37
38
```java
39
import io.dropwizard.util.DataSize;
40
import io.dropwizard.util.Duration;
41
42
// Parse human-readable data sizes
43
DataSize size = DataSize.parse("128 MB");
44
long bytes = size.toBytes();
45
46
// Parse human-readable durations
47
Duration timeout = Duration.parse("30 seconds");
48
long millis = timeout.toMilliseconds();
49
50
// Create programmatically
51
DataSize gigabyte = DataSize.gigabytes(1);
52
Duration fiveMinutes = Duration.minutes(5);
53
```
54
55
## Architecture
56
57
The Dropwizard Util package follows these key design patterns:
58
59
- **Immutable Value Objects**: DataSize and Duration are immutable and thread-safe
60
- **Static Factory Methods**: Convenient creation methods like `DataSize.gigabytes(1)`
61
- **Human-Readable Parsing**: String parsing with flexible format support
62
- **Utility Class Pattern**: Static helper methods in utility classes
63
- **Builder Pattern**: For complex object construction where applicable
64
- **Type Safety**: Strong typing with enums for units and constants
65
66
## Capabilities
67
68
### Data Size Management
69
70
Handle data sizes with SI and IEC prefix support, including human-readable parsing and unit conversion.
71
72
```java { .api }
73
public class DataSize implements Comparable<DataSize>, Serializable {
74
public static DataSize bytes(long count);
75
public static DataSize kilobytes(long count);
76
public static DataSize megabytes(long count);
77
public static DataSize gigabytes(long count);
78
public static DataSize terabytes(long count);
79
public static DataSize petabytes(long count);
80
public static DataSize kibibytes(long count);
81
public static DataSize mebibytes(long count);
82
public static DataSize gibibytes(long count);
83
public static DataSize tebibytes(long count);
84
public static DataSize pebibytes(long count);
85
public static DataSize parse(CharSequence size);
86
public static DataSize parse(CharSequence size, DataSizeUnit defaultUnit);
87
public long getQuantity();
88
public DataSizeUnit getUnit();
89
public long toBytes();
90
public long toKilobytes();
91
public long toMegabytes();
92
public long toGigabytes();
93
public long toTerabytes();
94
public long toPetabytes();
95
public long toKibibytes();
96
public long toMebibytes();
97
public long toGibibytes();
98
public long toTebibytes();
99
public long toPebibytes();
100
}
101
102
public enum DataSizeUnit {
103
BYTES, KILOBYTES, MEGABYTES, GIGABYTES, TERABYTES, PETABYTES,
104
KIBIBYTES, MEBIBYTES, GIBIBYTES, TEBIBYTES, PEBIBYTES;
105
106
public long convert(long size, DataSizeUnit unit);
107
public long toBytes(long l);
108
public long toKilobytes(long l);
109
public long toMegabytes(long l);
110
public long toGigabytes(long l);
111
public long toTerabytes(long l);
112
public long toPetabytes(long l);
113
public long toKibibytes(long l);
114
public long toMebibytes(long l);
115
public long toGibibytes(long l);
116
public long toTebibytes(long l);
117
public long toPebibytes(long l);
118
}
119
```
120
121
[Data Size and Units](./data-size.md)
122
123
### Duration Management
124
125
Parse and manipulate duration values with support for various time units and conversion to standard Java types.
126
127
```java { .api }
128
public class Duration implements Comparable<Duration>, Serializable {
129
public static Duration nanoseconds(long count);
130
public static Duration microseconds(long count);
131
public static Duration milliseconds(long count);
132
public static Duration seconds(long count);
133
public static Duration minutes(long count);
134
public static Duration hours(long count);
135
public static Duration days(long count);
136
public static Duration parse(String duration);
137
public long getQuantity();
138
public TimeUnit getUnit();
139
public long toNanoseconds();
140
public long toMicroseconds();
141
public long toMilliseconds();
142
public long toSeconds();
143
public long toMinutes();
144
public long toHours();
145
public long toDays();
146
public java.time.Duration toJavaDuration();
147
}
148
```
149
150
[Duration Utilities](./duration.md)
151
152
### Enum and Type Utilities
153
154
Flexible enum conversion with fuzzy matching and generic type parameter reflection.
155
156
```java { .api }
157
public class Enums {
158
public static Enum<?> fromStringFuzzy(String value, Enum<?>[] constants);
159
}
160
161
public class Generics {
162
public static Class<?> getTypeParameter(Class<?> klass);
163
public static <T> Class<T> getTypeParameter(Class<?> klass, Class<? super T> bound);
164
}
165
```
166
167
[Enum and Generic Utilities](./enum-generics.md)
168
169
### Resource and Exception Management
170
171
Resource loading utilities and exception chain analysis tools.
172
173
```java { .api }
174
public final class Resources {
175
public static URL getResource(String resourceName);
176
}
177
178
public final class Throwables {
179
public static Optional<Throwable> findThrowableInChain(
180
Predicate<Throwable> condition,
181
Throwable t
182
);
183
}
184
```
185
186
[Resource and Exception Utilities](./resource-exception.md)
187
188
### JAR Location and Execution Services
189
190
JAR metadata extraction and direct task execution capabilities.
191
192
```java { .api }
193
public class JarLocation {
194
public JarLocation(Class<?> klass);
195
public Optional<String> getVersion();
196
public String toString();
197
}
198
199
public class DirectExecutorService extends AbstractExecutorService {
200
public void execute(Runnable command);
201
public void shutdown();
202
public List<Runnable> shutdownNow();
203
public boolean isShutdown();
204
public boolean isTerminated();
205
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
206
}
207
```
208
209
[JAR Location and Executor Services](./jar-executor.md)
210
211
## Types
212
213
### Core Value Objects
214
215
```java { .api }
216
// Immutable data size representation
217
public class DataSize implements Comparable<DataSize>, Serializable {
218
// Factory methods and instance methods as shown above
219
}
220
221
// Immutable duration representation
222
public class Duration implements Comparable<Duration>, Serializable {
223
// Factory methods and instance methods as shown above
224
}
225
226
// JAR location encapsulation
227
public class JarLocation {
228
// Constructor and methods as shown above
229
}
230
231
// Direct execution service
232
public class DirectExecutorService extends AbstractExecutorService {
233
// Execution methods as shown above
234
}
235
```
236
237
### Enums
238
239
```java { .api }
240
public enum DataSizeUnit {
241
BYTES(8L),
242
KILOBYTES(8L * 1000L),
243
MEGABYTES(8L * 1000L * 1000L),
244
GIGABYTES(8L * 1000L * 1000L * 1000L),
245
TERABYTES(8L * 1000L * 1000L * 1000L * 1000L),
246
PETABYTES(8L * 1000L * 1000L * 1000L * 1000L * 1000L),
247
KIBIBYTES(8L * 1024L),
248
MEBIBYTES(8L * 1024L * 1024L),
249
GIBIBYTES(8L * 1024L * 1024L * 1024L),
250
TEBIBYTES(8L * 1024L * 1024L * 1024L * 1024L),
251
PEBIBYTES(8L * 1024L * 1024L * 1024L * 1024L * 1024L);
252
}
253
```
254
255
### Standard Java Types Used
256
257
- `java.util.concurrent.TimeUnit` - For duration time units
258
- `java.util.concurrent.AbstractExecutorService` - Base for DirectExecutorService
259
- `java.time.Duration` - For Duration conversion
260
- `java.lang.reflect.Type` - For generic type operations
261
- `java.util.function.Predicate<Throwable>` - For exception filtering
262
- `java.util.Optional<T>` - For optional return values
263
- `java.net.URL` - For resource URLs