Skip to content
/ mug Public

A small Java 8 library (string manipulation, BiStream, Structured Concurrency, SQL Templates)

License

Notifications You must be signed in to change notification settings

google/mug

Disclaimer: This is not an official Google product.

Mug

A small Java 8+ utilities library (javadoc), widely used in Google's internal Java codebase, with 0 deps (Proto, BigQuery, Guava addons are in separate artifacts).

Highlights

  • BiStream – streams Map and pair-wise collections
    BiStream.zip(keys, values).toMap()
  • Substring – 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) -> ...)
  • SafeSqlthe only safe SQL template (think your SQL is safe? think again!)
    SafeSql.of("select id, `{col}` from Users where id = {id}", col, id)
  • DateTimeFormats – parse datetimes by example
    DateTimeFormatter format = formatOf("2024-03-14 10:00:00.123 America/New_York")
More tools
  • Iteration - implement lazy stream with recursive code
  • BinarySearch - solve LeetCode binary search problems
    BinarySearch.inSortedArrayWithTolerance(doubleArray, 0.0001).find(target)
  • StructuredConcurrency - simple structured concurrency on virtual threads
    concurrently(() -> fetchArm(), () -> fetchLeg(), (arm, leg) -> makeRobot(arm, leg))
  • MoreStreams
    whileNotNull(queue::poll).filter(...).map(...)
  • Optionals
    return optionally(obj.hasFoo(), obj::getFoo);
Installation
Maven

Add the following to pom.xml:

  <dependency>
    <groupId>com.google.mug</groupId>
    <artifactId>mug</artifactId>
    <version>8.5</version>
  </dependency>

Add mug-errorprone to your annotationProcessorPaths:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <annotationProcessorPaths>
              <path>
                <groupId>com.google.errorprone</groupId>
                <artifactId>error_prone_core</artifactId>
                <version>2.23.0</version>
              </path>
              <path>
                <groupId>com.google.mug</groupId>
                <artifactId>mug-errorprone</artifactId>
                <version>8.5</version>
              </path>
            </annotationProcessorPaths>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

Protobuf utils (javadoc):

  <dependency>
    <groupId>com.google.mug</groupId>
    <artifactId>mug-protobuf</artifactId>
    <version>8.5</version>
  </dependency>

Guava add-ons (with SafeSql, SafeQuery and GoogleSql):

  <dependency>
    <groupId>com.google.mug</groupId>
    <artifactId>mug-guava</artifactId>
    <version>8.5</version>
  </dependency>
Gradle

Add to build.gradle:

  implementation 'com.google.mug:mug:8.5'
  implementation 'com.google.mug:mug-guava:8.5'
  implementation 'com.google.mug:mug-protobuf:8.5'