33import java .lang .reflect .Array ;
44import java .util .*;
55
6- public abstract class EditorStatusDisplay <B , T > {
6+ /**
7+ * The helper class to handle editor status
8+ *
9+ * @param <B> the builder
10+ * @param <S> the section
11+ * @param <T> the object that is built from the builder
12+ */
13+ public abstract class EditorStatusDisplay <B , S , T > {
14+ /**
15+ * Create a new builder
16+ *
17+ * @return the builder
18+ */
719 protected abstract B newBuilder ();
820
9- protected abstract void appendPadding (B builder , int level );
10-
11- protected abstract void appendKey (B builder , String key , boolean fromCollection );
12-
13- protected abstract void appendSize (B builder , int size );
14-
15- protected abstract void appendValue (B builder , Object value , int level , Editor <?, ?> editor );
16-
21+ /**
22+ * Create a new section
23+ *
24+ * @param builder the builder
25+ * @return the section
26+ */
27+ protected abstract S newSection (B builder );
28+
29+ /**
30+ * Add padding to the section
31+ *
32+ * @param section the section
33+ * @param level the padding level
34+ */
35+ protected abstract void appendPadding (S section , int level );
36+
37+ /**
38+ * Add key to the section
39+ *
40+ * @param section the section
41+ * @param key the key
42+ * @param fromCollection whether the key is from a collection
43+ */
44+ protected abstract void appendKey (S section , String key , boolean fromCollection );
45+
46+ /**
47+ * Add size to the section
48+ *
49+ * @param section the section
50+ * @param size the size
51+ */
52+ protected abstract void appendSize (S section , int size );
53+
54+ /**
55+ * Add value to the section
56+ *
57+ * @param section the section
58+ * @param value the value
59+ * @param editor the current editor
60+ */
61+ protected abstract void appendValue (S section , Object value , Editor <?> editor );
62+
63+ /**
64+ * Add section to the builder
65+ *
66+ * @param section the section
67+ * @param builder the builder
68+ * @return the new builder
69+ */
70+ protected abstract B addToBuilder (S section , B builder );
71+
72+ /**
73+ * Build the final object from the builder
74+ *
75+ * @param builder the builder
76+ * @return the object
77+ */
1778 protected abstract T build (B builder );
1879
19- protected Object preprocessValue (Object value , Editor <?, ?> editor ) {
80+ /**
81+ * Preprocess the editor value
82+ *
83+ * @param value the value
84+ * @param editor the editor
85+ * @return the preprocessed object
86+ */
87+ protected Object preprocessValue (Object value , Editor <?> editor ) {
88+ while (value instanceof Editor <?>) {
89+ Editor <?> subEditor = (Editor <?>) value ;
90+ value = subEditor .status ();
91+ }
2092 return value ;
2193 }
2294
23- public List <T > display (Editor <?, ?> editor ) {
24- List <T > list = new ArrayList <>();
95+ /**
96+ * Display the editor by converting the editor status to an object
97+ *
98+ * @param editor the editor
99+ * @return the status object
100+ */
101+ public T display (Editor <?> editor ) {
102+ B builder = newBuilder ();
25103 Deque <Entry > deque = new ArrayDeque <>();
26104 deque .addFirst (new Entry (0 , "ROOT" , editor , false ));
27105 while (!deque .isEmpty ()) {
@@ -34,32 +112,28 @@ public List<T> display(Editor<?, ?> editor) {
34112 continue ;
35113 }
36114
37- while (value instanceof Editor <?, ?>) {
38- Editor <?, ?> subEditor = (Editor <?, ?>) value ;
39- value = subEditor .status ();
40- }
115+ S section = newSection (builder );
41116
42- B builder = newBuilder ();
43- appendPadding (builder , level );
44- appendKey (builder , entry .key (), entry .fromCollection ());
117+ appendPadding (section , level );
118+ appendKey (section , entry .key (), entry .fromCollection ());
45119 if (value instanceof Collection <?>) {
46120 Collection <?> collection = (Collection <?>) value ;
47- appendSize (builder , collection .size ());
121+ appendSize (section , collection .size ());
48122 List <Object > copyList = new ArrayList <>(collection );
49123 for (int i = copyList .size () - 1 ; i >= 0 ; i --) {
50124 Object o = copyList .get (i );
51125 deque .addFirst (new Entry (level + 1 , Integer .toString (i ), o , true ));
52126 }
53127 } else if (value .getClass ().isArray ()) {
54128 int length = Array .getLength (value );
55- appendSize (builder , length );
129+ appendSize (section , length );
56130 for (int i = length - 1 ; i >= 0 ; i --) {
57131 Object o = Array .get (value , i );
58132 deque .addFirst (new Entry (level + 1 , Integer .toString (i ), o , true ));
59133 }
60134 } else if (value instanceof Map <?, ?>) {
61135 Map <?, ?> subMap = (Map <?, ?>) value ;
62- appendSize (builder , subMap .size ());
136+ appendSize (section , subMap .size ());
63137 List <Map .Entry <?, ?>> copyList = new ArrayList <>(subMap .entrySet ());
64138 for (int i = subMap .size () - 1 ; i >= 0 ; i --) {
65139 Map .Entry <?, ?> o = copyList .get (i );
@@ -69,11 +143,11 @@ public List<T> display(Editor<?, ?> editor) {
69143 Map .Entry <?, ?> subEntry = (Map .Entry <?, ?>) value ;
70144 deque .addFirst (new Entry (level + 1 , Objects .toString (subEntry .getKey ()), subEntry .getValue (), false ));
71145 } else {
72- appendValue (builder , value , level , editor );
146+ appendValue (section , value , editor );
73147 }
74- list . add ( build ( builder ) );
148+ builder = addToBuilder ( section , builder );
75149 }
76- return list ;
150+ return build ( builder ) ;
77151 }
78152
79153 private static final class Entry {
@@ -120,14 +194,5 @@ public boolean equals(Object obj) {
120194 public int hashCode () {
121195 return Objects .hash (level , key , value , fromCollection );
122196 }
123-
124- @ Override
125- public String toString () {
126- return "Entry[" +
127- "level=" + level + ", " +
128- "key=" + key + ", " +
129- "value=" + value + ", " +
130- "fromCollection=" + fromCollection + ']' ;
131- }
132197 }
133198}
0 commit comments