Skip to content

Commit 62be5c6

Browse files
committed
refactoring of API signatures
1 parent 5e09361 commit 62be5c6

22 files changed

+168
-236
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/JcpPreprocessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ private List<PreprocessingState.ExcludeIfInfo> processGlobalDirectives(
304304
for (final FileInfoContainer fileRef : files) {
305305
if (!(fileRef.isExcludedFromPreprocessing() || fileRef.isCopyOnly())) {
306306
final long startTime = System.currentTimeMillis();
307-
result.addAll(fileRef.processGlobalDirectives(null, context));
307+
result.addAll(fileRef.processGlobalDirectives(context, null));
308308
final long elapsedTime = System.currentTimeMillis() - startTime;
309309
if (context.isVerbose()) {
310310
context.logForVerbose(String
@@ -377,7 +377,7 @@ private Statistics preprocessFiles(final Collection<FileInfoContainer> files,
377377
}
378378
} else {
379379
final long startTime = System.currentTimeMillis();
380-
fileRef.preprocessFileWithNotification(null, this.context, false);
380+
fileRef.preprocessFileWithNotification(this.context, null, false);
381381
final long elapsedTime = System.currentTimeMillis() - startTime;
382382
if (this.context.isVerbose()) {
383383
this.context.logForVerbose(String

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 100 additions & 103 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.igormaznitsa.jcp.context;
22

3-
import com.igormaznitsa.jcp.containers.FileInfoContainer;
4-
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
5-
63
/**
74
* A custom processor for detecting and handling uncommenting directives in source text.
85
* This processor recognizes lines or blocks marked with `//$` and `//$$` and can either
@@ -19,21 +16,15 @@ public interface CommentTextProcessor extends PreprocessorContextAware, Executio
1916
* Processes uncommented text detected in `//$` or `//$$` sections.
2017
* If no transformation is needed, the original text must be returned unchanged.
2118
*
19+
* @param context the current preprocessor context; must not be null
2220
* @param recommendedIndent the suggested indentation level for the processed text, if any modifications are applied
2321
* @param uncommentedText the text that was uncommented and is subject to processing; must not be null
24-
* @param fileContainer the container holding metadata about the source file invoking the processor; must not be null
25-
* @param positionInfo the position of the uncommented line or the first line of the uncommented block; must not be null
26-
* @param context the current preprocessor context; must not be null
27-
* @param state the current preprocessor state; must not be null
2822
* @return the processed text, which may be unchanged or modified
29-
* @since 7.2.1
23+
* @since 7.3.0
3024
*/
3125
String processUncommentedText(
32-
int recommendedIndent,
33-
String uncommentedText,
34-
FileInfoContainer fileContainer,
35-
FilePositionInfo positionInfo,
3626
PreprocessorContext context,
37-
PreprocessingState state);
27+
int recommendedIndent,
28+
String uncommentedText);
3829

3930
}
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.igormaznitsa.jcp.context;
22

3-
import com.igormaznitsa.jcp.containers.FileInfoContainer;
4-
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
5-
63
/**
74
* Interface describes an abstract object which can decide to be executed or not during execution in specified point.
85
*
@@ -17,16 +14,10 @@ public interface ExecutionAllowable {
1714
* dynamic decision.
1815
* <b>If execution in bounds of test or mock state then some arguments can be null.</b>
1916
*
20-
* @param nullableFileContainer the container holding metadata about the source file invoking the processor, can be null if started not full preprocessing cycle
21-
* @param nullablePositionInfo the position of the uncommented line or the first line of the uncommented block, can be null if started not full preprocessing cycle
22-
* @param context the current preprocessor context; must not be null
23-
* @param state the current preprocessor state; must not be null
17+
* @param context the current preprocessor context; must not be null
2418
* @return {@code true} if it is allowed to run; {@code false} otherwise
2519
*/
2620
boolean isAllowed(
27-
FileInfoContainer nullableFileContainer,
28-
FilePositionInfo nullablePositionInfo,
29-
PreprocessorContext context,
30-
PreprocessingState state
21+
PreprocessorContext context
3122
);
3223
}

jcp/src/main/java/com/igormaznitsa/jcp/context/JCPSpecialVariableProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public Value getVariable(final String varName, final PreprocessorContext context
163163
case VAR_DATE:
164164
return Value.valueOf(dateFormat.format(new Date()));
165165
case VAR_TIMESTAMP:
166-
final TextFileDataContainer filedata = state.peekFile();
166+
final TextFileDataContainer filedata = state.peekIncludeStackFile();
167167
final Value result;
168168
if (filedata == null) {
169169
result = Value.valueOf("<no file>");
@@ -173,7 +173,7 @@ public Value getVariable(final String varName, final PreprocessorContext context
173173
}
174174
return result;
175175
case VAR_LINE:
176-
final TextFileDataContainer currentFile = state.peekFile();
176+
final TextFileDataContainer currentFile = state.peekIncludeStackFile();
177177
final long line;
178178
if (currentFile == null) {
179179
line = -1L;

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ public ResettablePrinter getSelectedPrinter() {
178178

179179
public List<ExcludeIfInfo> popAllExcludeIfInfoData() {
180180
final List<ExcludeIfInfo> result = new ArrayList<>(deferredExcludeStack);
181-
deferredExcludeStack.clear();
181+
this.deferredExcludeStack.clear();
182182
return result;
183183
}
184184

185185

186186
public ExcludeIfInfo popExcludeIfData() {
187-
return deferredExcludeStack.pop();
187+
return this.deferredExcludeStack.pop();
188188
}
189189

190190

191191
public Set<PreprocessingFlag> getPreprocessingFlags() {
192-
return preprocessingFlags;
192+
return this.preprocessingFlags;
193193
}
194194

195195
public ResettablePrinter findPrinter(final PrinterType type) {
@@ -240,25 +240,26 @@ public TextFileDataContainer openFile(final File file) throws IOException {
240240
return newContainer;
241241
}
242242

243-
public TextFileDataContainer peekFile() {
244-
return includeStack.peek();
243+
public TextFileDataContainer peekIncludeStackFile() {
244+
return this.includeStack.peek();
245245
}
246246

247-
public List<TextFileDataContainer> getCurrentIncludeStack() {
247+
public List<TextFileDataContainer> getIncludeStack() {
248248
return this.includeStack;
249249
}
250250

251-
public Optional<TextFileDataContainer> findLastTextFileDataContainerInStack() {
251+
public Optional<TextFileDataContainer> findActiveTextFileDataContainer() {
252252
if (this.isMockMode()) {
253253
return Optional.of(
254254
new TextFileDataContainer(new File(FAKE_FILE_PATH), new String[] {""}, false, 0));
255255
}
256-
return this.includeStack.isEmpty() ? Optional.ofNullable(this.getRootTextContainer()) :
257-
Optional.of(this.includeStack.get(this.includeStack.size() - 1));
256+
final TextFileDataContainer includeFile = this.peekIncludeStackFile();
257+
return includeFile == null ? Optional.ofNullable(this.getRootTextContainer()) :
258+
Optional.of(includeFile);
258259
}
259260

260-
public Optional<FilePositionInfo> findLastPositionInfoInStack() {
261-
return findLastTextFileDataContainerInStack().map(
261+
public Optional<FilePositionInfo> findFilePositionInfo() {
262+
return this.findActiveTextFileDataContainer().map(
262263
x -> new FilePositionInfo(x.getFile(), x.getLastReadStringIndex()));
263264
}
264265

@@ -269,7 +270,7 @@ public FilePositionInfo[] makeIncludeStack() {
269270

270271
final FilePositionInfo[] stack = new FilePositionInfo[includeStack.size()];
271272
for (int i = 0; i < includeStack.size(); i++) {
272-
final TextFileDataContainer fileContainer = includeStack.get(i);
273+
final TextFileDataContainer fileContainer = this.includeStack.get(i);
273274
stack[i] =
274275
new FilePositionInfo(fileContainer.getFile(), fileContainer.getLastReadStringIndex());
275276
}
@@ -288,7 +289,7 @@ public TextFileDataContainer popTextContainer() {
288289
}
289290

290291
public FileInfoContainer getRootFileInfo() {
291-
return rootFileInfo;
292+
return this.rootFileInfo;
292293
}
293294

294295
public boolean isIncludeStackEmpty() {
@@ -300,11 +301,10 @@ public boolean isOnlyRootOnStack() {
300301
}
301302

302303

303-
private TextFileDataContainer cloneTopTextDataContainer(final boolean useLastReadStringIndex) {
304-
final TextFileDataContainer topElement = includeStack.peek();
304+
private TextFileDataContainer cloneTopTextDataContainer() {
305+
final TextFileDataContainer topElement = requireNonNull(includeStack.peek());
305306
return new TextFileDataContainer(topElement,
306-
useLastReadStringIndex ? topElement.getLastReadStringIndex() :
307-
topElement.getNextStringIndex());
307+
topElement.getLastReadStringIndex());
308308
}
309309

310310

@@ -323,7 +323,7 @@ public PreprocessingState popWhile() {
323323

324324

325325
public PreprocessingState pushWhile(final boolean makeActive) {
326-
final TextFileDataContainer whileRef = cloneTopTextDataContainer(true);
326+
final TextFileDataContainer whileRef = cloneTopTextDataContainer();
327327
whileStack.push(whileRef);
328328
if (makeActive) {
329329
activeWhile = whileRef;
@@ -355,7 +355,7 @@ public PreprocessingState goToString(final int stringIndex) {
355355

356356

357357
public PreprocessingState pushIf(final boolean makeActive) {
358-
final TextFileDataContainer ifRef = cloneTopTextDataContainer(true);
358+
final TextFileDataContainer ifRef = cloneTopTextDataContainer();
359359
ifStack.push(ifRef);
360360
if (makeActive) {
361361
activeIf = ifRef;

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContext.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
package com.igormaznitsa.jcp.context;
2323

24-
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findLastActiveFileContainer;
2524
import static java.util.Collections.singletonList;
2625
import static java.util.Objects.requireNonNull;
2726
import static java.util.stream.Collectors.toUnmodifiableList;
@@ -199,7 +198,7 @@ public PreprocessorContext(final PreprocessorContext context) {
199198
this.preprocessorLogger = context.getPreprocessorLogger();
200199

201200
this.commentTextProcessors = new ArrayList<>(context.commentTextProcessors);
202-
this.currentInCloneSource = context.getPreprocessingState().peekFile();
201+
this.currentInCloneSource = context.getPreprocessingState().peekIncludeStackFile();
203202
}
204203

205204
private static String makeStackView(
@@ -768,12 +767,7 @@ private Optional<SpecialVariableProcessor> findAllowedSpecialVariableProcessor(
768767
final String normalizedName) {
769768
return this.mapVariableNameToSpecialVarProcessor.get(normalizedName)
770769
.stream()
771-
.filter(x -> x.isAllowed(
772-
findLastActiveFileContainer(this).orElse(null),
773-
this.getPreprocessingState().findLastPositionInfoInStack().orElse(null),
774-
this,
775-
this.getPreprocessingState()
776-
)).findFirst();
770+
.filter(x -> x.isAllowed(this)).findFirst();
777771
}
778772

779773
/**
@@ -858,12 +852,7 @@ public Value findVariableForName(final String name, final boolean enforceUnknown
858852
final SpecialVariableProcessor processor =
859853
mapVariableNameToSpecialVarProcessor.containsKey(normalized) ?
860854
mapVariableNameToSpecialVarProcessor.get(normalized).
861-
stream().filter(x -> x.isAllowed(
862-
findLastActiveFileContainer(this).orElse(null),
863-
this.getPreprocessingState().findLastPositionInfoInStack().orElse(null),
864-
this,
865-
this.getPreprocessingState()
866-
)).findFirst().orElse(null) : null;
855+
stream().filter(x -> x.isAllowed(this)).findFirst().orElse(null) : null;
867856

868857
if (processor != null) {
869858
return processor.getVariable(normalized, this);
@@ -976,7 +965,7 @@ public File findFileInSources(final String path, final boolean mustExist) {
976965

977966
File result = null;
978967

979-
final TextFileDataContainer theFile = this.getPreprocessingState().peekFile();
968+
final TextFileDataContainer theFile = this.getPreprocessingState().peekIncludeStackFile();
980969
final String parentDir = theFile == null ? null : theFile.getFile().getParent();
981970

982971
final File resultFile = new File(path);
@@ -1142,7 +1131,7 @@ public void logForVerbose(final String str) {
11421131
if (isVerbose()) {
11431132
final String stack;
11441133
stack = makeStackView(this.currentInCloneSource, this.cloned,
1145-
this.getPreprocessingState().getCurrentIncludeStack());
1134+
this.getPreprocessingState().getIncludeStack());
11461135
this.logInfo(str + (stack.isEmpty() ? ' ' : '\n') + stack);
11471136
}
11481137
}

jcp/src/main/java/com/igormaznitsa/jcp/context/SpecialVariableProcessor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
package com.igormaznitsa.jcp.context;
2323

24-
import com.igormaznitsa.jcp.containers.FileInfoContainer;
25-
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
2624
import com.igormaznitsa.jcp.expression.Value;
2725
import java.util.Set;
2826

@@ -44,11 +42,7 @@ public interface SpecialVariableProcessor extends PreprocessorContextAware,
4442
Set<String> getVariableNames();
4543

4644
@Override
47-
default boolean isAllowed(
48-
FileInfoContainer fileContainer,
49-
FilePositionInfo positionInfo,
50-
PreprocessorContext context,
51-
PreprocessingState state) {
45+
default boolean isAllowed(PreprocessorContext context) {
5246
return true;
5347
}
5448

jcp/src/main/java/com/igormaznitsa/jcp/directives/ActionDirectiveHandler.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
package com.igormaznitsa.jcp.directives;
2323

24-
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findLastActiveFileContainer;
25-
2624
import com.igormaznitsa.jcp.context.PreprocessingState;
2725
import com.igormaznitsa.jcp.context.PreprocessorContext;
2826
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
@@ -75,14 +73,7 @@ public AfterDirectiveProcessingBehaviour execute(final String string,
7573
try {
7674
final List<ExpressionTree> args = parseString(string, context);
7775
final PreprocessorExtension extension = extensions.stream()
78-
.filter(x -> x.isAllowed(
79-
findLastActiveFileContainer(context).orElseThrow(
80-
() -> new IllegalStateException("Can't find active file container")),
81-
context.getPreprocessingState().findLastPositionInfoInStack().orElseThrow(
82-
() -> new IllegalStateException("Can't find last position in include stack")),
83-
context,
84-
context.getPreprocessingState()
85-
))
76+
.filter(x -> x.isAllowed(context))
8677
.filter(x -> x.hasAction(args.size()))
8778
.findFirst().orElse(null);
8879

jcp/src/main/java/com/igormaznitsa/jcp/directives/ExcludeIfDirectiveHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public AfterDirectiveProcessingBehaviour execute(final String string,
6262
final PreprocessorContext context) {
6363
final PreprocessingState state = context.getPreprocessingState();
6464
state.pushExcludeIfData(state.getRootFileInfo(), string,
65-
Objects.requireNonNull(state.peekFile(), "'IF' stack is empty!").getLastReadStringIndex());
65+
Objects.requireNonNull(state.peekIncludeStackFile(), "'IF' stack is empty!")
66+
.getLastReadStringIndex());
6667
return AfterDirectiveProcessingBehaviour.PROCESSED;
6768
}
6869
}

0 commit comments

Comments
 (0)