isclaimer: This is not an official Google product.
Mug (
)
A small Java 8+ string processing and streams library (javadoc), widely used in Google’s internal Java codebase, with 0 deps (Proto, BigQuery, Guava addons are in separate artifacts). 
Full Spectrum of String Utilities
- ✅
Substring – simple and composable substring extraction & manipulation
→ Substring.between("(", ")").from("call(foo)") → "foo"
- ✅
StringFormat – compile-time-safe bidirectional parsing/formatting
→ new StringFormat("/home/{user}/{date}").parse(filePath, (user, date) -> ...)
- ✅
Parser – everyday string parsing easier than and beyond regex
sequence(word().followedBy("="), digits(), Map::entry)
.zeroOrMoreDelimitedBy(",")
.between("{", "}") // {k1=100, k2=200, k3=300, ...}
.parse(input);
DateTimeFormats – define datetime formats by examples
→ DateTimeFormatter format = formatOf("2024-03-14 10.20:00.123 America/New_York")
Streams
- ✅
BiStream – streams Map and pair-wise collections
→ BiStream.zip(keys, values).toMap()
- ✅
MoreStreams – extra stream utilities
Stream<List<Double>> greenDays = MoreStreams.groupConsecutive(
stockPrices, (p1, p2) -> p1 <= p2, toUnmodifiableList());
Others
- ✅
@ParametersMustMatchByNamerecord Profile(String userId, String userName) {}
new Profile(user.id(), user.name()) Compiles ✅
new Profile(user.name(), user.id()) Does Not Compile ❌
- ✅
SafeSql – compile-time-enforced safe, composable SQL template
→ SafeSql.of("select id, `{col}` from Users where id = {id}", col, id)
More tools
- [`Iteration`](./mug/wiki/Iteration-Explained) - implement lazy stream with recursive code
- [`BinarySearch`](/mug/mug-guava/src/main/java/com/google/guava/labs/collect/) - solve LeetCode binary search problems
→ `BinarySearch.inSortedArrayWithTolerance(doubleArray, 0.0001).find(target)`
- [`StructuredConcurrency`](/mug/mug/src/main/java/com/google/mu/util/concurrent/) - simple structured concurrency on virtual threads
→ `concurrently(() -> fetchArm(), () -> fetchLeg(), (arm, leg) -> makeRobot(arm, leg))`
- [`Optionals`](./mug/apidocs/com/google/mu/util/Optionals.html)
→ `return optionally(obj.hasFoo(), obj::getFoo);`
Installation
##### Maven
Add the following to pom.xml:
```
com.google.mug
mug
10.2
```
Add `mug-errorprone` to your annotationProcessorPaths:
```
maven-compiler-plugin
com.google.errorprone
error_prone_core
2.23.0
com.google.mug
mug-errorprone
10.2
```
SafeSql ([javadoc](https://google.github.io/mug/apidocs/com/google/mu/safesql/package-summary.html)):
```
com.google.mug
mug-safesql
10.2
```
Dot Parse Combinators ([javadoc](https://google.github.io/mug/apidocs/com/google/common/labs/parse/package-summary.html)):
```
com.google.mug
dot-parse
10.2
```
Protobuf utils ([javadoc](https://google.github.io/mug/apidocs/com/google/mu/protobuf/util/package-summary.html)):
```
com.google.mug
mug-protobuf
10.2
```
##### Gradle
Add to build.gradle:
```
implementation 'com.google.mug:mug:10.2'
implementation 'com.google.mug:mug-safesql:10.2'
implementation 'com.google.mug:dot-parse:10.2'
implementation 'com.google.mug:mug-guava:10.2'
implementation 'com.google.mug:mug-protobuf:10.2'
```