You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -3,25 +3,34 @@ Disclaimer: This is not an official Google product.
3
3
# Mug
4
4
A small Java 8+ utilities library ([javadoc](http://google.github.io/mug/apidocs/index.html)), widely used in Google's internal Java codebase, with **0 deps** (Proto, BigQuery, Guava addons are in separate artifacts). 
5
5
6
-
Offers:
7
-
***Intuitive to read**, powerful string manipulation ([StringFormat](https://github.com/google/mug/wiki/StringFormat-Explained), [Substring](https://github.com/google/mug/wiki/Substring-Explained))
8
-
*`new StringFormat("/{user}-home/{yyyy}/{mm}/{dd}").parse(path, (user, yyyy, mm, dd) -> ...)`
9
-
*`String user = before(first('@')).from(email).orElseThrow();`
**Example 7: find the substring between the first and last curly braces ({) :**
151
-
```java
152
-
String body =Substring.between(first('{'), last('}'))
153
-
.from(source)
154
-
.orElseThrow(...);
155
-
```
156
-
157
-
## Stream
158
-
159
-
#### [BiStream](https://google.github.io/mug/apidocs/com/google/mu/util/stream/BiStream.html) streams pairs of objects.
160
-
161
-
This class closely mirrors JDK `Stream` API (the few extra methods of "its own" are very straight-forward). If you are familiar with Jdk stream, learning curve is minimal.
**Q:Why not `Map<Foo, Bar>` or `Multimap<Foo, Bar>`?**
237
-
238
-
A:SometimesFoo and Bar are just an arbitrary pair of objects, with no key-value relationship. Or you may not trust `Foo#equals()` and `hashCode()`.Instead, drop-in replace your `Stream<Pair<Foo, Bar>>`/`List<Pair<Foo, Bar>>` with `BiStream<Foo, Bar>`/`BiCollection<Foo, Bar>` to get better readability.
239
-
240
-
**Q:Why not `Stream<FooAndBar>`?**
241
-
242
-
A:When you already have a proper domain object, sure. But you might find it cumbersome to define a bunch of FooAndBar, PatioChairAndKitchenSink one-off classes especially if the relationship between the two types is only relevant in the local code context.
243
-
244
-
**Q:Why not `Stream<Pair<Foo, Bar>>`?**
245
-
246
-
A:It's distracting to read code littered with opaque method names like `getFirst()` and `getSecond()`.
**Example 2: to iterate over `Stream`s in the presence of checked exceptions or control flow:**
262
-
263
-
The `Stream` API provides `forEach()` to iterate over a stream, if you don't have to throw checked exceptions.
264
-
265
-
When checked exception is in the way, or if you need control flow (`continue`, `return` etc.), `iterateThrough()` and `iterateOnce()` can help. The following code uses `iterateThrough()` to write objects into an `ObjectOutputStream`, with `IOException` propagated:
266
-
267
-
```java
268
-
Stream<?> stream =...;
269
-
ObjectOutput out =...;
270
-
iterateThrough(stream, out::writeObject);
271
-
```
272
-
273
-
with control flow:
274
-
```java
275
-
for (Object obj : iterateOnce(stream)) {
276
-
if (...) continue;
277
-
elseif (...) return;
278
-
out.writeObject(obj);
279
-
}
280
-
```
281
-
282
-
**Example3: to merge maps:**
283
-
```java
284
-
interfacePage {
285
-
Map<Day, Long>getTrafficHistogram();
286
-
}
287
-
288
-
List<Page> pages =...;
289
-
290
-
// Merge traffic histogram across all pages of the web site
0 commit comments