Skip to content

Commit 630c142

Browse files
author
Camilo Molina Orth
committed
feat: add numeric converter
1 parent a368994 commit 630c142

18 files changed

+1135
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can pull it from the central Maven repositories:
2121
<dependency>
2222
<groupId>cl.bennu</groupId>
2323
<artifactId>commons</artifactId>
24-
<version>1.4.0</version>
24+
<version>1.5.0</version>
2525
</dependency>
2626
```
2727

@@ -42,13 +42,13 @@ Dependencies
4242
------------
4343

4444
- lombok 1.18.38
45-
- jackson-core 2.18.3
46-
- jackson-annotations 2.18.3
45+
- jackson-core 2.19.0
46+
- jackson-annotations 2.19.0
4747
- commons-lang3 3.17.0
48-
- commons-io 2.18.0
48+
- commons-io 2.19.0
4949
- java-jwt 4.5.0
5050
- commons-beanutils 1.10.1
51-
- commons-collections4 4.5.0-M3
51+
- commons-collections4 4.5.0
5252
- slf4j-api 2.0.17
5353

5454
Test Dependencies

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>cl.bennu</groupId>
99
<artifactId>commons</artifactId>
10-
<version>1.4.0</version>
10+
<version>1.5.0</version>
1111
<name>bennu-commons</name>
1212
<description>Utilitarios bennu</description>
1313
<url>https://github.com/bennu/commons</url>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cl.bennu.commons.enums;
2+
3+
import cl.bennu.commons.number.*;
4+
import cl.bennu.commons.number.iface.SpecificNumberSystem;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonFormat;
7+
import lombok.Getter;
8+
9+
import java.util.Arrays;
10+
import java.util.Map;
11+
12+
@Getter
13+
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
14+
public enum NumberSystemTypeEnum implements cl.bennu.commons.enums.base.BaseEnum {
15+
16+
//@formatter:off
17+
POSITIONAL(1, "Positional")
18+
, ADDITIVE(2, "Additive")
19+
, MULTIPLICATIVE(3, "Multiplicative")
20+
, HYBRID(4, "Hybrid")
21+
, UNARY(5, "Unary")
22+
, ALPHABETICAL(6, "Alphabetical")
23+
, LOGOGRAPHIC(7, "Logographic")
24+
, NOT_POSITIONAL(8, "Not positional")
25+
, CUNEIFORM(9, "Cuneiform")
26+
, NUMERICAL_SYLLABIC(10, "Numerical syllabic")
27+
;
28+
//@formatter:on
29+
30+
NumberSystemTypeEnum(Integer id, String name) {
31+
this.id = id;
32+
this.name = name;
33+
}
34+
35+
private final Integer id;
36+
private final String name;
37+
38+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
39+
public static NumberSystemTypeEnum valueOf(Object o) {
40+
if (o instanceof Integer id) {
41+
return Arrays.stream(values()).filter(e -> e.getId().equals(id)).findFirst().orElse(null);
42+
} else if (o instanceof Map map) {
43+
Integer id = (Integer) map.get("id");
44+
return Arrays.stream(values()).filter(e -> e.getId().equals(id)).findFirst().orElse(null);
45+
} else {
46+
return null;
47+
}
48+
}
49+
50+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package cl.bennu.commons.enums;
2+
3+
import cl.bennu.commons.number.*;
4+
import cl.bennu.commons.number.iface.SpecificNumberSystem;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonFormat;
7+
import lombok.Getter;
8+
9+
import java.util.Arrays;
10+
import java.util.Map;
11+
12+
@Getter
13+
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
14+
public enum NumericBaseEnum implements cl.bennu.commons.enums.base.BaseEnum {
15+
16+
//@formatter:off
17+
BINARY(1, 2, "Binary", NumberSystemTypeEnum.POSITIONAL)
18+
, OCTAL(2, 8, "Octal", NumberSystemTypeEnum.POSITIONAL)
19+
, DECIMAL(3, 10, "Decimal", NumberSystemTypeEnum.POSITIONAL)
20+
, HEXADECIMAL(4, 16, "Hexadecimal", NumberSystemTypeEnum.POSITIONAL)
21+
22+
, TERNARY(5, 3, "Ternary", NumberSystemTypeEnum.POSITIONAL)
23+
, QUATERNARY(6, 4, "Quaternary", NumberSystemTypeEnum.POSITIONAL)
24+
, QUINARY(7, 5, "Quinary", NumberSystemTypeEnum.POSITIONAL)
25+
, SENARY(8, 6, "Senary", NumberSystemTypeEnum.POSITIONAL)
26+
, SEPTENARY(9, 7, "Septenary", NumberSystemTypeEnum.POSITIONAL)
27+
, NONARY(10, 9, "Nonary", NumberSystemTypeEnum.POSITIONAL)
28+
, UNDECIMAL(11, 11, "Undecimal", NumberSystemTypeEnum.POSITIONAL)
29+
, DUODECIMAL(12, 12, "Duodecimal", NumberSystemTypeEnum.POSITIONAL)
30+
, VIGESIMAL(13, 20, "Vigesimal", NumberSystemTypeEnum.POSITIONAL)
31+
32+
, ROMAN(14, new RomanNumeralSystem(), "Roman", NumberSystemTypeEnum.ADDITIVE)
33+
, ATTIC(15, new AtticNumeralSystem(), "Attic", NumberSystemTypeEnum.ADDITIVE)
34+
, EGYPTIAN(16, new EgyptianNumeralSystem(), "Egyptian", NumberSystemTypeEnum.ADDITIVE)
35+
, MAYAN(17, new MayanNumeralSystem(), "Mayan", NumberSystemTypeEnum.NOT_POSITIONAL)
36+
, BABYLONIAN(18, new BabylonianNumeralSystem(), "Babylonian", NumberSystemTypeEnum.ADDITIVE)
37+
, HEBREW(19, new HebrewNumeralSystem(), "Hebrew", NumberSystemTypeEnum.ALPHABETICAL)
38+
, GREEKIONIAN(20, new GreekIonianNumeralSystem(), "Greek Ionian", NumberSystemTypeEnum.ALPHABETICAL)
39+
, CYRILLIC(21, new CyrillicNumeralSystem(), "Cyrillic", NumberSystemTypeEnum.NOT_POSITIONAL)
40+
, AEGEAN(22, new AegeanNumeralSystem(), "Aegean", NumberSystemTypeEnum.NOT_POSITIONAL)
41+
42+
, BASE_13(23, 13, "Base 13", NumberSystemTypeEnum.POSITIONAL)
43+
, BASE_14(24, 14, "Base 14", NumberSystemTypeEnum.POSITIONAL)
44+
, BASE_15(25, 15, "Base 15", NumberSystemTypeEnum.POSITIONAL)
45+
, BASE_17(26, 17, "Base 17", NumberSystemTypeEnum.POSITIONAL)
46+
, BASE_18(27, 18, "Base 18", NumberSystemTypeEnum.POSITIONAL)
47+
, BASE_19(28, 19, "Base 19", NumberSystemTypeEnum.POSITIONAL)
48+
, BASE_21(29, 21, "Base 21", NumberSystemTypeEnum.POSITIONAL)
49+
, BASE_22(30, 22, "Base 22", NumberSystemTypeEnum.POSITIONAL)
50+
, BASE_23(31, 23, "Base 23", NumberSystemTypeEnum.POSITIONAL)
51+
, BASE_24(32, 24, "Base 24", NumberSystemTypeEnum.POSITIONAL)
52+
, BASE_25(33, 25, "Base 25", NumberSystemTypeEnum.POSITIONAL)
53+
, BASE_26(34, 26, "Base 26", NumberSystemTypeEnum.POSITIONAL)
54+
, BASE_27(35, 27, "Base 27", NumberSystemTypeEnum.POSITIONAL)
55+
, BASE_28(36, 28, "Base 28", NumberSystemTypeEnum.POSITIONAL)
56+
, BASE_29(37, 29, "Base 29", NumberSystemTypeEnum.POSITIONAL)
57+
, BASE_30(38, 30, "Base 30", NumberSystemTypeEnum.POSITIONAL)
58+
, BASE_31(39, 31, "Base 31", NumberSystemTypeEnum.POSITIONAL)
59+
, BASE_32(40, 32, "Base 32", NumberSystemTypeEnum.POSITIONAL)
60+
, BASE_33(41, 33, "Base 33", NumberSystemTypeEnum.POSITIONAL)
61+
, BASE_34(42, 34, "Base 34", NumberSystemTypeEnum.POSITIONAL)
62+
, BASE_35(43, 35, "Base 35", NumberSystemTypeEnum.POSITIONAL)
63+
, BASE_36(44, 36, "Base 36", NumberSystemTypeEnum.POSITIONAL)
64+
65+
, BRAILLE(45, new BrailleNumeralSystem(), "Braille", NumberSystemTypeEnum.NOT_POSITIONAL)
66+
, MORSE(46, new MorseNumeralSystem(), "Morse", NumberSystemTypeEnum.NOT_POSITIONAL)
67+
;
68+
//@formatter:on
69+
70+
NumericBaseEnum(Integer id, Integer base, String name, NumberSystemTypeEnum numberSystemTypeEnum) {
71+
this.id = id;
72+
this.base = base;
73+
this.name = name;
74+
this.specificNumberSystem = null;
75+
this.numberSystemTypeEnum = numberSystemTypeEnum;
76+
}
77+
78+
NumericBaseEnum(Integer id, SpecificNumberSystem specificNumberSystem, String name, NumberSystemTypeEnum numberSystemTypeEnum) {
79+
this.id = id;
80+
this.base = null;
81+
this.name = name;
82+
this.specificNumberSystem = specificNumberSystem;
83+
this.numberSystemTypeEnum = numberSystemTypeEnum;
84+
}
85+
86+
private final Integer id;
87+
private final Integer base;
88+
private final String name;
89+
private final SpecificNumberSystem specificNumberSystem;
90+
private final NumberSystemTypeEnum numberSystemTypeEnum;
91+
92+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
93+
public static NumericBaseEnum valueOf(Object o) {
94+
if (o instanceof Integer id) {
95+
return Arrays.stream(values()).filter(e -> e.getId().equals(id)).findFirst().orElse(null);
96+
} else if (o instanceof Map map) {
97+
Integer id = (Integer) map.get("id");
98+
return Arrays.stream(values()).filter(e -> e.getId().equals(id)).findFirst().orElse(null);
99+
} else {
100+
return null;
101+
}
102+
}
103+
104+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cl.bennu.commons.number;
2+
3+
import cl.bennu.commons.number.iface.SpecificNumberSystem;
4+
5+
import java.util.LinkedHashMap;
6+
import java.util.Map;
7+
8+
public class AegeanNumeralSystem implements SpecificNumberSystem {
9+
10+
private static final LinkedHashMap<String, Long> SYMBOLS = new LinkedHashMap<>();
11+
12+
static {
13+
SYMBOLS.put("*", 10_000L); // 𐄫
14+
SYMBOLS.put("@", 1_000L); // 𐄢
15+
SYMBOLS.put("9", 100L); // 𐄙
16+
SYMBOLS.put("n", 10L); // 𐄐
17+
SYMBOLS.put("|", 1L); // 𐄇
18+
}
19+
20+
@Override
21+
public String convert(Long number) {
22+
if (number == null) throw new IllegalArgumentException("Number cannot be null");
23+
24+
return convert(number.longValue());
25+
}
26+
27+
@Override
28+
public String convert(long number) {
29+
if (number <= 0) throw new IllegalArgumentException("Number must be greater than 0");
30+
31+
StringBuilder sb = new StringBuilder();
32+
for (Map.Entry<String, Long> entry : SYMBOLS.entrySet()) {
33+
while (number >= entry.getValue()) {
34+
sb.append(entry.getKey());
35+
number -= entry.getValue();
36+
}
37+
}
38+
return sb.toString();
39+
}
40+
41+
@Override
42+
public String convert(Integer number) {
43+
if (number == null) throw new IllegalArgumentException("Number cannot be null");
44+
45+
return convert(number.longValue());
46+
}
47+
48+
@Override
49+
public String convert(int number) {
50+
return convert((long) number);
51+
}
52+
53+
@Override
54+
public long convert(String value) {
55+
long total = 0;
56+
for (char c : value.toCharArray()) {
57+
String symbol = String.valueOf(c);
58+
Long num = SYMBOLS.get(symbol);
59+
if (num == null) throw new IllegalArgumentException("Invalid symbol: " + symbol);
60+
total += num;
61+
}
62+
return total;
63+
}
64+
65+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cl.bennu.commons.number;
2+
3+
import cl.bennu.commons.number.iface.SpecificNumberSystem;
4+
5+
import java.util.LinkedHashMap;
6+
import java.util.Map;
7+
8+
public class AtticNumeralSystem implements SpecificNumberSystem {
9+
10+
private static final LinkedHashMap<String, Long> SYMBOLS = new LinkedHashMap<>();
11+
12+
static {
13+
SYMBOLS.put("ΜΔ", 1_000_000L);
14+
SYMBOLS.put("ΜΥ", 100_000L);
15+
SYMBOLS.put("Μ", 10_000L);
16+
SYMBOLS.put("Χ", 1_000L);
17+
SYMBOLS.put("Η", 100L);
18+
SYMBOLS.put("Δ", 10L);
19+
SYMBOLS.put("Π", 5L);
20+
SYMBOLS.put("Ι", 1L);
21+
}
22+
23+
@Override
24+
public String convert(Long number) {
25+
if (number == null) throw new IllegalArgumentException("Number cannot be null");
26+
27+
return convert(number.longValue());
28+
}
29+
30+
@Override
31+
public String convert(long number) {
32+
if (number <= 0) throw new IllegalArgumentException("Number must be greater than 0");
33+
34+
StringBuilder result = new StringBuilder();
35+
for (Map.Entry<String, Long> entry : SYMBOLS.entrySet()) {
36+
while (number >= entry.getValue()) {
37+
result.append(entry.getKey());
38+
number -= entry.getValue();
39+
}
40+
}
41+
return result.toString();
42+
}
43+
44+
@Override
45+
public String convert(Integer number) {
46+
if (number == null) throw new IllegalArgumentException("Number cannot be null");
47+
48+
return convert(number.longValue());
49+
}
50+
51+
@Override
52+
public String convert(int number) {
53+
return convert((long) number);
54+
}
55+
56+
@Override
57+
public long convert(String value) {
58+
long result = 0;
59+
int i = 0;
60+
value = value.toUpperCase();
61+
while (i < value.length()) {
62+
boolean matched = false;
63+
for (String symbol : SYMBOLS.keySet()) {
64+
if (value.startsWith(symbol, i)) {
65+
result += SYMBOLS.get(symbol);
66+
i += symbol.length();
67+
matched = true;
68+
break;
69+
}
70+
}
71+
if (!matched) throw new IllegalArgumentException("Invalid symbol: " + value);
72+
}
73+
74+
return result;
75+
}
76+
77+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cl.bennu.commons.number;
2+
3+
import cl.bennu.commons.number.iface.SpecificNumberSystem;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class BabylonianNumeralSystem implements SpecificNumberSystem {
9+
10+
@Override
11+
public String convert(Long number) {
12+
if (number == null) throw new IllegalArgumentException("Number cannot be null");
13+
14+
return convert(number.longValue());
15+
}
16+
17+
@Override
18+
public String convert(long number) {
19+
if (number < 0) throw new IllegalArgumentException("Number must be greater than 0");
20+
21+
if (number == 0) return "0";
22+
23+
List<String> parts = new ArrayList<>();
24+
while (number > 0) {
25+
long digit = number % 60;
26+
StringBuilder symbol = new StringBuilder();
27+
symbol.append("^".repeat((int) (digit / 10)));
28+
symbol.append("|".repeat((int) (digit % 10)));
29+
parts.add(0, symbol.toString());
30+
number /= 60;
31+
}
32+
return String.join(" ", parts);
33+
}
34+
35+
@Override
36+
public String convert(Integer number) {
37+
if (number == null) throw new IllegalArgumentException("Number cannot be null");
38+
39+
return convert(number.longValue());
40+
}
41+
42+
@Override
43+
public String convert(int number) {
44+
return convert((long) number);
45+
}
46+
47+
@Override
48+
public long convert(String value) {
49+
if (value.equals("0")) return 0;
50+
51+
String[] parts = value.split(" ");
52+
long result = 0;
53+
for (String group : parts) {
54+
long num = group.chars().filter(c -> c == '^').count() * 10L +
55+
group.chars().filter(c -> c == '|').count();
56+
result = result * 60 + num;
57+
}
58+
return result;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)