|
| 1 | +package gvisual; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Centralized registry of edge type metadata — names, hex colors, and |
| 7 | + * RGB tuples. |
| 8 | + * |
| 9 | + * <p>Before this class existed, every exporter (DOT, GEXF, JSON, HTML, |
| 10 | + * Timeline, CSV) maintained its own copy of the type→name and type→color |
| 11 | + * mappings. Adding a new edge type required updating 5+ files. Now there |
| 12 | + * is a single source of truth.</p> |
| 13 | + * |
| 14 | + * <p>Consumers should call the static methods rather than hard-coding |
| 15 | + * edge type strings.</p> |
| 16 | + * |
| 17 | + * @author zalenix |
| 18 | + */ |
| 19 | +public final class EdgeTypeRegistry { |
| 20 | + |
| 21 | + private EdgeTypeRegistry() { /* utility class */ } |
| 22 | + |
| 23 | + // ── Canonical edge type codes ─────────────────────────────────── |
| 24 | + public static final String FRIEND = "f"; |
| 25 | + public static final String FAMILIAR_STRANGER = "fs"; |
| 26 | + public static final String CLASSMATE = "c"; |
| 27 | + public static final String STRANGER = "s"; |
| 28 | + public static final String STUDY_GROUP = "sg"; |
| 29 | + |
| 30 | + // ── Display names ─────────────────────────────────────────────── |
| 31 | + private static final Map<String, String> NAMES = new LinkedHashMap<>(); |
| 32 | + static { |
| 33 | + NAMES.put(FRIEND, "Friend"); |
| 34 | + NAMES.put(FAMILIAR_STRANGER, "Friend-Stranger"); |
| 35 | + NAMES.put(CLASSMATE, "Classmate"); |
| 36 | + NAMES.put(STRANGER, "Stranger"); |
| 37 | + NAMES.put(STUDY_GROUP, "Study Group"); |
| 38 | + } |
| 39 | + |
| 40 | + // ── Hex colors (for DOT, HTML, SVG, etc.) ─────────────────────── |
| 41 | + private static final Map<String, String> HEX_COLORS = new LinkedHashMap<>(); |
| 42 | + static { |
| 43 | + HEX_COLORS.put(FRIEND, "#4CAF50"); // green |
| 44 | + HEX_COLORS.put(FAMILIAR_STRANGER, "#2196F3"); // blue |
| 45 | + HEX_COLORS.put(CLASSMATE, "#FF9800"); // orange |
| 46 | + HEX_COLORS.put(STRANGER, "#F44336"); // red |
| 47 | + HEX_COLORS.put(STUDY_GROUP, "#9C27B0"); // purple |
| 48 | + } |
| 49 | + |
| 50 | + // ── RGB colors (for GEXF and formats needing integer components) ─ |
| 51 | + private static final Map<String, int[]> RGB_COLORS = new LinkedHashMap<>(); |
| 52 | + static { |
| 53 | + RGB_COLORS.put(FRIEND, new int[]{0, 200, 0}); |
| 54 | + RGB_COLORS.put(FAMILIAR_STRANGER, new int[]{200, 200, 0}); |
| 55 | + RGB_COLORS.put(CLASSMATE, new int[]{0, 150, 255}); |
| 56 | + RGB_COLORS.put(STRANGER, new int[]{180, 180, 180}); |
| 57 | + RGB_COLORS.put(STUDY_GROUP, new int[]{255, 80, 80}); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the human-readable name for an edge type code. |
| 62 | + * |
| 63 | + * @param typeCode the edge type code (e.g. "f", "sg") |
| 64 | + * @return display name, or the raw code if unknown |
| 65 | + */ |
| 66 | + public static String getName(String typeCode) { |
| 67 | + if (typeCode == null) return "Unknown"; |
| 68 | + return NAMES.getOrDefault(typeCode, typeCode); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the hex color for an edge type code. |
| 73 | + * |
| 74 | + * @param typeCode the edge type code |
| 75 | + * @return hex color string (e.g. "#4CAF50"), or "#CCCCCC" if unknown |
| 76 | + */ |
| 77 | + public static String getHexColor(String typeCode) { |
| 78 | + return HEX_COLORS.getOrDefault(typeCode, "#CCCCCC"); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the RGB color components for an edge type code. |
| 83 | + * |
| 84 | + * @param typeCode the edge type code |
| 85 | + * @return int array [r, g, b], or {128,128,128} if unknown |
| 86 | + */ |
| 87 | + public static int[] getRgbColor(String typeCode) { |
| 88 | + int[] rgb = RGB_COLORS.get(typeCode); |
| 89 | + return (rgb != null) ? rgb : new int[]{128, 128, 128}; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns an unmodifiable copy of all known hex colors. |
| 94 | + * Useful for exporters that need the full palette. |
| 95 | + * |
| 96 | + * @return map of type code → hex color |
| 97 | + */ |
| 98 | + public static Map<String, String> getAllHexColors() { |
| 99 | + return Collections.unmodifiableMap(new LinkedHashMap<>(HEX_COLORS)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns an unmodifiable copy of all known RGB colors. |
| 104 | + * |
| 105 | + * @return map of type code → int[3] |
| 106 | + */ |
| 107 | + public static Map<String, int[]> getAllRgbColors() { |
| 108 | + return Collections.unmodifiableMap(new LinkedHashMap<>(RGB_COLORS)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns all known type codes in canonical order. |
| 113 | + * |
| 114 | + * @return list of type codes |
| 115 | + */ |
| 116 | + public static List<String> getAllTypeCodes() { |
| 117 | + return Collections.unmodifiableList(new ArrayList<>(NAMES.keySet())); |
| 118 | + } |
| 119 | +} |
0 commit comments