1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 02:09:35 +02:00

ZLTextParagraphCursorCache is not static

This commit is contained in:
Nikolay Pultsin 2014-12-19 22:53:39 +00:00
parent a58183e5d8
commit edb5a662e5
6 changed files with 33 additions and 31 deletions

View file

@ -206,24 +206,17 @@ public final class ZLTextParagraphCursor {
} }
public final int Index; public final int Index;
final ZLTextView View;
public final ZLTextModel Model; public final ZLTextModel Model;
private final ArrayList<ZLTextElement> myElements = new ArrayList<ZLTextElement>(); private final ArrayList<ZLTextElement> myElements = new ArrayList<ZLTextElement>();
private ZLTextParagraphCursor(ZLTextModel model, int index) { ZLTextParagraphCursor(ZLTextView view, ZLTextModel model, int index) {
View = view;
Model = model; Model = model;
Index = Math.min(index, Model.getParagraphsNumber() - 1); Index = Math.min(index, model.getParagraphsNumber() - 1);
fill(); fill();
} }
static ZLTextParagraphCursor cursor(ZLTextModel model, int index) {
ZLTextParagraphCursor result = ZLTextParagraphCursorCache.get(model, index);
if (result == null) {
result = new ZLTextParagraphCursor(model, index);
ZLTextParagraphCursorCache.put(model, index, result);
}
return result;
}
private static final char[] SPACE_ARRAY = { ' ' }; private static final char[] SPACE_ARRAY = { ' ' };
void fill() { void fill() {
ZLTextParagraph paragraph = Model.getParagraph(Index); ZLTextParagraph paragraph = Model.getParagraph(Index);
@ -256,11 +249,11 @@ public final class ZLTextParagraphCursor {
} }
public boolean isLast() { public boolean isLast() {
return (Index + 1 >= Model.getParagraphsNumber()); return Index + 1 >= Model.getParagraphsNumber();
} }
public boolean isEndOfSection() { public boolean isEndOfSection() {
return (Model.getParagraph(Index).getKind() == ZLTextParagraph.Kind.END_OF_SECTION_PARAGRAPH); return Model.getParagraph(Index).getKind() == ZLTextParagraph.Kind.END_OF_SECTION_PARAGRAPH;
} }
int getParagraphLength() { int getParagraphLength() {
@ -268,11 +261,11 @@ public final class ZLTextParagraphCursor {
} }
public ZLTextParagraphCursor previous() { public ZLTextParagraphCursor previous() {
return isFirst() ? null : cursor(Model, Index - 1); return isFirst() ? null : View.cursor(Index - 1);
} }
public ZLTextParagraphCursor next() { public ZLTextParagraphCursor next() {
return isLast() ? null : cursor(Model, Index + 1); return isLast() ? null : View.cursor(Index + 1);
} }
ZLTextElement getElement(int index) { ZLTextElement getElement(int index) {

View file

@ -44,18 +44,18 @@ class ZLTextParagraphCursorCache {
} }
} }
private static final HashMap<Key,WeakReference<ZLTextParagraphCursor>> ourMap = new HashMap<Key,WeakReference<ZLTextParagraphCursor>>(); private final HashMap<Key,WeakReference<ZLTextParagraphCursor>> myMap = new HashMap<Key,WeakReference<ZLTextParagraphCursor>>();
public static void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) { void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) {
ourMap.put(new Key(model, index), new WeakReference<ZLTextParagraphCursor>(cursor)); myMap.put(new Key(model, index), new WeakReference<ZLTextParagraphCursor>(cursor));
} }
public static ZLTextParagraphCursor get(ZLTextModel model, int index) { ZLTextParagraphCursor get(ZLTextModel model, int index) {
WeakReference<ZLTextParagraphCursor> ref = ourMap.get(new Key(model, index)); WeakReference<ZLTextParagraphCursor> ref = myMap.get(new Key(model, index));
return (ref != null) ? ref.get() : null; return ref != null ? ref.get() : null;
} }
public static void clear() { void clear() {
ourMap.clear(); myMap.clear();
} }
} }

View file

