summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/math/CardinalState.java
blob: d8990ddd7c2c5275878153bd464216a47a80c26d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package bjc.utils.math;

import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.function.LongPredicate;

/*
 * @TODO 2/12/18 Ben Culkin :AdditionalCardinals
 * 
 * Add some built-in implementations for various things.
 */
/**
 * Customizations for number cardinalization.
 * 
 * @author EVE
 *
 */
public class CardinalState {
	/**
	 * Alias type for converting numbers to cardinals.
	 * 
	 * @author EVE
	 *
	 */
	@FunctionalInterface
	public interface Cardinalizer extends BiFunction<Long, CardinalState, String> {
		/*
		 * Alias
		 */
	}

	/**
	 * Custom cardinals for numbers.
	 */
	public final Map<Long, String> customNumbers;

	/**
	 * Custom functions to apply to certain scales.
	 */
	public final Map<LongPredicate, Cardinalizer> customScales;

	/**
	 * Create a new set of cardinalization customizations.
	 * 
	 * @param customNumbers
	 *        The custom numbers to use.
	 * @param customScales
	 *        The custom scales to use.
	 */
	public CardinalState(Map<Long, String> customNumbers, Map<LongPredicate, Cardinalizer> customScales) {
		this.customNumbers = customNumbers;
		this.customScales = customScales;
	}

	/**
	 * Handle a custom cardinal number
	 * 
	 * @param number
	 *        The number to handle
	 * @return The number as a cardinal, or null if we don't handle it.
	 */
	public String handleCustom(long number) {
		if(customNumbers.containsKey(number)) {
			return customNumbers.get(number);
		}

		for(Entry<LongPredicate, Cardinalizer> ent : customScales.entrySet()) {
			if(ent.getKey().test(number)) {
				return ent.getValue().apply(number, this);
			}
		}

		return null;
	}
}