|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Martin Paljak <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package pro.javacard.prefs; |
| 23 | + |
| 24 | +import java.util.*; |
| 25 | + |
| 26 | +public final class Preferences { |
| 27 | + private final Map<Preference<?>, Object> values; |
| 28 | + // Registry allows to slurp in known preferences from a file. |
| 29 | + private static final Map<String, Preference<?>> REGISTRY = new HashMap<>(); |
| 30 | + |
| 31 | + public Preferences() { |
| 32 | + this.values = Map.of(); |
| 33 | + } |
| 34 | + |
| 35 | + private Preferences(Map<Preference<?>, Object> values) { |
| 36 | + // .copyOf() assures that there are no null values |
| 37 | + this.values = Map.copyOf(values); |
| 38 | + } |
| 39 | + |
| 40 | + // Only allows non-null overrides for preferences that have default values |
| 41 | + public <V> Preferences with(Preference<V> key, V value) { |
| 42 | + if (value == null) { |
| 43 | + throw new IllegalArgumentException("Cannot set null outcome for preference '" + key.name() + "'"); |
| 44 | + } |
| 45 | + if (!key.validator().test(value)) { |
| 46 | + throw new IllegalArgumentException("Value for preference '" + key.name() + "' fails validation: " + value); |
| 47 | + } |
| 48 | + Map<Preference<?>, Object> newValues = new HashMap<>(); |
| 49 | + newValues.put(key, value); |
| 50 | + // Calling merge will take care of readonly preferences |
| 51 | + return merge(new Preferences(newValues)); |
| 52 | + } |
| 53 | + |
| 54 | + public <V> Preferences without(Preference<V> key) { |
| 55 | + if (key.readonly()) { |
| 56 | + throw new IllegalArgumentException("Can't remove readonly preference!"); |
| 57 | + } |
| 58 | + if (!values.containsKey(key)) { |
| 59 | + return this; |
| 60 | + } |
| 61 | + Map<Preference<?>, Object> newValues = new HashMap<>(values); |
| 62 | + newValues.remove(key); |
| 63 | + return new Preferences(newValues); |
| 64 | + } |
| 65 | + |
| 66 | + // Always returns non-null: either the explicit override or the default outcome |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + public <V> V get(Preference.Default<V> key) { |
| 69 | + V value = (V) values.get(key); |
| 70 | + return value != null ? value : key.defaultValue(); |
| 71 | + } |
| 72 | + |
| 73 | + // Returns empty Optional when using default, present Optional when explicitly overridden |
| 74 | + @SuppressWarnings("unchecked") |
| 75 | + public <V> Optional<V> valueOf(Preference<V> key) { |
| 76 | + // We don't allow null values, so the optional is empty only |
| 77 | + // if the preference is not explicitly established |
| 78 | + return Optional.ofNullable((V) values.get(key)); |
| 79 | + } |
| 80 | + |
| 81 | + // Add all keys from other to this |
| 82 | + public Preferences merge(Preferences other) { |
| 83 | + Map<Preference<?>, Object> newValues = new HashMap<>(this.values); |
| 84 | + for (Map.Entry<Preference<?>, Object> entry : other.values.entrySet()) { |
| 85 | + Preference<?> key = entry.getKey(); |
| 86 | + if (key.readonly() && this.values.containsKey(key)) { |
| 87 | + // Skip readonly preferences that already exist in this instance |
| 88 | + //log.warn("Trying to overwrite read-only preference " + key); |
| 89 | + continue; |
| 90 | + } |
| 91 | + newValues.put(key, entry.getValue()); |
| 92 | + } |
| 93 | + return new Preferences(newValues); |
| 94 | + } |
| 95 | + |
| 96 | + public Set<Preference<?>> keys() { |
| 97 | + return values.keySet(); |
| 98 | + } |
| 99 | + |
| 100 | + public boolean isEmpty() { |
| 101 | + return values.isEmpty(); |
| 102 | + } |
| 103 | + |
| 104 | + public int size() { |
| 105 | + return values.size(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String toString() { |
| 110 | + StringBuilder sb = new StringBuilder(); |
| 111 | + sb.append("Preferences{"); |
| 112 | + for (var k : values.entrySet()) { |
| 113 | + sb.append(k.getKey().name()); |
| 114 | + sb.append("("); |
| 115 | + sb.append(k.getKey().type().getTypeName()); |
| 116 | + sb.append(")"); |
| 117 | + sb.append("="); |
| 118 | + if (k.getValue() instanceof byte[] bytes) { |
| 119 | + sb.append(HexFormat.of().formatHex(bytes)); |
| 120 | + } else { |
| 121 | + sb.append(k.getValue()); |
| 122 | + } |
| 123 | + sb.append(";"); |
| 124 | + } |
| 125 | + sb.append("}"); |
| 126 | + return sb.toString(); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + // For parameters (no default outcome) |
| 131 | + public static <T> Preference.Parameter<T> register(String name, Class<T> type, boolean readonly) { |
| 132 | + Preference.Parameter<T> preference = Preference.parameter(name, type, readonly); |
| 133 | + REGISTRY.put(preference.name(), preference); |
| 134 | + return preference; |
| 135 | + } |
| 136 | + |
| 137 | + // For parameters (readonly by default) |
| 138 | + public static <T> Preference.Parameter<T> register(String name, Class<T> type) { |
| 139 | + return register(name, type, true); |
| 140 | + } |
| 141 | + |
| 142 | + // For preferences with default values |
| 143 | + public static <T> Preference.Default<T> register(String name, Class<T> type, T defaultValue, boolean readonly) { |
| 144 | + Preference.Default<T> preference = Preference.of(name, type, defaultValue, readonly); |
| 145 | + REGISTRY.put(preference.name(), preference); |
| 146 | + return preference; |
| 147 | + } |
| 148 | + |
| 149 | + // For preferences with default values (not readonly by default) |
| 150 | + public static <T> Preference.Default<T> register(String name, Class<T> type, T defaultValue) { |
| 151 | + return register(name, type, defaultValue, false); |
| 152 | + } |
| 153 | +} |
0 commit comments