@ -196,8 +196,7 @@ class ZLTextSelection extends ZLTextHighlighting {
if (isEmpty()) { if (isEmpty()) {
return null; return null;
} }
final ZLTextParagraphCursor cursor = final ZLTextParagraphCursor cursor = myView.cursor(myRightMostRegionSoul.ParagraphIndex);
ZLTextParagraphCursor.cursor(myView.getModel(), myRightMostRegionSoul.ParagraphIndex);
final ZLTextElement element = cursor.getElement(myRightMostRegionSoul.EndElementIndex); final ZLTextElement element = cursor.getElement(myRightMostRegionSoul.EndElementIndex);
return new ZLTextFixedPosition( return new ZLTextFixedPosition(
myRightMostRegionSoul.ParagraphIndex, myRightMostRegionSoul.ParagraphIndex,

View file

@ -34,8 +34,7 @@ public abstract class ZLTextTraverser {
public void traverse(ZLTextPosition from, ZLTextPosition to) { public void traverse(ZLTextPosition from, ZLTextPosition to) {
final int fromParagraph = from.getParagraphIndex(); final int fromParagraph = from.getParagraphIndex();
final int toParagraph = to.getParagraphIndex(); final int toParagraph = to.getParagraphIndex();
ZLTextParagraphCursor cursor = ZLTextParagraphCursor cursor = myView.cursor(fromParagraph);
ZLTextParagraphCursor.cursor(myView.getModel(), fromParagraph);
for (int i = fromParagraph; i <= toParagraph; ++i) { for (int i = fromParagraph; i <= toParagraph; ++i) {
final int fromElement = i == fromParagraph ? from.getElementIndex() : 0; final int fromElement = i == fromParagraph ? from.getElementIndex() : 0;
final int toElement = i == toParagraph ? to.getElementIndex() : cursor.getParagraphLength() - 1; final int toElement = i == toParagraph ? to.getElementIndex() : cursor.getParagraphLength() - 1;

View file

@ -64,12 +64,14 @@ public abstract class ZLTextView extends ZLTextViewBase {
private final Set<ZLTextHighlighting> myHighlightings = private final Set<ZLTextHighlighting> myHighlightings =
Collections.synchronizedSet(new TreeSet<ZLTextHighlighting>()); Collections.synchronizedSet(new TreeSet<ZLTextHighlighting>());
private final ZLTextParagraphCursorCache myCursorCache = new ZLTextParagraphCursorCache();
public ZLTextView(ZLApplication application) { public ZLTextView(ZLApplication application) {
super(application); super(application);
} }
public synchronized void setModel(ZLTextModel model) { public synchronized void setModel(ZLTextModel model) {
ZLTextParagraphCursorCache.clear(); myCursorCache.clear();
mySelection.clear(); mySelection.clear();
myHighlightings.clear(); myHighlightings.clear();
@ -81,7 +83,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
if (myModel != null) { if (myModel != null) {
final int paragraphsNumber = myModel.getParagraphsNumber(); final int paragraphsNumber = myModel.getParagraphsNumber();
if (paragraphsNumber > 0) { if (paragraphsNumber > 0) {
myCurrentPage.moveStartCursor(ZLTextParagraphCursor.cursor(myModel, 0)); myCurrentPage.moveStartCursor(cursor(0));
} }
} }
Application.getViewWidget().reset(); Application.getViewWidget().reset();
@ -1580,7 +1582,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
protected synchronized void rebuildPaintInfo() { protected synchronized void rebuildPaintInfo() {
myPreviousPage.reset(); myPreviousPage.reset();
myNextPage.reset(); myNextPage.reset();
ZLTextParagraphCursorCache.clear(); myCursorCache.clear();
if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) { if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) {
myCurrentPage.LineInfos.clear(); myCurrentPage.LineInfos.clear();
@ -1841,4 +1843,13 @@ public abstract class ZLTextView extends ZLTextViewBase {
} }
} }
} }
ZLTextParagraphCursor cursor(int index) {
ZLTextParagraphCursor result = myCursorCache.get(myModel, index);
if (result == null) {
result = new ZLTextParagraphCursor(this, myModel, index);
myCursorCache.put(myModel, index, result);
}
return result;
}
} }

View file

@ -163,7 +163,7 @@ public final class ZLTextWordCursor extends ZLTextPosition {
if (!isNull() && (paragraphIndex != myParagraphCursor.Index)) { if (!isNull() && (paragraphIndex != myParagraphCursor.Index)) {
final ZLTextModel model = myParagraphCursor.Model; final ZLTextModel model = myParagraphCursor.Model;
paragraphIndex = Math.max(0, Math.min(paragraphIndex, model.getParagraphsNumber() - 1)); paragraphIndex = Math.max(0, Math.min(paragraphIndex, model.getParagraphsNumber() - 1));
myParagraphCursor = ZLTextParagraphCursor.cursor(model, paragraphIndex); myParagraphCursor = myParagraphCursor.View.cursor(paragraphIndex);
moveToParagraphStart(); moveToParagraphStart();
} }
} }