mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 19:42:17 +02:00
text model optimization (in progress)
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@400 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
parent
d4c920db65
commit
892ab3350f
9 changed files with 61 additions and 24 deletions
|
@ -2,7 +2,7 @@ package org.zlibrary.text.model;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
public interface ZLTextParagraph {
|
||||
public interface ZLTextParagraph extends Iterable<ZLTextParagraph.Entry> {
|
||||
interface Entry {
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,4 @@ public interface ZLTextParagraph {
|
|||
Kind getKind();
|
||||
int getEntryNumber();
|
||||
int getTextLength();
|
||||
List<Entry> getEntries();
|
||||
void addEntry(Entry entry);
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ public class ZLModelFactory {
|
|||
return new ZLTextTreeModelImpl();
|
||||
}
|
||||
//paragraphs
|
||||
/*
|
||||
public ZLTextParagraph createParagraph() {
|
||||
return new ZLTextParagraphImpl();
|
||||
}
|
||||
|
@ -32,6 +33,7 @@ public class ZLModelFactory {
|
|||
public ZLTextTreeParagraph createTreeParagraph() {
|
||||
return new ZLTextTreeParagraphImpl(null);
|
||||
}
|
||||
*/
|
||||
|
||||
//entries
|
||||
public ZLTextControlEntry createControlEntry(byte kind, boolean isStart) {
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.Map;
|
|||
|
||||
abstract class ZLTextModelImpl implements ZLTextModel {
|
||||
private final ArrayList<ZLTextParagraph> myParagraphs = new ArrayList<ZLTextParagraph>();
|
||||
protected final ArrayList<ZLTextParagraph.Entry> myEntries = new ArrayList<ZLTextParagraph.Entry>();
|
||||
|
||||
private static final int DATA_BLOCK_SIZE = 51200;
|
||||
private int myBlockOffset = 0;
|
||||
|
@ -34,7 +35,7 @@ abstract class ZLTextModelImpl implements ZLTextModel {
|
|||
|
||||
private void addEntry(ZLTextParagraph.Entry entry) {
|
||||
final ArrayList<ZLTextParagraph> paragraphs = myParagraphs;
|
||||
paragraphs.get(paragraphs.size() - 1).addEntry(entry);
|
||||
((ZLTextParagraphImpl)paragraphs.get(paragraphs.size() - 1)).addEntry(entry);
|
||||
}
|
||||
|
||||
public void addControl(byte textKind, boolean isStart) {
|
||||
|
@ -95,7 +96,7 @@ abstract class ZLTextModelImpl implements ZLTextModel {
|
|||
StringBuilder sb = new StringBuilder();
|
||||
for (ZLTextParagraph paragraph: myParagraphs) {
|
||||
sb.append("[PARAGRAPH]\n");
|
||||
for (ZLTextParagraph.Entry entry: paragraph.getEntries()) {
|
||||
for (ZLTextParagraph.Entry entry : paragraph) {
|
||||
if (entry instanceof ZLTextEntryImpl) {
|
||||
ZLTextEntryImpl textEntry = (ZLTextEntryImpl)entry;
|
||||
sb.append("[TEXT]");
|
||||
|
|
|
@ -3,31 +3,61 @@ package org.zlibrary.text.model.impl;
|
|||
import org.zlibrary.text.model.ZLTextParagraph;
|
||||
import org.zlibrary.text.model.impl.ZLTextEntryImpl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.*;
|
||||
|
||||
class ZLTextParagraphImpl implements ZLTextParagraph {
|
||||
private final ArrayList<ZLTextParagraph.Entry> myEntries = new ArrayList<ZLTextParagraph.Entry>();
|
||||
private final ArrayList<Entry> myEntries;
|
||||
private final int myOffset;
|
||||
private int myLength;
|
||||
|
||||
ZLTextParagraphImpl() {
|
||||
ZLTextParagraphImpl(ArrayList<Entry> entries) {
|
||||
myEntries = entries;
|
||||
myOffset = entries.size();
|
||||
myLength = 0;
|
||||
}
|
||||
|
||||
public List<ZLTextParagraph.Entry> getEntries() {
|
||||
return Collections.unmodifiableList(myEntries);
|
||||
private class EntryIterator implements Iterator<Entry> {
|
||||
private int myPosition;
|
||||
|
||||
EntryIterator() {
|
||||
myPosition = myOffset;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return myPosition < myOffset + myLength;
|
||||
}
|
||||
|
||||
public Entry next() {
|
||||
if (myPosition == myOffset + myLength) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return myEntries.get(myPosition++);
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<Entry> iterator() {
|
||||
return new EntryIterator();
|
||||
}
|
||||
|
||||
public Kind getKind() {
|
||||
return Kind.TEXT_PARAGRAPH;
|
||||
}
|
||||
|
||||
public int getEntryNumber() {
|
||||
return myEntries.size();
|
||||
public final int getEntryNumber() {
|
||||
return myLength;
|
||||
}
|
||||
|
||||
public int getTextLength() {
|
||||
public final int getTextLength() {
|
||||
int size = 0;
|
||||
for (ZLTextParagraph.Entry entry: myEntries) {
|
||||
final ArrayList<Entry> entries = myEntries;
|
||||
final int from = myOffset;
|
||||
final int to = from + myLength;
|
||||
for (int i = from; i < to; ++i) {
|
||||
Entry entry = entries.get(i);
|
||||
if (entry instanceof ZLTextEntryImpl) {
|
||||
size += ((ZLTextEntryImpl)entry).getDataLength();
|
||||
}
|
||||
|
@ -35,7 +65,8 @@ class ZLTextParagraphImpl implements ZLTextParagraph {
|
|||
return size;
|
||||
}
|
||||
|
||||
public void addEntry(ZLTextParagraph.Entry entry) {
|
||||
final void addEntry(Entry entry) {
|
||||
myEntries.add(entry);
|
||||
++myLength;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,9 @@ import org.zlibrary.text.model.ZLTextPlainModel;
|
|||
|
||||
class ZLTextPlainModelImpl extends ZLTextModelImpl implements ZLTextPlainModel {
|
||||
public void createParagraph(ZLTextParagraph.Kind kind) {
|
||||
ZLTextParagraph paragraph = (kind == ZLTextParagraph.Kind.TEXT_PARAGRAPH) ? new ZLTextParagraphImpl() : new ZLTextSpecialParagraphImpl(kind);
|
||||
ZLTextParagraph paragraph =
|
||||
(kind == ZLTextParagraph.Kind.TEXT_PARAGRAPH) ?
|
||||
new ZLTextParagraphImpl(myEntries) : new ZLTextSpecialParagraphImpl(kind, myEntries);
|
||||
addParagraphInternal(paragraph);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package org.zlibrary.text.model.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
class ZLTextSpecialParagraphImpl extends ZLTextParagraphImpl {
|
||||
private Kind myKind;
|
||||
|
||||
ZLTextSpecialParagraphImpl(Kind kind) {
|
||||
ZLTextSpecialParagraphImpl(Kind kind, ArrayList<Entry> entries) {
|
||||
super(entries);
|
||||
myKind = kind;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ public class ZLTextTreeModelImpl extends ZLTextModelImpl implements ZLTextTreeMo
|
|||
private final ZLTextTreeParagraph myRoot;
|
||||
|
||||
public ZLTextTreeModelImpl() {
|
||||
myRoot = new ZLTextTreeParagraphImpl(null);
|
||||
myRoot = new ZLTextTreeParagraphImpl(null, myEntries);
|
||||
myRoot.open(true);
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ public class ZLTextTreeModelImpl extends ZLTextModelImpl implements ZLTextTreeMo
|
|||
if (parent == null) {
|
||||
parent = myRoot;
|
||||
}
|
||||
ZLTextTreeParagraph tp = new ZLTextTreeParagraphImpl(parent);
|
||||
ZLTextTreeParagraph tp = new ZLTextTreeParagraphImpl(parent, myEntries);
|
||||
addParagraphInternal(tp);
|
||||
return tp;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,8 @@ class ZLTextTreeParagraphImpl extends ZLTextParagraphImpl implements ZLTextTreeP
|
|||
private ZLTextTreeParagraph myParent;
|
||||
private final ArrayList<ZLTextTreeParagraph> myChildren = new ArrayList<ZLTextTreeParagraph>();
|
||||
|
||||
ZLTextTreeParagraphImpl(ZLTextTreeParagraph parent) {
|
||||
ZLTextTreeParagraphImpl(ZLTextTreeParagraph parent, ArrayList<Entry> entries) {
|
||||
super(entries);
|
||||
myParent = parent;
|
||||
if (parent != null) {
|
||||
((ZLTextTreeParagraphImpl)parent).addChild(this);
|
||||
|
|
|
@ -24,8 +24,7 @@ public abstract class ZLTextParagraphCursor {
|
|||
/*Why do we need ZLTextParagraph.Entry interface?*/
|
||||
|
||||
public void fill() {
|
||||
List <ZLTextParagraph.Entry> entries = myParagraph.getEntries();
|
||||
for (ZLTextParagraph.Entry entry : entries) {
|
||||
for (ZLTextParagraph.Entry entry : myParagraph) {
|
||||
if (entry instanceof ZLTextEntry) {
|
||||
processTextEntry((ZLTextEntry) entry);
|
||||
} else if (entry instanceof ZLTextControlEntry) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue