1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package Torello.JavaDoc.SyntaxHiLite;

import Torello.CSS.ClassNameCSSException;

import Torello.HTML.HTMLTags;
import Torello.HTML.TagNode;
import Torello.HTML.SD;
import Torello.HTML.TC;

import Torello.Java.Additional.Ret2;

import java.util.Objects;
import java.util.Arrays;

public class AbstractConfig<T extends AbstractConfig<T>>
    implements java.io.Serializable
{    
    protected static final long serialVersionUID = 1;


    // ********************************************************************************************
    // ********************************************************************************************
    // Class-Config - Fields (These a)
    // ********************************************************************************************
    // ********************************************************************************************


    protected final String[] classNames;

    protected final boolean[] spansOrBolds, utilize;

    protected boolean   upperOrLower_TagName        = true;
    protected boolean   upperOrLower_ClassAttrName  = true;
    protected SD        quotes                      = null;

    private final int CONFIG_ARR_LEN;

    private final Class<T> captureTheClassDummy;

    protected AbstractConfig(
            final int       CONFIG_ARR_LEN,
            final boolean[] spansOrBolds,
            final Class<T>  captureTheClassDummy
        )
    {
        Objects.requireNonNull
            (spansOrBolds, "The array passed to parameter 'spansOrBolds' is null");

        Objects.requireNonNull
            (captureTheClassDummy, "The class passed to parameter 'captureTheClassDummy' is null");

        if (CONFIG_ARR_LEN < 1) throw new IllegalArgumentException(
            '[' + CONFIG_ARR_LEN + "] was passed to CONFIG_ARR_LEN, but this value must be a " +
            "positive integer that is greater than 0."
        );

        if (spansOrBolds.length != CONFIG_ARR_LEN) throw new IllegalArgumentException(
            "The length of the Array-Parameter 'spansOrBolds' is [" + spansOrBolds.length + "], " +
            "but the value passed to parameter CONFIG_ARR_LENGTH was [" + CONFIG_ARR_LEN + "].  " +
            "These values must be equals."
        );

        this.CONFIG_ARR_LEN         = CONFIG_ARR_LEN;
        this.captureTheClassDummy   = captureTheClassDummy;

        this.classNames     = new String[CONFIG_ARR_LEN];
        this.spansOrBolds   = new boolean[CONFIG_ARR_LEN];
        this.utilize        = new boolean[CONFIG_ARR_LEN];

        for (int i=1; i < CONFIG_ARR_LEN; i++) this.classNames[i]   = "HL-" + i;
        for (int i=0; i < CONFIG_ARR_LEN; i++) this.spansOrBolds[i] = spansOrBolds[i];
        for (int i=0; i < CONFIG_ARR_LEN; i++) this.utilize[i]      = true;
    }

    protected AbstractConfig(AbstractConfig<T> c)
    {
        final int L = this.CONFIG_ARR_LEN = c.CONFIG_ARR_LEN;

        this.captureTheClassDummy = c.captureTheClassDummy;

        this.classNames      = new String[c.CONFIG_ARR_LEN];
        this.spansOrBolds    = new boolean[c.CONFIG_ARR_LEN];
        this.utilize         = new boolean[c.CONFIG_ARR_LEN];

        System.arraycopy(c.classNames,   0, this.classNames,    0, L);
        System.arraycopy(c.spansOrBolds, 0, this.spansOrBolds,  0, L);
        System.arraycopy(c.utilize,      0, this.utilize,       0, L);

        this.upperOrLower_TagName       = c.upperOrLower_TagName;
        this.upperOrLower_ClassAttrName = c.upperOrLower_ClassAttrName;
        this.quotes                     = c.quotes;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Class-Config - "Setters" and "Getters" (This is SYNCHRONIZED, That's Why)
    // ********************************************************************************************
    // ********************************************************************************************


    public synchronized AbstractConfig<T> setClassNameCSS(byte whichOne, String className)
    {
        checkWhichOne(whichOne);
        ClassNameCSSException.check(className);
        this.classNames[whichOne] = className;
        return this;
    }

    public synchronized String getClassName(byte whichOne)
    { checkWhichOne(whichOne);  return this.classNames[whichOne]; }

    public synchronized AbstractConfig<T> setSpanOrBold(byte whichOne, boolean spanOrBold)
    {
        checkWhichOne(whichOne);
        this.spansOrBolds[whichOne] = spanOrBold;
        return this;
    }

    public synchronized boolean getSpansOrBolds(byte whichOne)
    { checkWhichOne(whichOne);  return this.spansOrBolds[whichOne]; }

    public synchronized AbstractConfig<T> setUtilizeOrForget(byte whichOne, boolean useOrForget)
    {
        checkWhichOne(whichOne);
        this.utilize[whichOne] = useOrForget;
        return this;
    }

    public synchronized boolean getUtilized(byte whichOne)
    { checkWhichOne(whichOne);  return this.utilize[whichOne]; }

    private void checkWhichOne(byte whichOne)
    {
        if ((whichOne < 1) || (whichOne >= this.CONFIG_ARR_LEN))

            throw new IllegalArgumentException(
                "You have passed an invalid value [" + whichOne + "], to parameter 'whichOne'.  " +
                "This must be a valid byte, whose value is between 1 and " +
                (this.CONFIG_ARR_LEN - 1)
            );
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // This method serves as a "Helper" to the method that creates a HiLiter in sub-classes
    // ********************************************************************************************
    // ********************************************************************************************


    protected Ret2<TagNode[], TagNode[]> generateHiLiterHelper() 
    {
        final String    cAttr       = this.upperOrLower_ClassAttrName ? "CLASS" : "class";
        final boolean   useQuotes   = (this.quotes != null);
        final String    q           = useQuotes ? ("" + this.quotes.quote) : "";

        final TagNode CLOSE_SPAN = this.upperOrLower_TagName
            ? HTMLTags.hasTag("SPAN", TC.ClosingTags)
            : HTMLTags.hasTag("span", TC.ClosingTags);

        final TagNode CLOSE_BOLD = this.upperOrLower_TagName
            ? HTMLTags.hasTag("B", TC.ClosingTags)
            : HTMLTags.hasTag("b", TC.ClosingTags);

        final TagNode[] openers = new TagNode[this.CONFIG_ARR_LEN];
        final TagNode[] closers = new TagNode[this.CONFIG_ARR_LEN];

        for (int i=1; i < this.CONFIG_ARR_LEN; i++)
        {
            openers[i] = this.utilize[i]
                ? TAG(cAttr, q, this.classNames[i], this.spansOrBolds[i])
                : null;

            closers[i] = this.utilize[i]
                ? (spansOrBolds[i] ? CLOSE_SPAN : CLOSE_BOLD)
                : null;
        }

        return new Ret2<>(openers, closers);
    }

    private TagNode TAG(String cAttr, String q, String className, boolean spanOrBold)
    {
        final String tag;

        if (upperOrLower_TagName)   tag = spanOrBold ? "SPAN" : "B";
        else                        tag = spanOrBold ? "span" : "b";

        return new TagNode('<' + tag + ' ' + cAttr + '=' + q + className + q + '>');
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Class-Config - java.lang.Object (JLO) & java.lang.Cloneable
    // ********************************************************************************************
    // ********************************************************************************************


    public boolean equals(Object other)
    {
        if (! (other instanceof Torello.JavaDoc.SyntaxHiLite.AbstractConfig))
            return false;

        @SuppressWarnings("rawtypes")
        Torello.JavaDoc.SyntaxHiLite.AbstractConfig o =
            (Torello.JavaDoc.SyntaxHiLite.AbstractConfig) other;

        return
                (this.CONFIG_ARR_LEN == o.CONFIG_ARR_LEN)
            &&  Objects.equals(this.captureTheClassDummy, o.captureTheClassDummy)

            &&  Arrays.equals(this.classNames,      o.classNames)
            &&  Arrays.equals(this.spansOrBolds,    o.spansOrBolds)
            &&  Arrays.equals(this.utilize,         o.utilize)

            // Tag-Case / Upper-Case or Lower-Case
            &&  (this.upperOrLower_TagName          == o.upperOrLower_TagName)
            &&  (this.upperOrLower_ClassAttrName    == o.upperOrLower_ClassAttrName)
            &&  Objects.equals(this.quotes, o.quotes);
    }

    public int hashCode()
    {
        int ret = 0;

        for (int i=0; i < this.CONFIG_ARR_LEN; i += 3)
            ret += this.classNames[i].hashCode();

        for (int i=0; i < this.CONFIG_ARR_LEN; i += 3)
            ret += (this.spansOrBolds[i] ? 1 : 0) * 1;

        return ret;
    }
}