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

Commit c92e223

Browse files
refactor: extract EdgeType enum to eliminate edge-type if/else chains
Introduces EdgeType.java — a Java enum that centralises the five relationship categories (Friend, Classmate, Familiar Stranger, Stranger, Study Group) with their: - File code strings ('f', 'fs', 'c', 's', 'sg') - Display labels - Colours - Default duration/meeting thresholds Refactored in Main.java: - Edge classification in addGraph(): replaced 5-branch if/else with EdgeType.fromCode() lookup + getEdgeList() helper - Edge paint transformer: replaced 5-branch color if/else with EdgeType.colorForCode() single call - Vertex paint transformer: replaced 5 counter variables + 7-branch if/else with Set<EdgeType> + iterator().next().getColor() - getDominantLabel(): replaced 5-branch if/else with EdgeType lookup - Stats panel & category panel label colours: replaced FRIEND_COLOR etc. constants with EdgeType.FRIEND.getColor() etc. - JSlider default values: replaced scattered threshold constants with EdgeType.getDefaultDurationThreshold()/getDefaultMeetingThreshold() Removed from Main.java: - 5 colour constants (FRIEND_COLOR, FS_COLOR, etc.) - 10 threshold constants (FRIEND_DUR_THRESHOLD, etc.) - 5 fLabelled/fsLabelled/etc. boolean tracking fields - Multiple cascading if/else chains (replaced by enum lookups) New helpers in Main.java: - getEdgeList(EdgeType): maps enum to the corresponding edge list - isEdgeTypeVisible(String): maps type code to checkbox state Net effect: -113 lines from Main.java, all edge-type knowledge centralised in one enum. Adding a new relationship type now requires adding one enum constant instead of editing 8+ places.
1 parent 66f9110 commit c92e223

2 files changed

Lines changed: 174 additions & 113 deletions

File tree

Gvisual/src/gvisual/EdgeType.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package gvisual;
2+
3+
import java.awt.Color;
4+
5+
/**
6+
* Enumeration of edge relationship categories in the graph.
7+
*
8+
* <p>Centralises the code string, display label, colour, and default
9+
* threshold values that were previously scattered across Main.java as
10+
* parallel if/else chains and separate constant fields.</p>
11+
*
12+
* <p>Usage:</p>
13+
* <pre>
14+
* EdgeType type = EdgeType.fromCode("f"); // FRIEND
15+
* Color c = type.getColor(); // Color.GREEN
16+
* String label = type.getDisplayLabel(); // "friend"
17+
* </pre>
18+
*/
19+
public enum EdgeType {
20+
21+
FRIEND ("f", "friend", Color.GREEN, 10, 2),
22+
CLASSMATE ("c", "Classmate", Color.BLUE, 30, 1),
23+
FAMILIAR ("fs", "Familiar Stranger", Color.GRAY, 2, 1),
24+
STRANGER ("s", "Stranger", Color.RED, 2, 2),
25+
STUDY_GROUP ("sg", "Study Groups", Color.ORANGE, 20, 1);
26+
27+
private final String code;
28+
private final String displayLabel;
29+
private final Color color;
30+
private final int defaultDurationThreshold;
31+
private final int defaultMeetingThreshold;
32+
33+
EdgeType(String code, String displayLabel, Color color,
34+
int defaultDurationThreshold, int defaultMeetingThreshold) {
35+
this.code = code;
36+
this.displayLabel = displayLabel;
37+
this.color = color;
38+
this.defaultDurationThreshold = defaultDurationThreshold;
39+
this.defaultMeetingThreshold = defaultMeetingThreshold;
40+
}
41+
42+
/** Returns the short code used in data files (e.g. "f", "fs", "c"). */
43+
public String getCode() {
44+
return code;
45+
}
46+
47+
/** Returns the human-readable label for graph legends. */
48+
public String getDisplayLabel() {
49+
return displayLabel;
50+
}
51+
52+
/** Returns the default edge colour for rendering. */
53+
public Color getColor() {
54+
return color;
55+
}
56+
57+
/** Returns the default duration-of-meeting threshold (minutes). */
58+
public int getDefaultDurationThreshold() {
59+
return defaultDurationThreshold;
60+
}
61+
62+
/** Returns the default number-of-meetings threshold. */
63+
public int getDefaultMeetingThreshold() {
64+
return defaultMeetingThreshold;
65+
}
66+
67+
/**
68+
* Looks up an {@code EdgeType} by its file code string.
69+
*
70+
* @param code the edge type code ("f", "fs", "c", "s", "sg")
71+
* @return the matching {@code EdgeType}, or {@code null} if unknown
72+
*/
73+
public static EdgeType fromCode(String code) {
74+
if (code == null) {
75+
return null;
76+
}
77+
String lower = code.toLowerCase();
78+
for (EdgeType t : values()) {
79+
if (t.code.equals(lower)) {
80+
return t;
81+
}
82+
}
83+
return null;
84+
}
85+
86+
/**
87+
* Returns the colour for a given edge type code.
88+
* Falls back to {@link Color#RED} (stranger) for unknown codes.
89+
*
90+
* @param typeCode the edge type code
91+
* @return the associated colour
92+
*/
93+
public static Color colorForCode(String typeCode) {
94+
EdgeType t = fromCode(typeCode);
95+
return t != null ? t.color : Color.RED;
96+
}
97+
}

0 commit comments

Comments
 (0)