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

Map => LruCache

This commit is contained in:
Nikolay Pultsin 2015-04-23 04:29:01 +01:00
parent 74d5245ff5
commit 461155b46f
4 changed files with 12 additions and 24 deletions

View file

@ -19,34 +19,22 @@
package org.geometerplus.zlibrary.text.view; package org.geometerplus.zlibrary.text.view;
import java.lang.ref.WeakReference; import android.support.v4.util.LruCache;
import java.util.*;
import org.geometerplus.zlibrary.text.model.ZLTextModel; import org.geometerplus.zlibrary.text.model.ZLTextModel;
final class CursorManager { final class CursorManager extends LruCache<Integer,ZLTextParagraphCursor> {
private final ZLTextModel myModel; private final ZLTextModel myModel;
final ExtensionElementManager ExtensionManager; final ExtensionElementManager ExtensionManager;
CursorManager(ZLTextModel model, ExtensionElementManager extManager) { CursorManager(ZLTextModel model, ExtensionElementManager extManager) {
super(200); // max 200 cursors in the cache
myModel = model; myModel = model;
ExtensionManager = extManager; ExtensionManager = extManager;
} }
private final HashMap<Integer,WeakReference<ZLTextParagraphCursor>> myMap = @Override
new HashMap<Integer,WeakReference<ZLTextParagraphCursor>>(); protected ZLTextParagraphCursor create(Integer index) {
return new ZLTextParagraphCursor(this, myModel, index);
ZLTextParagraphCursor cursor(int index) {
final WeakReference<ZLTextParagraphCursor> 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<ZLTextParagraphCursor>(result));
}
return result;
}
void clear() {
myMap.clear();
} }
} }

View file

@ -265,11 +265,11 @@ public final class ZLTextParagraphCursor {
} }
public ZLTextParagraphCursor previous() { public ZLTextParagraphCursor previous() {
return isFirst() ? null : CursorManager.cursor(Index - 1); return isFirst() ? null : CursorManager.get(Index - 1);
} }
public ZLTextParagraphCursor next() { public ZLTextParagraphCursor next() {
return isLast() ? null : CursorManager.cursor(Index + 1); return isLast() ? null : CursorManager.get(Index + 1);
} }
ZLTextElement getElement(int index) { ZLTextElement getElement(int index) {

View file

@ -83,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(myCursorManager.cursor(0)); myCurrentPage.moveStartCursor(myCursorManager.get(0));
} }
} }
Application.getViewWidget().reset(); Application.getViewWidget().reset();
@ -1588,7 +1588,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
myPreviousPage.reset(); myPreviousPage.reset();
myNextPage.reset(); myNextPage.reset();
if (myCursorManager != null) { if (myCursorManager != null) {
myCursorManager.clear(); myCursorManager.evictAll();
} }
if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) { if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) {
@ -1852,7 +1852,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
} }
ZLTextParagraphCursor cursor(int index) { ZLTextParagraphCursor cursor(int index) {
return myCursorManager.cursor(index); return myCursorManager.get(index);
} }
protected abstract ExtensionElementManager getExtensionManager(); protected abstract ExtensionElementManager getExtensionManager();

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 = myParagraphCursor.CursorManager.cursor(paragraphIndex); myParagraphCursor = myParagraphCursor.CursorManager.get(paragraphIndex);
moveToParagraphStart(); moveToParagraphStart();
} }
} }