1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +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;
final ZLTextView View;
public final ZLTextModel Model;
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;
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) {

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) {
ourMap.put(new Key(model, index), new WeakReference<ZLTextParagraphCursor>(cursor));
void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) {
myMap.put(new Key(model, index), new WeakReference<ZLTextParagraphCursor>(cursor));
}
public static ZLTextParagraphCursor get(ZLTextModel model, int index) {
WeakReference<ZLTextParagraphCursor> ref = ourMap.get(new Key(model, index));
return (ref != null) ? ref.get() : null;
ZLTextParagraphCursor get(ZLTextModel model, int index) {
WeakReference<ZLTextParagraphCursor> ref = myMap.get(new Key(model, index));
return ref != null ? ref.get() : null;
}
public static void clear() {
ourMap.clear();
void clear() {
myMap.clear();
}
}

View file

@ -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,

View file

@ -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;

View file

@ -64,12 +64,14 @@ public abstract class ZLTextView extends ZLTextViewBase {
private final Set<ZLTextHighlighting> myHighlightings =
Collections.synchronizedSet(new TreeSet<ZLTextHighlighting>());
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;
}
}

View file

@ -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();
}
}