1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +02:00

BigDecimal for series index

This commit is contained in:
Nikolay Pultsin 2012-04-15 16:34:24 +01:00
parent ad37400b9b
commit 1b31f8f7e4
16 changed files with 96 additions and 51 deletions

View file

@ -18,7 +18,7 @@ litres: author photos
* rtf: underline & strikethrough
* fb2 plugins: compare native & java
* java fb2 plugin: use Base64InputStream technique
* language codes in ZLTextHyphenator and ZLLanguageUtil
* language codes in ZLTextHyphenator and ZLLanguageUtil (java & cpp)
* resources synchronization
* in library: search more on network
* ePubs in zips

View file

@ -58,7 +58,11 @@ static void fillMetaInfo(JNIEnv* env, jobject javaBook, Book &book) {
javaString = AndroidUtil::createJavaString(env, book.seriesTitle());
if (javaString != 0) {
AndroidUtil::Method_Book_setSeriesInfo->call(javaBook, javaString, (jfloat)book.indexInSeries());
jstring indexString = AndroidUtil::createJavaString(env, book.indexInSeries());
AndroidUtil::Method_Book_setSeriesInfo->call(javaBook, javaString, indexString);
if (indexString != 0) {
env->DeleteLocalRef(indexString);
}
env->DeleteLocalRef(javaString);
}

View file

@ -110,7 +110,7 @@ void FB2MetaInfoReader::startElementHandler(int tag, const char **attributes) {
std::string seriesTitle = name;
ZLStringUtil::stripWhiteSpaces(seriesTitle);
const char *number = attributeValue(attributes, "number");
myBook.setSeries(seriesTitle, number != 0 ? atoi(number) : 0);
myBook.setSeries(seriesTitle, number != 0 ? std::string(number) : std::string());
}
}
break;

View file

