-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
core/src/main/java/io/github/mmm/base/number/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
core/src/main/java/io/github/mmm/base/score/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
core/src/test/java/io/github/mmm/base/score/ScoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |