diff --git a/src/org/geometerplus/zlibrary/text/view/CursorManager.java b/src/org/geometerplus/zlibrary/text/view/CursorManager.java index a399a1f29..a97a392e4 100644 --- a/src/org/geometerplus/zlibrary/text/view/CursorManager.java +++ b/src/org/geometerplus/zlibrary/text/view/CursorManager.java @@ -19,34 +19,22 @@ package org.geometerplus.zlibrary.text.view; -import java.lang.ref.WeakReference; -import java.util.*; +import android.support.v4.util.LruCache; import org.geometerplus.zlibrary.text.model.ZLTextModel; -final class CursorManager { +final class CursorManager extends LruCache { private final ZLTextModel myModel; final ExtensionElementManager ExtensionManager; CursorManager(ZLTextModel model, ExtensionElementManager extManager) { + super(200); // max 200 cursors in the cache myModel = model; ExtensionManager = extManager; } - private final HashMap> myMap = - new HashMap>(); - - ZLTextParagraphCursor cursor(int index) { - final WeakReference ref = myMap.get(index); - ZLTextParagraphCursor result = ref != null ? ref.get() : null; - if (result == null) { - result = new ZLTextParagraphCursor(this, myModel, index); - myMap.put(index, new WeakReference(result)); - } - return result; - } - - void clear() { - myMap.clear(); + @Override + protected ZLTextParagraphCursor create(Integer index) { + return new ZLTextParagraphCursor(this, myModel, index); } } diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java index 28cb50522..6f371f05a 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java @@ -265,11 +265,11 @@ public final class ZLTextParagraphCursor { } public ZLTextParagraphCursor previous() { - return isFirst() ? null : CursorManager.cursor(Index - 1); + return isFirst() ? null : CursorManager.get(Index - 1); } public ZLTextParagraphCursor next() { - return isLast() ? null : CursorManager.cursor(Index + 1); + return isLast() ? null : CursorManager.get(Index + 1); } ZLTextElement getElement(int index) { diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java index 26d73458b..82dde51a5 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java @@ -83,7 +83,7 @@ public abstract class ZLTextView extends ZLTextViewBase { if (myModel != null) { final int paragraphsNumber = myModel.getParagraphsNumber(); if (paragraphsNumber > 0) { - myCurrentPage.moveStartCursor(myCursorManager.cursor(0)); + myCurrentPage.moveStartCursor(myCursorManager.get(0)); } } Application.getViewWidget().reset(); @@ -1588,7 +1588,7 @@ public abstract class ZLTextView extends ZLTextViewBase { myPreviousPage.reset(); myNextPage.reset(); if (myCursorManager != null) { - myCursorManager.clear(); + myCursorManager.evictAll(); } if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) { @@ -1852,7 +1852,7 @@ public abstract class ZLTextView extends ZLTextViewBase { } ZLTextParagraphCursor cursor(int index) { - return myCursorManager.cursor(index); + return myCursorManager.get(index); } protected abstract ExtensionElementManager getExtensionManager(); diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java b/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java index 0b64987ac..3a6927412 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 = myParagraphCursor.CursorManager.cursor(paragraphIndex); + myParagraphCursor = myParagraphCursor.CursorManager.get(paragraphIndex); moveToParagraphStart(); } }