Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit 57aeb77

Browse files
Merge pull request #115 from sauravbhattacharya001/refactor/centralize-edge-type-registry
refactor: centralize edge type metadata into EdgeTypeRegistry
2 parents a5c5c9e + 3f0394d commit 57aeb77

3 files changed

Lines changed: 126 additions & 30 deletions

File tree

Gvisual/src/gvisual/DotExporter.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,7 @@ public class DotExporter {
4949
private String rankDir = null; // TB, LR, BT, RL
5050
private final Map<String, String> graphAttributes = new LinkedHashMap<>();
5151

52-
// Default color palette for Edge types
53-
private static final Map<String, String> DEFAULT_TYPE_COLORS = new LinkedHashMap<>();
54-
static {
55-
DEFAULT_TYPE_COLORS.put("f", "#4CAF50"); // friend = green
56-
DEFAULT_TYPE_COLORS.put("fs", "#2196F3"); // friend-stranger = blue
57-
DEFAULT_TYPE_COLORS.put("c", "#FF9800"); // classmate = orange
58-
DEFAULT_TYPE_COLORS.put("s", "#F44336"); // stranger = red
59-
DEFAULT_TYPE_COLORS.put("sg", "#9C27B0"); // study group = purple
60-
}
61-
62-
private final Map<String, String> typeColors = new LinkedHashMap<>(DEFAULT_TYPE_COLORS);
52+
private final Map<String, String> typeColors = new LinkedHashMap<>(EdgeTypeRegistry.getAllHexColors());
6353

6454
/**
6555
* Creates a new DOT exporter for the given graph.
@@ -295,17 +285,10 @@ public String exportToString() {
295285

296286
/**
297287
* Returns a human-readable name for an Edge type code.
288+
* Delegates to the centralized {@link EdgeTypeRegistry}.
298289
*/
299290
private String getTypeName(String type) {
300-
if (type == null) return "unknown";
301-
switch (type) {
302-
case "f": return "Friend";
303-
case "fs": return "Friend-Stranger";
304-
case "c": return "Classmate";
305-
case "s": return "Stranger";
306-
case "sg": return "Study Group";
307-
default: return type;
308-
}
291+
return EdgeTypeRegistry.getName(type);
309292
}
310293

311294
/**
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

Gvisual/src/gvisual/GexfExporter.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ public class GexfExporter {
4949
private String description = "";
5050
private boolean includeVizData = true;
5151

52-
// Edge-type → RGB colour mapping (matches the app's colour scheme)
53-
private static final Map<String, int[]> TYPE_COLORS = new LinkedHashMap<>();
54-
static {
55-
TYPE_COLORS.put("f", new int[]{0, 200, 0}); // friend — green
56-
TYPE_COLORS.put("fs", new int[]{255, 165, 0}); // family/sibling — orange
57-
TYPE_COLORS.put("c", new int[]{0, 150, 255}); // classmate — blue
58-
TYPE_COLORS.put("s", new int[]{180, 180, 180}); // stranger — gray
59-
TYPE_COLORS.put("sg", new int[]{255, 80, 80}); // study group — red
60-
}
52+
// Edge-type → RGB colour mapping — delegates to EdgeTypeRegistry for consistency
53+
// across all exporters. Local reference kept for backward compatibility of
54+
// the getEdgeColor() method.
6155

6256
/**
6357
* Creates a new GEXF exporter for the given graph.
@@ -207,7 +201,7 @@ public String exportToString() {
207201

208202
// Viz colour by Edge type
209203
if (includeVizData && type.length() > 0) {
210-
int[] rgb = TYPE_COLORS.getOrDefault(type, new int[]{128, 128, 128});
204+
int[] rgb = EdgeTypeRegistry.getRgbColor(type);
211205
sb.append(" <viz:color r=\"").append(rgb[0])
212206
.append("\" g=\"").append(rgb[1])
213207
.append("\" b=\"").append(rgb[2])

0 commit comments

Comments
 (0)