Skip to content

Commit

Permalink
simple enhancement for text stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Nov 2, 2024
1 parent d490f38 commit 0326cd4
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
/**
* Contains number related types such as {@link io.github.mmm.base.number.NumberType} or
* {@link io.github.mmm.base.number.LongParser}.
*/
package io.github.mmm.base.number;
122 changes: 122 additions & 0 deletions core/src/main/java/io/github/mmm/base/score/Score.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.base.score;

/**
* Data-type for a score as a double value in the range from {@code 0} to {@code 1}.
*
* @since 1.0.0
*/
public final class Score {

private final double value;

/** The minimum score (0.0). */
public static final Score MIN = new Score(0);

/** The very low score (0.1). */
public static final Score VERY_LOW = new Score(0.1);

/** The lower score (0.2). */
public static final Score LOWER = new Score(0.2);

/** The low score (0.3). */
public static final Score LOW = new Score(0.3);

/** The low medium score (0.4). */
public static final Score LOW_MEDIUM = new Score(0.4);

/** The medium score (0.5). */
public static final Score MEDIUM = new Score(0.5);

/** The high medium score (0.6). */
public static final Score HIGH_MEDIUM = new Score(0.6);

/** The high score (0.7). */
public static final Score HIGH = new Score(0.7);

/** The higher score (0.8). */
public static final Score HIGHER = new Score(0.8);

/** The very high score (0.9). */
public static final Score VERY_HIGH = new Score(0.9);

/** The maximum score (1.0). */
public static final Score MAX = new Score(1);

private static final Score[] CONST = new Score[] { MIN, VERY_LOW, LOWER, LOW, LOW_MEDIUM, MEDIUM, HIGH_MEDIUM, HIGH,
HIGHER, VERY_HIGH, MAX };

private Score(double value) {

super();
this.value = value;
}

/**
* @return the score as double value in the range from {@code 0} to {@code 1}.
*/
public double getValue() {

return this.value;
}

/**
* @param score the factor to multiply by.
* @return the product of this {@link Score} multiplied with the given {@link Score}.
*/
public Score multiply(Score score) {

if ((score == MIN) || (this == MIN)) {
return MIN;
} else if (score == MAX) {
return this;
} else if (this == MAX) {
return score;
}
double newValue = this.value * score.value;
return of(newValue);
}

@Override
public int hashCode() {

return (int) this.value * 100;
}

@Override
public boolean equals(Object obj) {

if (obj == this) {
return true;
} else if (obj instanceof Score score) {
return this.value == score.value;
} else {
return false;
}
}

@Override
public String toString() {

return Double.toString(this.value);
}

/**
* @param value the {@link #getValue() score value}.
* @return the {@link Score} for the given {@code value}.
*/
public static final Score of(double value) {

if ((value < 0) || (value > 1)) {
throw new IllegalArgumentException("Invalid score: " + value);
}
for (Score constant : CONST) {
if (constant.value == value) {
return constant;
}
}
return new Score(value);
}

}
6 changes: 6 additions & 0 deletions core/src/main/java/io/github/mmm/base/score/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
/**
* Contains {@link io.github.mmm.base.score.Score}.
*/
package io.github.mmm.base.score;
48 changes: 45 additions & 3 deletions core/src/main/java/io/github/mmm/base/text/CaseConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,49 @@
public enum CaseConversion {

/** Convert {@link String#toLowerCase() to lower case}. */
LOWER_CASE,
LOWER_CASE {
@Override
public void append(StringBuilder sb, String text) {

int len = text.length();
for (int i = 0; i < len; i++) {
int c = text.codePointAt(i);
if ((c >= 'A') && (c <= 'Z')) {
c += 32; // A = 65, a = 97 (65+32)
} else if ((c < 0) || (c > 256)) {
c = Character.toLowerCase(c);
}
sb.appendCodePoint(c);
}
}
},

/** Convert {@link String#toUpperCase() to upper case}. */
UPPER_CASE,
UPPER_CASE {
@Override
public void append(StringBuilder sb, String text) {

int len = text.length();
for (int i = 0; i < len; i++) {
int c = text.codePointAt(i);
if ((c >= 'a') && (c <= 'z')) {
c -= 32; // a = 97, A = 65 (97-32)
} else if ((c < 0) || (c > 256)) {
c = Character.toUpperCase(c);
}
sb.appendCodePoint(c);
}
}
},

/** Do not convert (keep original case). */
ORIGINAL_CASE;
ORIGINAL_CASE {
@Override
public void append(StringBuilder sb, String text) {

sb.append(text);
}
};

/**
* The character representing {@link #ORIGINAL_CASE} in {@link #ofExample(char, boolean) examples}. Was chosen so it
Expand Down Expand Up @@ -74,6 +110,12 @@ public char convert(char c) {
}
}

/**
* @param sb the {@link StringBuilder} to {@link StringBuilder#append(String) append} to.
* @param text the text to append.
*/
public abstract void append(StringBuilder sb, String text);

/**
* @param c the character to {@link #convert(char) convert}.
* @return the {@link #convert(char) converted} character {@code c} but {@link #EXAMPLE_CHAR_FOR_ORIGINAL_CASE} if
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

exports io.github.mmm.base.range;

exports io.github.mmm.base.score;

exports io.github.mmm.base.service;

exports io.github.mmm.base.sort;
Expand Down
63 changes: 63 additions & 0 deletions core/src/test/java/io/github/mmm/base/score/ScoreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.base.score;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test of {@link Score}.
*/
public class ScoreTest extends Assertions {

private static final Score[] SCORES = new Score[] { Score.MIN, Score.VERY_LOW, Score.LOWER, Score.LOW,
Score.LOW_MEDIUM, Score.MEDIUM, Score.HIGH_MEDIUM, Score.HIGH, Score.HIGHER, Score.VERY_HIGH, Score.MAX };

/** Test {@link Score#of(double)}. */
@Test
public void testOf() {

assertThat(Score.of(0.0)).isSameAs(Score.MIN);
assertThat(Score.of(0.1)).isSameAs(Score.VERY_LOW);
assertThat(Score.of(0.2)).isSameAs(Score.LOWER);
assertThat(Score.of(0.3)).isSameAs(Score.LOW);
assertThat(Score.of(0.4)).isSameAs(Score.LOW_MEDIUM);
assertThat(Score.of(0.5)).isSameAs(Score.MEDIUM);
assertThat(Score.of(0.6)).isSameAs(Score.HIGH_MEDIUM);
assertThat(Score.of(0.7)).isSameAs(Score.HIGH);
assertThat(Score.of(0.8)).isSameAs(Score.HIGHER);
assertThat(Score.of(0.9)).isSameAs(Score.VERY_HIGH);
assertThat(Score.of(1.0)).isSameAs(Score.MAX);
assertThat(Score.of(0.25)).isNotSameAs(Score.of(0.25)).isEqualTo(Score.of(0.25));
}

/** Test {@link Score#getValue()}. */
@Test
public void testGetValue() {

assertThat(Score.MIN.getValue()).isEqualTo(0.0);
assertThat(Score.VERY_LOW.getValue()).isEqualTo(0.1);
assertThat(Score.LOWER.getValue()).isEqualTo(0.2);
assertThat(Score.LOW.getValue()).isEqualTo(0.3);
assertThat(Score.LOW_MEDIUM.getValue()).isEqualTo(0.4);
assertThat(Score.MEDIUM.getValue()).isEqualTo(0.5);
assertThat(Score.HIGH_MEDIUM.getValue()).isEqualTo(0.6);
assertThat(Score.HIGH.getValue()).isEqualTo(0.7);
assertThat(Score.HIGHER.getValue()).isEqualTo(0.8);
assertThat(Score.VERY_HIGH.getValue()).isEqualTo(0.9);
assertThat(Score.MAX.getValue()).isEqualTo(1.0);
assertThat(Score.of(0.25).getValue()).isEqualTo(0.25);
}

/** Test {@link Score#multiply(Score)}. */
@Test
public void testMultiply() {

for (Score score : SCORES) {
assertThat(Score.MIN.multiply(score)).isSameAs(Score.MIN);
assertThat(Score.MAX.multiply(score)).isSameAs(score);
}
assertThat(Score.MEDIUM.multiply(Score.MEDIUM)).isEqualTo(Score.of(0.25));
}

}

0 comments on commit 0326cd4

Please sign in to comment.