@ -36,7 +36,7 @@
const std::string Book::AutoEncoding = "auto";
Book::Book(const ZLFile &file, int id) : myBookId(id), myFile(file), myIndexInSeries(0) {
Book::Book(const ZLFile &file, int id) : myBookId(id), myFile(file) {
}
Book::~Book() {
@ -273,7 +273,7 @@ void Book::setEncoding(const std::string &encoding) {
myEncoding = encoding;
}
void Book::setSeries(const std::string &title, int index) {
void Book::setSeries(const std::string &title, const std::string &index) {
mySeriesTitle = title;
myIndexInSeries = index;
}

View file

@ -66,7 +66,7 @@ public: // unmodifiable book methods
const std::string &language() const;
const std::string &encoding() const;
const std::string &seriesTitle() const;
int indexInSeries() const;
const std::string &indexInSeries() const;
const TagList &tags() const;
const AuthorList &authors() const;
@ -75,7 +75,7 @@ public: // modifiable book methods
void setTitle(const std::string &title);
void setLanguage(const std::string &language);
void setEncoding(const std::string &encoding);
void setSeries(const std::string &title, int index);
void setSeries(const std::string &title, const std::string &index);
public:
bool addTag(shared_ptr<Tag> tag);
@ -102,7 +102,7 @@ private:
std::string myLanguage;
std::string myEncoding;
std::string mySeriesTitle;
int myIndexInSeries;
std::string myIndexInSeries;
TagList myTags;
AuthorList myAuthors;
@ -134,7 +134,7 @@ inline const ZLFile &Book::file() const { return myFile; }
inline const std::string &Book::language() const { return myLanguage; }
inline const std::string &Book::encoding() const { return myEncoding; }
inline const std::string &Book::seriesTitle() const { return mySeriesTitle; }
inline int Book::indexInSeries() const { return myIndexInSeries; }
inline const std::string &Book::indexInSeries() const { return myIndexInSeries; }
inline const TagList &Book::tags() const { return myTags; }
inline const AuthorList &Book::authors() const { return myAuthors; }

View file

@ -30,7 +30,7 @@ bool BookComparator::operator() (
int comp = seriesTitle0.compare(seriesTitle1);
if (comp == 0) {
if (!seriesTitle0.empty()) {
comp = book0->indexInSeries() - book1->indexInSeries();
comp = book0->indexInSeries().compare(book1->indexInSeries());
if (comp != 0) {
return comp < 0;
}

View file

@ -174,7 +174,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
Method_Book_getLanguage = new StringMethod(Class_Book, "getLanguage", "()");
Method_Book_getEncodingNoDetection = new StringMethod(Class_Book, "getEncodingNoDetection", "()");
Method_Book_setTitle = new VoidMethod(Class_Book, "setTitle", "(Ljava/lang/String;)");
Method_Book_setSeriesInfo = new VoidMethod(Class_Book, "setSeriesInfo", "(Ljava/lang/String;F)");
Method_Book_setSeriesInfo = new VoidMethod(Class_Book, "setSeriesInfo", "(Ljava/lang/String;Ljava/lang/String;)");
Method_Book_setLanguage = new VoidMethod(Class_Book, "setLanguage", "(Ljava/lang/String;)");
Method_Book_setEncoding = new VoidMethod(Class_Book, "setEncoding", "(Ljava/lang/String;)");
Method_Book_addAuthor = new VoidMethod(Class_Book, "addAuthor", "(Ljava/lang/String;Ljava/lang/String;)");

View file

@ -72,7 +72,7 @@
public ** getLanguage();
public ** getEncodingNoDetection();
public void setTitle(**);
public void setSeriesInfo(**,float);
public void setSeriesInfo(**,**);
public void setLanguage(**);
public void setEncoding(**);
public void addAuthor(**,**);

View file

@ -235,12 +235,8 @@ public class BookInfoActivity extends Activity {
final SeriesInfo series = book.getSeriesInfo();
setupInfoPair(R.id.book_series, "series", series == null ? null : series.Name);
String seriesIndexString = null;
if (series != null && series.Index > 0) {
if (Math.abs(series.Index - Math.round(series.Index)) < 0.01) {
seriesIndexString = String.valueOf(Math.round(series.Index));
} else {
seriesIndexString = String.format("%.1f", series.Index);
}
if (series != null && series.Index != null) {
seriesIndexString = series.Index.toString();
}
setupInfoPair(R.id.book_series_index, "indexInSeries", seriesIndexString);

View file

@ -20,6 +20,7 @@
package org.geometerplus.android.fbreader.library;
import java.util.*;
import java.math.BigDecimal;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
@ -70,7 +71,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
private void migrate(Context context) {
final int version = myDatabase.getVersion();
final int currentVersion = 18;
final int currentVersion = 19;
if (version >= currentVersion) {
return;
}
@ -115,6 +116,8 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
updateTables16();
case 17:
updateTables17();
case 18:
updateTables18();
}
myDatabase.setTransactionSuccessful();
myDatabase.endTransaction();
@ -255,9 +258,9 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
while (cursor.moveToNext()) {
Book book = booksById.get(cursor.getLong(0));
if (book != null) {
String series = seriesById.get(cursor.getLong(1));
final String series = seriesById.get(cursor.getLong(1));
if (series != null) {
setSeriesInfo(book, series, cursor.getFloat(2));
setSeriesInfo(book, series, cursor.getString(2));
}
}
}
@ -500,7 +503,10 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
}
myInsertBookSeriesStatement.bindLong(1, bookId);
myInsertBookSeriesStatement.bindLong(2, seriesId);
myInsertBookSeriesStatement.bindDouble(3, seriesInfo.Index);
SQLiteUtil.bindString(
myInsertBookSeriesStatement, 3,
seriesInfo.Index != null ? seriesInfo.Index.toString() : null
);
myInsertBookSeriesStatement.execute();
}
}
@ -509,7 +515,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
final Cursor cursor = myDatabase.rawQuery("SELECT Series.name,BookSeries.book_index FROM BookSeries INNER JOIN Series ON Series.series_id = BookSeries.series_id WHERE BookSeries.book_id = ?", new String[] { "" + bookId });
SeriesInfo info = null;
if (cursor.moveToNext()) {
info = new SeriesInfo(cursor.getString(0), cursor.getFloat(1));
info = new SeriesInfo(cursor.getString(0), SeriesInfo.createIndex(cursor.getString(1)));
}
cursor.close();
return info;
@ -1236,4 +1242,37 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
"pages_full INTEGER NOT NULL," +
"page_current INTEGER NOT NULL)");
}
private void updateTables18() {
myDatabase.execSQL("ALTER TABLE BookSeries RENAME TO BookSeries_Obsolete");
myDatabase.execSQL(
"CREATE TABLE BookSeries(" +
"series_id INTEGER NOT NULL REFERENCES Series(series_id)," +
"book_id INTEGER NOT NULL UNIQUE REFERENCES Books(book_id)," +
"book_index TEXT)");
final SQLiteStatement insert = myDatabase.compileStatement(
"INSERT INTO BookSeries (series_id,book_id,book_index) VALUES (?,?,?)"
);
final Cursor cursor = myDatabase.rawQuery("SELECT series_id,book_id,book_index FROM BookSeries_Obsolete", null);
while (cursor.moveToNext()) {
insert.bindLong(1, cursor.getLong(0));
insert.bindLong(2, cursor.getLong(1));
final float index = cursor.getFloat(2);
final String stringIndex;
if (index == 0.0f) {
stringIndex = null;
} else {
if (Math.abs(index - Math.round(index)) < 0.01) {
stringIndex = String.valueOf(Math.round(index));
} else {
stringIndex = String.format("%.1f", index);
}
}
final BigDecimal bdIndex = SeriesInfo.createIndex(stringIndex);
SQLiteUtil.bindString(insert, 3, bdIndex != null ? bdIndex.toString() : null);
insert.executeInsert();
}
cursor.close();
myDatabase.execSQL("DROP TABLE BookSeries_Obsolete");
}
}

View file

@ -117,15 +117,7 @@ public class FB2MetaInfoReader extends ZLXMLReaderAdapter {
if (name != null) {
name.trim();
if (name.length() != 0) {
int index = 0;
try {
final String sIndex = attributes.getValue("number");
if (sIndex != null) {
index = Integer.parseInt(sIndex);
}
} catch (NumberFormatException e) {
}
myBook.setSeriesInfo(name, index);
myBook.setSeriesInfo(name, attributes.getValue("number"));
}
}
}

View file

@ -33,7 +33,7 @@ class OEBMetaInfoReader extends ZLXMLReaderAdapter implements XMLNamespaces {
private final Book myBook;
private String mySeriesTitle = "";
private float mySeriesIndex = 0;
private String mySeriesIndex = null;
private final ArrayList<String> myAuthorList = new ArrayList<String>();
private final ArrayList<String> myAuthorList2 = new ArrayList<String>();
@ -47,7 +47,7 @@ class OEBMetaInfoReader extends ZLXMLReaderAdapter implements XMLNamespaces {
void readMetaInfo(ZLFile file) throws BookReadingException {
myReadState = ReadState.Nothing;
mySeriesTitle = "";
mySeriesIndex = 0;
mySeriesIndex = null;
try {
ZLXMLProcessor.read(this, file, 512);
@ -122,11 +122,7 @@ class OEBMetaInfoReader extends ZLXMLReaderAdapter implements XMLNamespaces {
if (attributes.getValue("name").equals("calibre:series")) {
mySeriesTitle = attributes.getValue("content");
} else if (attributes.getValue("name").equals("calibre:series_index")) {
final String strIndex = attributes.getValue("content");
try {
mySeriesIndex = Float.parseFloat(strIndex);
} catch (NumberFormatException e) {
}
mySeriesIndex = attributes.getValue("content");
}
}
break;

View file

@ -20,6 +20,7 @@
package org.geometerplus.fbreader.library;
import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
@ -276,11 +277,15 @@ public class Book {
return mySeriesInfo;
}
void setSeriesInfoWithNoCheck(String name, float index) {
void setSeriesInfoWithNoCheck(String name, BigDecimal index) {
mySeriesInfo = new SeriesInfo(name, index);
}
public void setSeriesInfo(String name, float index) {
public void setSeriesInfo(String name, String index) {
setSeriesInfo(name, SeriesInfo.createIndex(index));
}
public void setSeriesInfo(String name, BigDecimal index) {
if (mySeriesInfo == null) {
if (name != null) {
mySeriesInfo = new SeriesInfo(name, index);

View file

@ -19,6 +19,8 @@
package org.geometerplus.fbreader.library;
import java.math.BigDecimal;
import org.geometerplus.fbreader.tree.FBTree;
public final class BookInSeriesTree extends BookTree {
@ -33,11 +35,12 @@ public final class BookInSeriesTree extends BookTree {
@Override
public int compareTo(FBTree tree) {
if (tree instanceof BookInSeriesTree) {
final float difference =
Book.getSeriesInfo().Index - ((BookTree)tree).Book.getSeriesInfo().Index;
if (difference != 0) {
return difference > 0 ? 1 : -1;
final BigDecimal index0 = Book.getSeriesInfo().Index;
final BigDecimal index1 = ((BookTree)tree).Book.getSeriesInfo().Index;
if (index0 == null) {
return index1 == null ? 0 : 1;
}
return index1 == null ? -1 : index0.compareTo(index1);
}
return super.compareTo(tree);
}

View file

@ -49,8 +49,8 @@ public abstract class BooksDatabase {
protected void addTag(Book book, Tag tag) {
book.addTagWithNoCheck(tag);
}
protected void setSeriesInfo(Book book, String series, float index) {
book.setSeriesInfoWithNoCheck(series, index);
protected void setSeriesInfo(Book book, String series, String index) {
book.setSeriesInfoWithNoCheck(series, SeriesInfo.createIndex(index));
}
protected abstract void executeAsATransaction(Runnable actions);

View file

@ -19,11 +19,21 @@
package org.geometerplus.fbreader.library;
public final class SeriesInfo {
public final String Name;
public final float Index;
import java.math.BigDecimal;
public SeriesInfo(String name, float index) {
public final class SeriesInfo {
public static BigDecimal createIndex(String index) {
try {
return index != null ? new BigDecimal(index).stripTrailingZeros() : null;
} catch (NumberFormatException e) {
return null;
}
}
public final String Name;
public final BigDecimal Index;
public SeriesInfo(String name, BigDecimal index) {
Name = name;
Index = index;
}