Skip to content

Commit

Permalink
#429 [refactor] make class LayoutState immutable
Browse files Browse the repository at this point in the history
* make all LayoutState.* fields final
* pass all values into constructor instead of using setters
  • Loading branch information
asolntsev committed Oct 28, 2024
1 parent 31e966e commit 9b6d2a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyList;

/**
* This class tracks state which changes over the course of a layout run.
* Generally speaking, if possible, state information should be stored in the box
Expand All @@ -66,7 +68,7 @@ public class LayoutContext implements CssContext {
@Nullable
private MarkerData _currentMarkerData;

private final Deque<BlockFormattingContext> _bfcs = new ArrayDeque<>();
private final Deque<BlockFormattingContext> _blockFormattingContexts = new ArrayDeque<>();
private final Deque<Layer> _layers = new ArrayDeque<>();

private final FontContext _fontContext;
Expand Down Expand Up @@ -130,7 +132,7 @@ public void reInit(boolean keepLayers) {
_firstLetters = new StyleTracker();
_currentMarkerData = null;

_bfcs.clear();
_blockFormattingContexts.clear();

if (! keepLayers) {
_rootLayer = null;
Expand All @@ -142,22 +144,9 @@ public void reInit(boolean keepLayers) {
}

public LayoutState captureLayoutState() {
LayoutState result = new LayoutState();

result.setFirstLines(_firstLines);
result.setFirstLetters(_firstLetters);
result.setCurrentMarkerData(_currentMarkerData);

result.setBFCs(_bfcs);

if (isPrint()) {
result.setPageName(getPageName());
result.setExtraSpaceBottom(getExtraSpaceBottom());
result.setExtraSpaceTop(getExtraSpaceTop());
result.setNoPageBreak(getNoPageBreak());
}

return result;
return isPrint() ?
new LayoutState(_firstLines, _firstLetters, _currentMarkerData, _blockFormattingContexts, getPageName(), getExtraSpaceBottom(), getExtraSpaceTop(), getNoPageBreak()) :
new LayoutState(_firstLines, _firstLetters, _currentMarkerData, _blockFormattingContexts);
}

public void restoreLayoutState(LayoutState layoutState) {
Expand All @@ -166,8 +155,8 @@ public void restoreLayoutState(LayoutState layoutState) {

_currentMarkerData = layoutState.getCurrentMarkerData();

_bfcs.clear();
_bfcs.addAll(layoutState.getBFCs());
_blockFormattingContexts.clear();
_blockFormattingContexts.addAll(layoutState.getBFCs());

if (isPrint()) {
setPageName(layoutState.getPageName());
Expand All @@ -178,17 +167,9 @@ public void restoreLayoutState(LayoutState layoutState) {
}

public LayoutState copyStateForRelayout() {
LayoutState result = new LayoutState();

result.setFirstLetters(_firstLetters.copyOf());
result.setFirstLines(_firstLines.copyOf());
result.setCurrentMarkerData(_currentMarkerData);

if (isPrint()) {
result.setPageName(getPageName());
}

return result;
return isPrint() ?
new LayoutState(_firstLines.copyOf(), _firstLetters.copyOf(), _currentMarkerData, emptyList(), getPageName(), 0, 0, 0) :
new LayoutState(_firstLines.copyOf(), _firstLetters.copyOf(), _currentMarkerData, emptyList());
}

public void restoreStateForRelayout(LayoutState layoutState) {
Expand All @@ -203,15 +184,15 @@ public void restoreStateForRelayout(LayoutState layoutState) {
}

public BlockFormattingContext getBlockFormattingContext() {
return _bfcs.getLast();
return _blockFormattingContexts.getLast();
}

public void pushBFC(BlockFormattingContext bfc) {
_bfcs.add(bfc);
_blockFormattingContexts.add(bfc);
}

public void popBFC() {
_bfcs.removeLast();
_blockFormattingContexts.removeLast();
}

public void pushLayer(Box master) {
Expand Down Expand Up @@ -245,6 +226,7 @@ public Layer getLayer() {
return _layers.getLast();
}

@Nullable
public Layer getRootLayer() {
return _rootLayer;
}
Expand Down Expand Up @@ -352,7 +334,7 @@ public void setExtraSpaceTop(int extraSpaceTop) {
_extraSpaceTop = extraSpaceTop;
}

public void resolveCounters(CalculatedStyle style, Integer startIndex) {
public void resolveCounters(CalculatedStyle style, @Nullable Integer startIndex) {
//new context for child elements
CounterContext cc = new CounterContext(style, startIndex);
_counterContextMap.put(style, cc);
Expand Down Expand Up @@ -500,7 +482,7 @@ public String getPageName() {
return _pageName;
}

public void setPageName(String currentPageName) {
public void setPageName(@Nullable String currentPageName) {
_pageName = currentPageName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,80 +33,69 @@
* It is only created as needed.
*/
public class LayoutState {
private StyleTracker _firstLines;
private StyleTracker _firstLetters;
private final StyleTracker _firstLines;
private final StyleTracker _firstLetters;

private MarkerData _currentMarkerData;
@Nullable
private final MarkerData _currentMarkerData;

private Deque<BlockFormattingContext> _BFCs;
private final Deque<BlockFormattingContext> _BFCs;

private String _pageName;
private int _extraSpaceTop;
private int _extraSpaceBottom;
private int _noPageBreak;
@Nullable
private final String _pageName;
private final int _extraSpaceTop;
private final int _extraSpaceBottom;
private final int _noPageBreak;

public Deque<BlockFormattingContext> getBFCs() {
return _BFCs;
public LayoutState(StyleTracker firstLines, StyleTracker firstLetters, @Nullable MarkerData currentMarkerData,
Collection<BlockFormattingContext> blockFormattingContexts,
@Nullable String pageName, int extraSpaceTop, int extraSpaceBottom, int noPageBreak) {
this._firstLines = firstLines;
this._firstLetters = firstLetters;
this._currentMarkerData = currentMarkerData;
this._BFCs = new ArrayDeque<>(blockFormattingContexts);
this._pageName = pageName;
this._extraSpaceTop = extraSpaceTop;
this._extraSpaceBottom = extraSpaceBottom;
this._noPageBreak = noPageBreak;
}

public LayoutState(StyleTracker firstLines, StyleTracker firstLetters, @Nullable MarkerData currentMarkerData,
Collection<BlockFormattingContext> blockFormattingContexts) {
this(firstLines, firstLetters, currentMarkerData, blockFormattingContexts, null, 0, 0, 0);
}

public void setBFCs(Collection<BlockFormattingContext> s) {
_BFCs = new ArrayDeque<>(s);
public Deque<BlockFormattingContext> getBFCs() {
return _BFCs;
}

@Nullable
public MarkerData getCurrentMarkerData() {
return _currentMarkerData;
}

public void setCurrentMarkerData(MarkerData currentMarkerData) {
_currentMarkerData = currentMarkerData;
}

public StyleTracker getFirstLetters() {
return _firstLetters;
}

public void setFirstLetters(StyleTracker firstLetters) {
_firstLetters = firstLetters;
}

public StyleTracker getFirstLines() {
return _firstLines;
}

public void setFirstLines(StyleTracker firstLines) {
_firstLines = firstLines;
}

@Nullable
public String getPageName() {
return _pageName;
}

public void setPageName(String pageName) {
_pageName = pageName;
}

public int getExtraSpaceTop() {
return _extraSpaceTop;
}

public void setExtraSpaceTop(int extraSpaceTop) {
_extraSpaceTop = extraSpaceTop;
}

public int getExtraSpaceBottom() {
return _extraSpaceBottom;
}

public void setExtraSpaceBottom(int extraSpaceBottom) {
_extraSpaceBottom = extraSpaceBottom;
}

public int getNoPageBreak() {
return _noPageBreak;
}

public void setNoPageBreak(int noPageBreak) {
_noPageBreak = noPageBreak;
}
}

0 comments on commit 9b6d2a0

Please sign in to comment.