Skip to content

Commit

Permalink
Introduce formatter methods to customize version output (#252)
Browse files Browse the repository at this point in the history
* Intriduce formatter methods to customize version output

* Cleanup
  • Loading branch information
piotrooo committed Apr 25, 2024
1 parent a01df7f commit 22bf498
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and [Ivy](https://ant.apache.org/ivy/history/latest-milestone/settings/version-m
* [Internal](#internal)
* [Modifying the version](#modifying-the-version)
* [Builder](#builder)
* [Formatting](#formatting)
* [Contributing](#contributing)
* [Thanks](#thanks)

Expand Down Expand Up @@ -254,6 +255,19 @@ And is an equivalent of:
Semver semver = new Semver("1.2.0+5bb76cdb");
```

### Formatting

Sometimes you want to format `Semver` using custom formatters. You can do this using
a `format(Function<Semver, String> formatter)` method from the `Semver` class:

```java
Semver semver = new Semver("1.2.3-alpha.1+sha.1234");
String customVersion = semver.format(sem -> format("%d:%d:%d", sem.getMajor(), sem.getMinor(), sem.getPatch())); // 1:2:2
```

There is also a method in the `SemverBuilder` called `toVersion(Function<Semver, String> formatter)` which behaves
exactly the same.

## Contributing

Any pull request or bug report are welcome!
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/semver4j/Semver.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.semver4j.internal.StrictParser.Version;

import java.util.*;
import java.util.function.Function;

import static java.lang.String.format;
import static java.lang.String.join;
import static java.util.Collections.emptyList;
import static java.util.Objects.hash;
Expand Down Expand Up @@ -570,6 +570,13 @@ public boolean satisfies(@NotNull final RangesList rangesList) {
return rangesList.isSatisfiedBy(this);
}

/**
* Format {@link Semver} object using custom formatting rules.
*/
public String format(Function<Semver, String> formatter) {
return formatter.apply(this);
}

@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
Expand Down Expand Up @@ -685,7 +692,7 @@ public Semver toSemver() {
*/
@NotNull
public String toVersion() {
String resultVersion = format(Locale.ROOT, "%d.%d.%d", major, minor, patch);
String resultVersion = String.format(Locale.ROOT, "%d.%d.%d", major, minor, patch);
if (!preRelease.isEmpty()) {
resultVersion += "-" + join(".", preRelease);
}
Expand All @@ -694,5 +701,12 @@ public String toVersion() {
}
return resultVersion;
}

/**
* Format {@link Semver} object using custom formatting rules.
*/
public String toVersion(Function<Semver, String> formatter) {
return formatter.apply(toSemver());
}
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/semver4j/SemverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1237,4 +1237,47 @@ void shouldBuildBasicSemver() {
//then
assertThat(semver.getVersion()).isEqualTo("1.2.3");
}

@Test
void shouldUseCustomFormatterInBuilder() {
//given
Semver.Builder builder = Semver.of()
.withMajor(1)
.withMinor(2)
.withPatch(3)
.withPreRelease("alpha")
.withBuild("5bb76cdb");

//when
String version = builder.toVersion(semver -> {
String preRelease = join("&", semver.getPreRelease());
String build = join("&", semver.getBuild());
return format(Locale.ROOT, "%d:%d:%d|%s*%s", semver.getMajor(), semver.getMinor(), semver.getPatch(), preRelease, build);
});

//then
assertThat(version).isEqualTo("1:2:3|alpha*5bb76cdb");
}

@Test
void shouldUseCustomFormatter() {
//given
Semver actualSemver = Semver.of()
.withMajor(1)
.withMinor(2)
.withPatch(3)
.withPreRelease("alpha")
.withBuild("5bb76cdb")
.toSemver();

//when
String version = actualSemver.format(semver -> {
String preRelease = join("&", semver.getPreRelease());
String build = join("&", semver.getBuild());
return format(Locale.ROOT, "%d:%d:%d|%s*%s", semver.getMajor(), semver.getMinor(), semver.getPatch(), preRelease, build);
});

//then
assertThat(version).isEqualTo("1:2:3|alpha*5bb76cdb");
}
}

0 comments on commit 22bf498

Please sign in to comment.