Builder configuration initialization requires text.

This commit is contained in:
Erik C. Thauvin 2024-05-17 02:05:09 -07:00
parent 258b8126bb
commit 9088716047
Signed by: erik
GPG key ID: 776702A6A2DA330E
9 changed files with 90 additions and 28 deletions

View file

@ -116,7 +116,7 @@ None of the attributes are required.
In addition to setters, a configuration builder is also available:
```java
final ReadingTime rt = new ReadingTime(Files.readString(text));
final ReadingTime rt = new ReadingTime(text);
rt.setPostfix("minute to read");
rt.setPlural("minutes to read");
```
@ -125,8 +125,7 @@ or
```java
final Config config =
new Config.Builder()
.text(Files.readString(text))
new Config.Builder(text)
.postfix("minute to read")
.plural("minutes to read")
.build();

View file

@ -3,8 +3,8 @@
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>LongMethod:ReadingTimeTest.kt$ReadingTimeTest$@Test fun testReadingTimeInSec()</ID>
<ID>LongParameterList:Config.kt$Config$( val text: String, val wpm: Int, val postfix: String, val plural: String, val excludeImages: Boolean, val extra: Int, val roundingMode: RoundingMode )</ID>
<ID>LongParameterList:ReadingTime.kt$ReadingTime$( text: String, wpm: Int = 275, var postfix: String = "min read", var plural: String = "min read", excludeImages: Boolean = false, extra: Int = 0, var roundingMode: RoundingMode = RoundingMode.HALF_EVEN )</ID>
<ID>MagicNumber:Config.kt$Config.Builder$275</ID>
<ID>MagicNumber:ReadingTime.kt$ReadingTime$10</ID>
<ID>MagicNumber:ReadingTime.kt$ReadingTime$12</ID>
<ID>MagicNumber:ReadingTime.kt$ReadingTime$3</ID>

View file

@ -13,6 +13,13 @@
<option value="$PROJECT_DIR$/../../../../java/bld-checkstyle/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-exec/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-testng/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-generated-version/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-checkstyle/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-generated-version/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-jacoco-report/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-testng/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-exec/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-pitest/config/pmd.xml" />
</list>
</option>
<option name="skipTestSources" value="false" />

View file

@ -16,8 +16,7 @@ public class ReadingTimeSample {
rt.setPlural("minutes to read");
// final Config config =
// new Config.Builder()
// .text(Files.readString(text))
// new Config.Builder(Files.readString(text))
// .postfix("minute to read")
// .plural("minutes to read")
// .build();

View file

@ -5,6 +5,23 @@
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="PDMPlugin">
<option name="customRuleSets">
<list>
<option value="K:\java\semver\config\pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-pitest/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-jacoco-report/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-checkstyle/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-exec/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-testng/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/bld-generated-version/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-checkstyle/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-generated-version/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-jacoco-report/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-testng/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-exec/config/pmd.xml" />
<option value="$PROJECT_DIR$/../../../../java/rife2/bld-pitest/config/pmd.xml" />
</list>
</option>
<option name="skipTestSources" value="false" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="17" project-jdk-type="JavaSDK" />

View file

@ -16,8 +16,7 @@ public class ReadingTimeSample {
rt.setPlural("minutes to read");
// final Config config =
// new Config.Builder()
// .text(Files.readString(text))
// new Config.Builder(Files.readString(text))
// .postfix("minute to read")
// .plural("minutes to read")
// .build();

View file

@ -1,8 +1,8 @@
bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true
bld.extension-detekt=com.uwyn.rife2:bld-detekt:0.9.2
bld.extension-jacoco=com.uwyn.rife2:bld-jacoco-report:0.9.5
bld.extensions=com.uwyn.rife2:bld-kotlin:0.9.4
bld.extension-detekt=com.uwyn.rife2:bld-detekt:0.9.4
bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.downloadLocation=
bld.sourceDirectories=

View file

@ -35,35 +35,76 @@ import java.math.RoundingMode
/**
* Provides a configuration builder.
*/
class Config private constructor(
val text: String,
val wpm: Int,
val postfix: String,
val plural: String,
val excludeImages: Boolean,
val extra: Int,
class Config private constructor(builder: Builder) {
val text: String
val wpm: Int
val postfix: String
val plural: String
val excludeImages: Boolean
val extra: Int
val roundingMode: RoundingMode
) {
init {
text = builder.text
wpm = builder.wpm
postfix = builder.postfix
plural = builder.plural
excludeImages = builder.excludeImages
extra = builder.extra
roundingMode = builder.roundingMode
}
/**
* Configures the parameters.
*
* @param text The text to be evaluated.
*/
data class Builder(var text: String) {
var wpm: Int = 275
var postfix: String = "min read"
var plural: String = "min read"
var excludeImages: Boolean = false
var extra: Int = 0
var roundingMode: RoundingMode = RoundingMode.HALF_EVEN
/**
* The text to be evaluated.
*/
data class Builder(
private var text: String = "",
private var wpm: Int = 275,
private var postfix: String = "min read",
private var plural: String = "min read",
private var excludeImages: Boolean = false,
private var extra: Int = 0,
private var roundingMode: RoundingMode = RoundingMode.HALF_EVEN
) {
fun text(text: String) = apply { this.text = text }
/**
* The words per minute reading average.
*/
fun wpm(wpm: Int) = apply { this.wpm = wpm }
/**
* The value to be appended to the reading time.
*/
fun postfix(postfix: String) = apply { this.postfix = postfix }
/**
* The value to be appended if the reading time is more than 1 minute.
*/
fun plural(plural: String) = apply { this.plural = plural }
/**
* Images are excluded from the reading time when set.
*/
fun excludeImages(excludeImages: Boolean) = apply { this.excludeImages = excludeImages }
/**
* Additional seconds to be added to the total reading time.
*/
fun extra(extra: Int) = apply { this.extra = extra }
/**
* The [RoundingMode] to apply. Default is [RoundingMode.HALF_DOWN].
*/
fun roundingMode(roundingMode: RoundingMode) = apply { this.roundingMode = roundingMode }
fun build() = Config(text, wpm, postfix, plural, excludeImages, extra, roundingMode)
/**
* Builds the configuration.
*/
fun build() = Config(this)
}
}

View file

@ -188,7 +188,7 @@ class ReadingTimeTest {
@Test
fun testReadingTimeConfig() {
var config = Config.Builder().text(blogPost)
var config = Config.Builder(blogPost)
assertEquals("2 min read", ReadingTime(config.build()).calcReadingTime(),
"calcReadingTime(blogPost)")