1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00

ZLTextModel refactoring: no interfaces for entries, final public fields instead of getters (in progress)

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@393 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Nikolay Pultsin 2007-12-13 10:19:39 +00:00
parent 64df86aa98
commit 731f7eee31
19 changed files with 78 additions and 110 deletions

View file

@ -2,9 +2,10 @@ package org.zlibrary.text.model;
import java.util.List; import java.util.List;
import org.zlibrary.text.model.entry.ZLTextParagraphEntry;
public interface ZLTextParagraph { public interface ZLTextParagraph {
interface Entry {
}
enum Kind { enum Kind {
TEXT_PARAGRAPH, TEXT_PARAGRAPH,
TREE_PARAGRAPH, TREE_PARAGRAPH,
@ -18,6 +19,6 @@ public interface ZLTextParagraph {
Kind getKind(); Kind getKind();
int getEntryNumber(); int getEntryNumber();
int getTextLength(); int getTextLength();
List<ZLTextParagraphEntry> getEntries(); List<Entry> getEntries();
void addEntry(ZLTextParagraphEntry entry); void addEntry(Entry entry);
} }

View file

@ -1,8 +1,9 @@
package org.zlibrary.text.model.entry; package org.zlibrary.text.model.entry;
import org.zlibrary.core.image.ZLImage; import org.zlibrary.core.image.ZLImage;
import org.zlibrary.text.model.ZLTextParagraph;
public interface ZLImageEntry extends ZLTextParagraphEntry { public interface ZLImageEntry extends ZLTextParagraph.Entry {
String getId(); String getId();
short getVOffset(); short getVOffset();
ZLImage getImage(); ZLImage getImage();

View file

@ -1,7 +0,0 @@
package org.zlibrary.text.model.entry;
public interface ZLTextControlEntry extends ZLTextParagraphEntry {
byte getKind();
boolean isStart();
boolean isHyperlink();
}

View file

@ -1,6 +1,8 @@
package org.zlibrary.text.model.entry; package org.zlibrary.text.model.entry;
public interface ZLTextEntry extends ZLTextParagraphEntry { import org.zlibrary.text.model.ZLTextParagraph;
public interface ZLTextEntry extends ZLTextParagraph.Entry {
char[] getData(); char[] getData();
int getDataOffset(); int getDataOffset();
int getDataLength(); int getDataLength();

View file

@ -1,5 +1,7 @@
package org.zlibrary.text.model.entry; package org.zlibrary.text.model.entry;
public interface ZLTextFixedHSpaceEntry extends ZLTextParagraphEntry { import org.zlibrary.text.model.ZLTextParagraph;
public interface ZLTextFixedHSpaceEntry extends ZLTextParagraph.Entry {
byte length(); byte length();
} }

View file

@ -1,6 +1,8 @@
package org.zlibrary.text.model.entry; package org.zlibrary.text.model.entry;
public interface ZLTextForcedControlEntry extends ZLTextParagraphEntry{ import org.zlibrary.text.model.ZLTextParagraph;
public interface ZLTextForcedControlEntry extends ZLTextParagraph.Entry {
boolean isLeftIndentSupported(); boolean isLeftIndentSupported();
short getLeftIndent(); short getLeftIndent();
void setLeftIndent(short leftIndent); void setLeftIndent(short leftIndent);

View file

@ -1,5 +0,0 @@
package org.zlibrary.text.model.entry;
public interface ZLTextHyperlinkControlEntry extends ZLTextControlEntry {
String getLabel();
}

View file

@ -1,4 +0,0 @@
package org.zlibrary.text.model.entry;
public interface ZLTextParagraphEntry {
}

View file

@ -1,16 +1,14 @@
package org.zlibrary.text.model.impl; package org.zlibrary.text.model.impl;
import org.zlibrary.text.model.entry.ZLTextParagraphEntry;
class EntryPool { class EntryPool {
private static final ZLTextParagraphEntry[] ourStartEntries = new ZLTextParagraphEntry[256]; private static final ZLTextControlEntry[] ourStartEntries = new ZLTextControlEntry[256];
private static final ZLTextParagraphEntry[] ourEndEntries = new ZLTextParagraphEntry[256]; private static final ZLTextControlEntry[] ourEndEntries = new ZLTextControlEntry[256];
public static ZLTextParagraphEntry getControlEntry(byte kind, boolean isStart) { public static ZLTextControlEntry getControlEntry(byte kind, boolean isStart) {
ZLTextParagraphEntry[] entries = isStart ? ourStartEntries : ourEndEntries; ZLTextControlEntry[] entries = isStart ? ourStartEntries : ourEndEntries;
ZLTextParagraphEntry entry = entries[kind & 0xFF]; ZLTextControlEntry entry = entries[kind & 0xFF];
if (entry == null) { if (entry == null) {
entry = new ZLTextControlEntryImpl(kind, isStart); entry = new ZLTextControlEntry(kind, isStart);
entries[kind & 0xFF] = entry; entries[kind & 0xFF] = entry;
} }
return entry; return entry;

View file

@ -5,11 +5,9 @@ import org.zlibrary.text.model.ZLTextParagraph;
import org.zlibrary.text.model.ZLTextPlainModel; import org.zlibrary.text.model.ZLTextPlainModel;
import org.zlibrary.text.model.ZLTextTreeModel; import org.zlibrary.text.model.ZLTextTreeModel;
import org.zlibrary.text.model.ZLTextTreeParagraph; import org.zlibrary.text.model.ZLTextTreeParagraph;
import org.zlibrary.text.model.entry.ZLTextControlEntry;
import org.zlibrary.text.model.entry.ZLTextEntry; import org.zlibrary.text.model.entry.ZLTextEntry;
import org.zlibrary.text.model.entry.ZLTextFixedHSpaceEntry; import org.zlibrary.text.model.entry.ZLTextFixedHSpaceEntry;
import org.zlibrary.text.model.entry.ZLTextForcedControlEntry; import org.zlibrary.text.model.entry.ZLTextForcedControlEntry;
import org.zlibrary.text.model.entry.ZLTextHyperlinkControlEntry;
public class ZLModelFactory { public class ZLModelFactory {
@ -40,7 +38,7 @@ public class ZLModelFactory {
//entries //entries
public ZLTextControlEntry createControlEntry(byte kind, boolean isStart) { public ZLTextControlEntry createControlEntry(byte kind, boolean isStart) {
return new ZLTextControlEntryImpl(kind, isStart); return new ZLTextControlEntry(kind, isStart);
} }
public ZLTextEntry createTextEntry(String text) { public ZLTextEntry createTextEntry(String text) {
@ -49,7 +47,7 @@ public class ZLModelFactory {
} }
public ZLTextHyperlinkControlEntry createHyperlinkControlEntry(byte kind, String label) { public ZLTextHyperlinkControlEntry createHyperlinkControlEntry(byte kind, String label) {
return new ZLTextHyperlinkControlEntryImpl(kind, label); return new ZLTextHyperlinkControlEntry(kind, label);
} }
public ZLTextFixedHSpaceEntry createFixedHSpaceEntry(byte lenght) { public ZLTextFixedHSpaceEntry createFixedHSpaceEntry(byte lenght) {

View file

@ -0,0 +1,17 @@
package org.zlibrary.text.model.impl;
import org.zlibrary.text.model.ZLTextParagraph;
public class ZLTextControlEntry implements ZLTextParagraph.Entry {
public final byte Kind;
public final boolean IsStart;
ZLTextControlEntry(byte kind, boolean isStart) {
Kind = kind;
IsStart = isStart;
}
public boolean isHyperlink() {
return false;
}
}

View file

@ -1,25 +0,0 @@
package org.zlibrary.text.model.impl;
import org.zlibrary.text.model.entry.ZLTextControlEntry;
class ZLTextControlEntryImpl implements ZLTextControlEntry {
private final byte myKind;
private final boolean myIsStart;
ZLTextControlEntryImpl(byte kind, boolean isStart) {
myKind = kind;
myIsStart = isStart;
}
public final byte getKind() {
return myKind;
}
public final boolean isStart() {
return myIsStart;
}
public boolean isHyperlink() {
return false;
}
}

View file

@ -0,0 +1,14 @@
package org.zlibrary.text.model.impl;
public final class ZLTextHyperlinkControlEntry extends ZLTextControlEntry {
private final String Label;
ZLTextHyperlinkControlEntry(byte kind, String label) {
super(kind, true);
Label = label;
}
public boolean isHyperlink() {
return true;
}
}

View file

@ -1,20 +0,0 @@
package org.zlibrary.text.model.impl;
import org.zlibrary.text.model.entry.ZLTextHyperlinkControlEntry;
class ZLTextHyperlinkControlEntryImpl extends ZLTextControlEntryImpl implements ZLTextHyperlinkControlEntry {
private final String myLabel;
ZLTextHyperlinkControlEntryImpl(byte kind, String label) {
super(kind, true);
myLabel = label;
}
public final String getLabel() {
return myLabel;
}
public boolean isHyperlink() {
return true;
}
}

View file

@ -5,7 +5,6 @@ import org.zlibrary.core.image.ZLImage;
import org.zlibrary.text.model.ZLTextModel; import org.zlibrary.text.model.ZLTextModel;
import org.zlibrary.text.model.ZLTextParagraph; import org.zlibrary.text.model.ZLTextParagraph;
import org.zlibrary.text.model.entry.ZLTextForcedControlEntry; import org.zlibrary.text.model.entry.ZLTextForcedControlEntry;
import org.zlibrary.text.model.entry.ZLTextParagraphEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
@ -81,7 +80,7 @@ abstract class ZLTextModelImpl implements ZLTextModel {
} }
public void addHyperlinkControl(byte textKind, String label) { public void addHyperlinkControl(byte textKind, String label) {
getLastParagraph().addEntry(new ZLTextHyperlinkControlEntryImpl(textKind, label)); getLastParagraph().addEntry(new ZLTextHyperlinkControlEntry(textKind, label));
} }
public void addImage(String id, Map<String, ZLImage> imageMap, short vOffset) { public void addImage(String id, Map<String, ZLImage> imageMap, short vOffset) {
@ -96,18 +95,18 @@ abstract class ZLTextModelImpl implements ZLTextModel {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (ZLTextParagraph paragraph: myParagraphs) { for (ZLTextParagraph paragraph: myParagraphs) {
sb.append("[PARAGRAPH]\n"); sb.append("[PARAGRAPH]\n");
for (ZLTextParagraphEntry entry: paragraph.getEntries()) { for (ZLTextParagraph.Entry entry: paragraph.getEntries()) {
if (entry instanceof ZLTextEntryImpl) { if (entry instanceof ZLTextEntryImpl) {
ZLTextEntryImpl textEntry = (ZLTextEntryImpl)entry; ZLTextEntryImpl textEntry = (ZLTextEntryImpl)entry;
sb.append("[TEXT]"); sb.append("[TEXT]");
sb.append(textEntry.getData(), textEntry.getDataOffset(), textEntry.getDataLength()); sb.append(textEntry.getData(), textEntry.getDataOffset(), textEntry.getDataLength());
sb.append("[/TEXT]"); sb.append("[/TEXT]");
} else if (entry instanceof ZLTextControlEntryImpl) { } else if (entry instanceof ZLTextControlEntry) {
ZLTextControlEntryImpl entryControl = (ZLTextControlEntryImpl)entry; ZLTextControlEntry controlEntry = (ZLTextControlEntry)entry;
if (entryControl.isStart()) if (controlEntry.IsStart)
sb.append("[CONTROL "+entryControl.getKind()+"]"); sb.append("[CONTROL "+controlEntry.Kind+"]");
else else
sb.append("[/CONTROL "+entryControl.getKind()+"]"); sb.append("[/CONTROL "+controlEntry.Kind+"]");
} }
} }
sb.append("[/PARAGRAPH]\n"); sb.append("[/PARAGRAPH]\n");

View file

@ -1,7 +1,6 @@
package org.zlibrary.text.model.impl; package org.zlibrary.text.model.impl;
import org.zlibrary.text.model.ZLTextParagraph; import org.zlibrary.text.model.ZLTextParagraph;
import org.zlibrary.text.model.entry.ZLTextParagraphEntry;
import org.zlibrary.text.model.impl.ZLTextEntryImpl; import org.zlibrary.text.model.impl.ZLTextEntryImpl;
import java.util.List; import java.util.List;
@ -9,12 +8,12 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
class ZLTextParagraphImpl implements ZLTextParagraph { class ZLTextParagraphImpl implements ZLTextParagraph {
private final ArrayList<ZLTextParagraphEntry> myEntries = new ArrayList<ZLTextParagraphEntry>(); private final ArrayList<ZLTextParagraph.Entry> myEntries = new ArrayList<ZLTextParagraph.Entry>();
ZLTextParagraphImpl() { ZLTextParagraphImpl() {
} }
public List<ZLTextParagraphEntry> getEntries() { public List<ZLTextParagraph.Entry> getEntries() {
return Collections.unmodifiableList(myEntries); return Collections.unmodifiableList(myEntries);
} }
@ -28,7 +27,7 @@ class ZLTextParagraphImpl implements ZLTextParagraph {
public int getTextLength() { public int getTextLength() {
int size = 0; int size = 0;
for (ZLTextParagraphEntry entry: myEntries) { for (ZLTextParagraph.Entry entry: myEntries) {
if (entry instanceof ZLTextEntryImpl) { if (entry instanceof ZLTextEntryImpl) {
size += ((ZLTextEntryImpl)entry).getDataLength(); size += ((ZLTextEntryImpl)entry).getDataLength();
} }
@ -36,7 +35,7 @@ class ZLTextParagraphImpl implements ZLTextParagraph {
return size; return size;
} }
public void addEntry(ZLTextParagraphEntry entry) { public void addEntry(ZLTextParagraph.Entry entry) {
myEntries.add(entry); myEntries.add(entry);
} }
} }

View file

@ -1,28 +1,24 @@
package org.zlibrary.text.view.impl; package org.zlibrary.text.view.impl;
import org.zlibrary.text.model.entry.ZLTextControlEntry; import org.zlibrary.text.model.impl.ZLTextControlEntry;
final class ZLTextControlElement extends ZLTextElement { final class ZLTextControlElement extends ZLTextElement {
private final ZLTextControlEntry myEntry; private final ZLTextControlEntry myEntry;
/*package*/ ZLTextControlElement(ZLTextControlEntry entry) { ZLTextControlElement(ZLTextControlEntry entry) {
// System.out.println(entry.getKind() + " " + entry.isStart()); // System.out.println(entry.getKind() + " " + entry.isStart());
myEntry = entry; myEntry = entry;
} }
/* public Kind getKind() {
return Kind.CONTROL_ELEMENT;
}*/
public ZLTextControlEntry getEntry() { public ZLTextControlEntry getEntry() {
return myEntry; return myEntry;
} }
public byte getTextKind() { public byte getTextKind() {
return getEntry().getKind(); return myEntry.Kind;
} }
public boolean isStart() { public boolean isStart() {
return myEntry.isStart(); return myEntry.IsStart;
} }
} }

View file

@ -3,6 +3,7 @@ package org.zlibrary.text.view.impl;
import org.zlibrary.text.model.ZLTextModel; import org.zlibrary.text.model.ZLTextModel;
import org.zlibrary.text.model.ZLTextParagraph; import org.zlibrary.text.model.ZLTextParagraph;
import org.zlibrary.text.model.entry.*; import org.zlibrary.text.model.entry.*;
import org.zlibrary.text.model.impl.*;
import org.zlibrary.core.image.ZLImage; import org.zlibrary.core.image.ZLImage;
@ -21,11 +22,11 @@ public abstract class ZLTextParagraphCursor {
//myOffset = 0; //myOffset = 0;
} }
/*Why do we need ZLTextParagraphEntry interface?*/ /*Why do we need ZLTextParagraph.Entry interface?*/
public void fill() { public void fill() {
List <ZLTextParagraphEntry> entries = myParagraph.getEntries(); List <ZLTextParagraph.Entry> entries = myParagraph.getEntries();
for (ZLTextParagraphEntry entry : entries) { for (ZLTextParagraph.Entry entry : entries) {
if (entry instanceof ZLTextEntry) { if (entry instanceof ZLTextEntry) {
processTextEntry((ZLTextEntry) entry); processTextEntry((ZLTextEntry) entry);
} else if (entry instanceof ZLTextControlEntry) { } else if (entry instanceof ZLTextControlEntry) {

View file

@ -1,7 +1,6 @@
package org.test.zlibrary.model; package org.test.zlibrary.model;
import org.zlibrary.text.model.entry.ZLTextControlEntry; import org.zlibrary.text.model.impl.ZLTextControlEntry;
import org.zlibrary.text.model.entry.ZLTextParagraphEntry;
import org.zlibrary.text.model.impl.ZLModelFactory; import org.zlibrary.text.model.impl.ZLModelFactory;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -13,8 +12,8 @@ public class TestTextControlEntry extends TestCase {
boolean start = true; boolean start = true;
byte kind = (byte)0; byte kind = (byte)0;
ZLTextControlEntry entry = factory.createControlEntry(kind, start); ZLTextControlEntry entry = factory.createControlEntry(kind, start);
assertEquals(entry.isHyperlink(), false); //assertEquals(entry.isHyperlink(), false);
assertEquals(entry.getKind(), kind); assertEquals(entry.Kind, kind);
assertEquals(entry.isStart(), start); assertEquals(entry.IsStart, start);
} }
} }