oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/java/canonical.txt
1.2 KiB, 1 run
created by r1870400018:11986, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | import java.util.HashMap; |
| 2 | import java.util.Map; |
| 3 | import java.util.Optional; |
| 4 | |
| 5 | public class Registry<T> { |
| 6 | |
| 7 | private final Map<String, T> entries = new HashMap<>(); |
| 8 | private final String name; |
| 9 | |
| 10 | public Registry(String name) { |
| 11 | this.name = name; |
| 12 | } |
| 13 | |
| 14 | public void register(String key, T value) { |
| 15 | if (entries.containsKey(key)) { |
| 16 | throw new IllegalArgumentException("duplicate key: " + key); |
| 17 | } |
| 18 | entries.put(key, value); |
| 19 | } |
| 20 | |
| 21 | public Optional<T> lookup(String key) { |
| 22 | return Optional.ofNullable(entries.get(key)); |
| 23 | } |
| 24 | |
| 25 | public boolean contains(String key) { |
| 26 | return entries.containsKey(key); |
| 27 | } |
| 28 | |
| 29 | public int size() { |
| 30 | return entries.size(); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public String toString() { |
| 35 | return String.format("Registry[%s, %d entries]", name, entries.size()); |
| 36 | } |
| 37 | |
| 38 | public static void main(String[] args) { |
| 39 | Registry<String> reg = new Registry<>("colours"); |
| 40 | reg.register("red", "#ff0000"); |
| 41 | reg.register("green", "#00ff00"); |
| 42 | reg.register("blue", "#0000ff"); |
| 43 | |
| 44 | System.out.println(reg); |
| 45 | |
| 46 | reg.lookup("red").ifPresent(v -> |
| 47 | System.out.println("red = " + v) |
| 48 | ); |
| 49 | } |
| 50 | } |