Skip to content

Commit 8505b28

Browse files
bactgoneall
authored andcommitted
Add Javadoc string for ParseInstruction
Signed-off-by: Arthit Suriyawongkul <[email protected]>
1 parent adb0951 commit 8505b28

File tree

1 file changed

+140
-25
lines changed

1 file changed

+140
-25
lines changed

src/main/java/org/spdx/utility/compare/CompareTemplateOutputHandler.java

Lines changed: 140 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,36 @@ class ParseInstruction {
5454
private boolean skip = false; // skip this instruction in matching
5555
private boolean skipFirstTextToken = false; // skip the first text token
5656
private DifferenceDescription lastOptionalDifference = null;
57-
57+
58+
/**
59+
* Construct a new {@link ParseInstruction} with the specified rule, text, and parent
60+
*
61+
* A parse instruction represents a single unit of parsing logic, which may include a rule,
62+
* associated text, and a hierarchical relationship to a parent instruction.
63+
*
64+
* @param rule The {@link LicenseTemplateRule} associated with this parse instruction. Can
65+
* be {@code null} if no rule is associated.
66+
* @param text The text content of this parse instruction. Can be {@code null} if no text is
67+
* associated.
68+
* @param parent The parent {@link ParseInstruction} of this parse instruction. Can be
69+
* {@code null} if this parse instruction has no parent.
70+
*/
5871
ParseInstruction(LicenseTemplateRule rule, String text, ParseInstruction parent) {
5972
this.rule = rule;
6073
this.text = text;
6174
this.subInstructions = new ArrayList<>();
6275
this.parent = parent;
6376
}
64-
77+
78+
/**
79+
* Return a string representation of this parse instruction
80+
*
81+
* If the parse instruction has an associated rule, the rule's string representation is returned.
82+
* If the parse instruction has associated text, the first 10 characters of the text are returned.
83+
* If neither a rule nor text is associated, "NONE" is returned.
84+
*
85+
* @return A string representation of this parse instruction.
86+
*/
6587
@Override
6688
public String toString() {
6789
if (this.rule != null) {
@@ -80,35 +102,60 @@ public String toString() {
80102
}
81103

82104
/**
83-
* @return the rule
105+
* Retrieve the license template rule associated with this parse instruction
106+
*
107+
* The rule defines the type of instruction (e.g., variable, optional, etc.) and provides
108+
* details about how the instruction should be processed during the comparison.
109+
*
110+
* @return The {@link LicenseTemplateRule} associated with this parse instruction, or {@code null}
111+
* if no rule is associated.
84112
*/
85113
public LicenseTemplateRule getRule() {
86114
return rule;
87115
}
88116

89117
/**
90-
* @param rule the rule to set
118+
* Set the license template rule associated with this parse instruction
119+
*
120+
* The rule defines the type of instruction (e.g., variable, optional, etc.) and provides
121+
* details about how the instruction should be processed during the comparison.
122+
*
123+
* @param rule The {@link LicenseTemplateRule} to associate with this parse instruction. Can
124+
* be {@code null} if no rule is associated.
91125
*/
92126
public void setRule(LicenseTemplateRule rule) {
93127
this.rule = rule;
94128
}
95129

96130
/**
97-
* @return the text
131+
* Retrieve the text associated with this parse instruction
132+
*
133+
* The text represents the content of this parse instruction, which may be compared against
134+
* other text during the license template matching process.
135+
*
136+
* @return The text associated with this parse instruction, or {@code null} if no text is
137+
* set.
98138
*/
99139
public String getText() {
100140
return text;
101141
}
102142

103143
/**
104-
* @param text the text to set
144+
* Set the text content for this parse instruction
145+
*
146+
* The text represents the content of this parse instruction, which may be compared against
147+
* other text during the license template matching process.
148+
*
149+
* @param text The text to associate with this parse instruction. Can be {@code null} if no
150+
* text is to be associated.
105151
*/
106152
public void setText(String text) {
107153
this.text = text;
108154
}
109155

110156
/**
111157
* Add the instruction to the list of sub-instructions
158+
*
112159
* @param instruction instruction to add
113160
*/
114161
public void addSubInstruction(ParseInstruction instruction) {
@@ -130,44 +177,67 @@ public void addSubInstruction(ParseInstruction instruction) {
130177
}
131178

132179
/**
133-
* @return the parent
180+
* Retrieve the parent parse instruction of this parse instruction
181+
*
182+
* @return The parent {@link ParseInstruction}, or {@code null} if this parse instruction
183+
* has no parent.
134184
*/
135185
public ParseInstruction getParent() {
136186
return parent;
137187
}
138188

139189
/**
140-
* @param parent the parent to set
190+
* Set the parent parse instruction for this parse instruction
191+
*
192+
* @param parent The {@link ParseInstruction} to set as the parent of this parse
193+
* instruction. Can be {@code null} if this parse instruction has no parent.
141194
*/
142195
public void setParent(ParseInstruction parent) {
143196
this.parent = parent;
144197
}
145198

146199
/**
147-
* @return the subInstructions
200+
* Retrieve the list of sub-instructions for this parse instruction.
201+
*
202+
* Sub-instructions represent the child instructions that are part of this parse
203+
* instruction. These can include text, rules, or other nested instructions.
204+
*
205+
* @return A {@link List} of {@link ParseInstruction} objects representing the
206+
* sub-instructions of this parse instruction. If there are no sub-instructions, an
207+
* empty list is returned.
148208
*/
149209
public List<ParseInstruction> getSubInstructions() {
150210
return subInstructions;
151211
}
152212

153213
/**
154-
* @return true iff there are only text instructions as sub instructions
214+
* Check whether all sub-instructions of this parse instruction contain only text
215+
*
216+
* @return {@code true} if all sub-instructions contain only text, {@code false} otherwise.
217+
* Also returns {@code false} if there are no sub-instructions.
155218
*/
156219
public boolean onlyText() {
157220
if (this.subInstructions.isEmpty()) {
158221
return false;
159222
}
160-
for (ParseInstruction subInstr:this.subInstructions) {
223+
for (ParseInstruction subInstr : this.subInstructions) {
161224
if (subInstr.getText() == null) {
162225
return false;
163226
}
164227
}
165228
return true;
166229
}
167230

231+
/**
232+
* Convert all sub-instructions of this parse instruction into a single concatenated text
233+
* string
234+
*
235+
* @return A concatenated string containing the text of all sub-instructions, or an empty
236+
* string if no sub-instructions contain text.
237+
*/
168238
public String toText() {
169239
StringBuilder sb = new StringBuilder();
170-
for (ParseInstruction subInstr:this.subInstructions) {
240+
for (ParseInstruction subInstr : this.subInstructions) {
171241
if (subInstr.getText() != null) {
172242
sb.append(subInstr.getText());
173243
}
@@ -177,6 +247,7 @@ public String toText() {
177247

178248
/**
179249
* Attempt to match this instruction against a tokenized array
250+
*
180251
* @param matchTokens Tokens to match the instruction against
181252
* @param startToken Index of the tokens to start the match
182253
* @param endToken Last index of the tokens to use in the match
@@ -193,12 +264,14 @@ public int match(String[] matchTokens, int startToken, int endToken, String orig
193264

194265
/**
195266
* Attempt to match this instruction against a tokenized array
267+
*
196268
* @param matchTokens Tokens to match the instruction against
197269
* @param startToken Index of the tokens to start the match
198270
* @param endToken Last index of the tokens to use in the match
199271
* @param originalText Original text used go generate the matchTokens
200272
* @param differences Description of differences found
201-
* @param tokenToLocation Map of the location of tokens * @param ignoreOptionalDifferences if true, don't record any optional differences
273+
* @param tokenToLocation Map of the location of tokens
274+
* @param ignoreOptionalDifferences if true, don't record any optional differences
202275
* @return Next token index after the match or -1 if no match was found
203276
* @throws LicenseParserException On license parsing errors
204277
*/
@@ -271,12 +344,13 @@ public int match(String[] matchTokens, int startToken, int endToken, String orig
271344

272345
/**
273346
* Match to an optional rule
347+
*
274348
* @param matchingStartTokens List of indexes for the start tokens for the next normal text
275349
* @param matchTokens Tokens to match against
276350
* @param startToken Index of the first token to search for the match
277351
* @param originalText Original text used go generate the matchTokens
278352
* @param tokenToLocation Map of token index to line/column where the token was found in the original text
279-
* @param ignoreOptionalDifferences if true, don't record any optional differences
353+
* @param ignoreOptionalDifferences if true, don't record any optional differences
280354
* @return the index of the token after the find or -1 if the text did not match
281355
* @throws LicenseParserException On license parsing errors
282356
*/
@@ -305,6 +379,7 @@ private int matchOptional(List<Integer> matchingStartTokens,
305379

306380
/**
307381
* Find the indexes that match the matching optional or first normal text within the sub-instructions
382+
*
308383
* @param afterChild the child after which to start searching for the first normal text
309384
* @param matchTokens Tokens used to match the text against
310385
* @param startToken Start of the match tokens to begin the search
@@ -427,6 +502,7 @@ private List<Integer> findNextNonVarTextStartTokens(ParseInstruction afterChild,
427502

428503
/**
429504
* Determine the number of tokens matched from the compare text
505+
*
430506
* @param text text to search
431507
* @param end End of matching text
432508
* @return number of tokens in the text
@@ -446,6 +522,7 @@ private int numTokensMatched(String text, int end) {
446522

447523
/**
448524
* Match to a variable rule
525+
*
449526
* @param matchingStartTokens List of indexes for the start tokens for the next normal text
450527
* @param matchTokens Tokens to match against
451528
* @param startToken Index of the first token to search for the match
@@ -476,7 +553,10 @@ private int matchVariable(List<Integer> matchingStartTokens, String[] matchToken
476553
}
477554

478555
/**
479-
* @return The difference description for the last optional rule which did not match
556+
* Retrieve the difference description for the last optional rule that did not match
557+
*
558+
* @return A {@link DifferenceDescription} object representing the last optional difference,
559+
* or {@code null} if no optional difference was found.
480560
*/
481561
public DifferenceDescription getLastOptionalDifference() {
482562
if (this.lastOptionalDifference != null) {
@@ -487,9 +567,17 @@ public DifferenceDescription getLastOptionalDifference() {
487567
return null;
488568
}
489569
}
490-
570+
571+
/**
572+
* Set the last optional difference that did not match.
573+
*
574+
* @param optionalDifference A {@link DifferenceDescription} object representing the last
575+
* optional difference. This must not be {@code null}, and it must have a non-empty
576+
* difference message.
577+
*/
491578
public void setLastOptionalDifference(DifferenceDescription optionalDifference) {
492-
if (optionalDifference != null && optionalDifference.getDifferenceMessage() != null && !optionalDifference.getDifferenceMessage().isEmpty()) {
579+
if (optionalDifference != null && optionalDifference.getDifferenceMessage() != null
580+
&& !optionalDifference.getDifferenceMessage().isEmpty()) {
493581
this.lastOptionalDifference = optionalDifference;
494582
if (this.parent != null) {
495583
this.parent.setLastOptionalDifference(optionalDifference);
@@ -498,7 +586,12 @@ public void setLastOptionalDifference(DifferenceDescription optionalDifference)
498586
}
499587

500588
/**
501-
* @return true if the instruction following this instruction is a beginOptional rule containing text with a single token
589+
* Determine if the instruction following this one is an optional rule containing text with
590+
* a single token
591+
*
592+
* @return {@code true} if the instruction following this instruction is a
593+
* {@code BEGIN_OPTIONAL} rule containing text with a single token, {@code false}
594+
* otherwise.
502595
*/
503596
public boolean isFollowingInstructionOptionalSingleToken() {
504597
if (parent == null) {
@@ -520,8 +613,12 @@ public boolean isFollowingInstructionOptionalSingleToken() {
520613
}
521614

522615
/**
523-
* @param parseInstruction subInstruction to find the next parse instruction after
524-
* @return the next instruction after parseInstruction in the subInstructions
616+
* Find the next parse instruction that follows the given parse instruction in the list of
617+
* sub-instructions
618+
*
619+
* @param parseInstruction subInstruction to find the next parse instruction after.
620+
* @return The next instruction after parseInstruction in the subInstructions, or
621+
* {@code null} if no such instruction exists.
525622
*/
526623
private ParseInstruction findFollowingInstruction(ParseInstruction parseInstruction) {
527624
if (parseInstruction == null) {
@@ -542,7 +639,9 @@ private ParseInstruction findFollowingInstruction(ParseInstruction parseInstruct
542639
}
543640

544641
/**
545-
* @return the tokens from the next group of optional
642+
* Retrieve the tokens from the next group of optional text
643+
*
644+
* @return The tokens from the next group of optional.
546645
*/
547646
public String[] getNextOptionalTextTokens() {
548647
if (parent == null) {
@@ -588,7 +687,9 @@ public void setSkip(boolean skip) {
588687
}
589688

590689
/**
591-
* @return the next sibling parse instruction which is just text (no rules)
690+
* Retrieve the next sibling parse instruction that contains only text (no rules)
691+
*
692+
* @return The next sibling parse instruction which is just text (no rules).
592693
*/
593694
public ParseInstruction getNextNormalTextInstruction() {
594695
if (this.parent == null) {
@@ -625,14 +726,28 @@ public ParseInstruction getNextNormalTextInstruction() {
625726
}
626727

627728
/**
628-
* @param skipFirstTextToken if true, the first text token will be skipped
729+
* Set whether the first text token of this parse instruction should be skipped during
730+
* matching
731+
*
732+
* This is useful in cases where the first token of the text is optional or should not be
733+
* considered for comparison purposes.
734+
*
735+
* @param skipFirstTextToken If {@code true}, the first text token will be skipped during
736+
* matching; otherwise, it will be included.
629737
*/
630738
public void setSkipFirstToken(boolean skipFirstTextToken) {
631739
this.skipFirstTextToken = skipFirstTextToken;
632740
}
633-
741+
634742
/**
635-
* @return true if the first text token should be skipped
743+
* Check whether the first text token of this parse instruction should be skipped during
744+
* matching
745+
*
746+
* This is useful in cases where the first token of the text is optional or should not be
747+
* considered for comparison purposes.
748+
*
749+
* @return {@code true} if the first text token should be skipped during matching;
750+
* {@code false} otherwise.
636751
*/
637752
public boolean isSkipFirstTextToken() {
638753
return this.skipFirstTextToken;

0 commit comments

Comments
 (0)