Back to directory
google avatar

mug

A small Java 8 library (string manipulation, BiStream, Structured Concurrency, SQL Templates). Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/google/mug.git

534

Stars

89

Forks

10

Watchers

Apache-2.0

License

Disclaimer: This is not an official Google product.

Mug (Coverage)

A small Java 8+ string processing and streams library (javadoc), battle tested in Google's internal codebase, with 0 deps (Proto, BigQuery, Guava addons are in separate artifacts).

Strings

  • 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, faster and beyond regex
    sequence(word().followedBy("="), digits(), Map::entry)
        .zeroOrMoreDelimitedBy(",")
        .between("{", "}")         // {k1=100, k2=200, k3=300, ...}
        .parseSkipping(Character::isWhitespace, input);
    
  • DateTimeFormats – define datetime formats by examples
    DateTimeFormatter format = formatOf("2024-03-14 10.8.10: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());
    

Compile-Time Guardrails

  • @ParametersMustMatchByNamerecord Profile(String userId, String userName) {}
    • new Profile(user.id(), user.name()) Compiles ✅
    • new Profile(user.name(), user.id()) Does Not Compile ❌
  • SafeSqlcompile-time-enforced safe, composable SQL template
    SafeSql.of("select id, `{col}` from Users where id = {id}", col, id)
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))
  • 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>10.8.1</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>10.8.1</version>
              </path>
            </annotationProcessorPaths>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

SafeSql (javadoc):

  <dependency>
    <groupId>com.google.mug</groupId>
    <artifactId>mug-safesql</artifactId>
    <version>10.8.1</version>
  </dependency>

Dot Parse Combinators (javadoc):

  <dependency>
    <groupId>com.google.mug</groupId>
    <artifactId>dot-parse</artifactId>
    <version>10.8.1</version>
  </dependency>

Protobuf utils (javadoc):

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

Gradle

Add to build.gradle:

  implementation 'com.google.mug:mug:10.8.1'
  implementation 'com.google.mug:mug-safesql:10.8.1'
  implementation 'com.google.mug:dot-parse:10.8.1'
  implementation 'com.google.mug:mug-guava:10.8.1'
  implementation 'com.google.mug:mug-protobuf:10.8.1'

Releases

Aug 8, 2026

Release 10.8.1

Download .zip

Parsers: DURATION, SIGNED_DOUBLE, UNSIGNED_DECIMAL, UNSIGNED_INTEGER, CODE_POINT etc. Parser.followedByZeroOrMore(). RegexPattern supports inline free spacing mode. Perf improvements.

Jul 21, 2026

Release 10.7

Download .zip

dot-parse

Parser.fail() for reporting custom errors. Parser.literally(Parser...) convenience method. More Parser.sequence() overloads. Suffix for more advanced left factoring.

dot-cel - faster Comm...

Jul 6, 2026

Release 10.6

Download .zip

(configurable) StackOverflowError protection against deeply nested recursive structure. Makes dot-parse the safest Java recursive descent parser.

extensive benchmarks stacking dot-parse against majo...

Jun 28, 2026

Release 10.5.1

Download .zip

Return elision works on .optional() Error message formatting bug fixes.

Jun 25, 2026

Release 10.5

Download .zip

dot-parse performance optimization (return elision) Parser.anyOf(str1, str2, ...) Parser.optionallyFollowedBy(Parser<?> suffix)