diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java index 5839629f0..41b803bfa 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java @@ -206,24 +206,17 @@ public final class ZLTextParagraphCursor { } public final int Index; + final ZLTextView View; public final ZLTextModel Model; private final ArrayList myElements = new ArrayList(); - private ZLTextParagraphCursor(ZLTextModel model, int index) { + ZLTextParagraphCursor(ZLTextView view, ZLTextModel model, int index) { + View = view; Model = model; - Index = Math.min(index, Model.getParagraphsNumber() - 1); + Index = Math.min(index, model.getParagraphsNumber() - 1); 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 = { ' ' }; void fill() { ZLTextParagraph paragraph = Model.getParagraph(Index); @@ -256,11 +249,11 @@ public final class ZLTextParagraphCursor { } public boolean isLast() { - return (Index + 1 >= Model.getParagraphsNumber()); + return Index + 1 >= Model.getParagraphsNumber(); } 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() { @@ -268,11 +261,11 @@ public final class ZLTextParagraphCursor { } public ZLTextParagraphCursor previous() { - return isFirst() ? null : cursor(Model, Index - 1); + return isFirst() ? null : View.cursor(Index - 1); } public ZLTextParagraphCursor next() { - return isLast() ? null : cursor(Model, Index + 1); + return isLast() ? null : View.cursor(Index + 1); } ZLTextElement getElement(int index) { diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java index 6042d9639..7952f3cd2 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java @@ -44,18 +44,18 @@ class ZLTextParagraphCursorCache { } } - private static final HashMap> ourMap = new HashMap>(); + private final HashMap> myMap = new HashMap>(); - public static void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) { - ourMap.put(new Key(model, index), new WeakReference(cursor)); + void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) { + myMap.put(new Key(model, index), new WeakReference(cursor)); } - public static ZLTextParagraphCursor get(ZLTextModel model, int index) { - WeakReference ref = ourMap.get(new Key(model, index)); - return (ref != null) ? ref.get() : null; + ZLTextParagraphCursor get(ZLTextModel model, int index) { + WeakReference ref = myMap.get(new Key(model, index)); + return ref != null ? ref.get() : null; } - public static void clear() { - ourMap.clear(); + void clear() { + myMap.clear(); } } diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextSelection.java b/src/org/geometerplus/zlibrary/text/view/ZLTextSelection.java index ca6eb3b55..147808a71 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextSelection.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextSelection.java @@ -196,8 +196,7 @@ class ZLTextSelection extends ZLTextHighlighting { if (isEmpty()) { return null; } - final ZLTextParagraphCursor cursor = - ZLTextParagraphCursor.cursor(myView.getModel(), myRightMostRegionSoul.ParagraphIndex); + final ZLTextParagraphCursor cursor = myView.cursor(myRightMostRegionSoul.ParagraphIndex); final ZLTextElement element = cursor.getElement(myRightMostRegionSoul.EndElementIndex); return new ZLTextFixedPosition( myRightMostRegionSoul.ParagraphIndex, diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextTraverser.java b/src/org/geometerplus/zlibrary/text/view/ZLTextTraverser.java index a193c4f24..1e40d7477 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextTraverser.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextTraverser.java @@ -34,8 +34,7 @@ public abstract class ZLTextTraverser { public void traverse(ZLTextPosition from, ZLTextPosition to) { final int fromParagraph = from.getParagraphIndex(); final int toParagraph = to.getParagraphIndex(); - ZLTextParagraphCursor cursor = - ZLTextParagraphCursor.cursor(myView.getModel(), fromParagraph); + ZLTextParagraphCursor cursor = myView.cursor(fromParagraph); for (int i = fromParagraph; i <= toParagraph; ++i) { final int fromElement = i == fromParagraph ? from.getElementIndex() : 0; final int toElement = i == toParagraph ? to.getElementIndex() : cursor.getParagraphLength() - 1; diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java index 3fde2a308..c722a3aa7 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java @@ -64,12 +64,14 @@ public abstract class ZLTextView extends ZLTextViewBase { private final Set myHighlightings = Collections.synchronizedSet(new TreeSet()); + private final ZLTextParagraphCursorCache myCursorCache = new ZLTextParagraphCursorCache(); + public ZLTextView(ZLApplication application) { super(application); } public synchronized void setModel(ZLTextModel model) { - ZLTextParagraphCursorCache.clear(); + myCursorCache.clear(); mySelection.clear(); myHighlightings.clear(); @@ -81,7 +83,7 @@ public abstract class ZLTextView extends ZLTextViewBase { if (myModel != null) { final int paragraphsNumber = myModel.getParagraphsNumber(); if (paragraphsNumber > 0) { - myCurrentPage.moveStartCursor(ZLTextParagraphCursor.cursor(myModel, 0)); + myCurrentPage.moveStartCursor(cursor(0)); } } Application.getViewWidget().reset(); @@ -1580,7 +1582,7 @@ public abstract class ZLTextView extends ZLTextViewBase { protected synchronized void rebuildPaintInfo() { myPreviousPage.reset(); myNextPage.reset(); - ZLTextParagraphCursorCache.clear(); + myCursorCache.clear(); if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) { 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; + } } diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java b/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java index 62677271c..e89b165d7 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java @@ -163,7 +163,7 @@ public final class ZLTextWordCursor extends ZLTextPosition { if (!isNull() && (paragraphIndex != myParagraphCursor.Index)) { final ZLTextModel model = myParagraphCursor.Model; paragraphIndex = Math.max(0, Math.min(paragraphIndex, model.getParagraphsNumber() - 1)); - myParagraphCursor = ZLTextParagraphCursor.cursor(model, paragraphIndex); + myParagraphCursor = myParagraphCursor.View.cursor(paragraphIndex); moveToParagraphStart(); } }