1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-06 03:50:19 +02:00

code simplification & optimization

This commit is contained in:
Nikolay Pultsin 2013-03-09 21:48:36 +04:00
parent f0ffe4b1b9
commit 8531c44dc5
10 changed files with 44 additions and 81 deletions

View file

@ -346,6 +346,17 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<String> firstTitleLetters() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.firstTitleLetters();
} catch (RemoteException e) {
return Collections.emptyList();
}
}
public synchronized List<String> titlesForAuthor(Author author, int limit) {
if (myInterface == null) {
return Collections.emptyList();

View file

@ -32,6 +32,7 @@ interface LibraryInterface {
List<String> series();
List<String> tags();
List<String> titles();
List<String> firstTitleLetters();
List<String> titlesForAuthor(in String author, int limit);
List<String> titlesForSeries(in String series, int limit);
List<String> titlesForSeriesAndAuthor(in String series, in String author, int limit);

View file

@ -222,6 +222,10 @@ public class LibraryService extends Service {
return myCollection.titles();
}
public List<String> firstTitleLetters() {
return myCollection.firstTitleLetters();
}
public List<String> titlesForAuthor(String author, int limit) {
return myCollection.titlesForAuthor(Util.stringToAuthor(author), limit);
}

View file

@ -375,6 +375,16 @@ public class BookCollection extends AbstractBookCollection {
}
}
public List<String> firstTitleLetters() {
synchronized (myBooksByFile) {
final TreeSet<String> titles = new TreeSet<String>();
for (Book book : myBooksByFile.values()) {
titles.add(book.firstTitleLetter());
}
return new ArrayList<String>(titles);
}
}
public List<String> titlesForAuthor(Author author, int limit) {
if (limit <= 0) {
return Collections.emptyList();

View file

@ -77,6 +77,7 @@ public interface IBookCollection {
List<String> series();
List<Tag> tags();
List<String> titles();
List<String> firstTitleLetters();
List<String> titlesForAuthor(Author author, int limit);
List<String> titlesForSeries(String series, int limit);
List<String> titlesForSeriesAndAuthor(String series, Author author, int limit);

View file

@ -22,10 +22,9 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.fbreader.book.*;
import org.geometerplus.fbreader.sort.TitledEntity;
import org.geometerplus.fbreader.tree.FBTree;
public class BookTree extends TitledEntityTree {
public class BookTree extends LibraryTree {
public final Book Book;
BookTree(IBookCollection collection, Book book) {
@ -73,6 +72,11 @@ public class BookTree extends TitledEntityTree {
return book != null && book.equals(Book);
}
@Override
protected String getSortKey() {
return Book.getSortKey();
}
@Override
public int compareTo(FBTree tree) {
final int cmp = super.compareTo(tree);
@ -95,9 +99,4 @@ public class BookTree extends TitledEntityTree {
}
return Book.equals(((BookTree)object).Book);
}
@Override
public TitledEntity getTitledEntity() {
return Book;
}
}

View file

@ -25,9 +25,8 @@ import java.util.List;
import org.geometerplus.zlibrary.core.util.MiscUtil;
import org.geometerplus.fbreader.book.*;
import org.geometerplus.fbreader.sort.TitledEntity;
public final class SeriesTree extends TitledEntityTree {
public final class SeriesTree extends LibraryTree {
public final Series Series;
public final Author Author;
@ -73,7 +72,7 @@ public final class SeriesTree extends TitledEntityTree {
@Override
protected String getSortKey() {
return " Series:" + super.getSortKey();
return Series.getSortKey();
}
@Override
@ -122,9 +121,4 @@ public final class SeriesTree extends TitledEntityTree {
return true;
}
}
@Override
public TitledEntity getTitledEntity() {
return Series;
}
}

View file

@ -40,24 +40,19 @@ public class TitleListTree extends FirstLevelTree {
clear();
myDoGroupByFirstLetter = false;
final TreeSet<String> letterSet = new TreeSet<String>();
final List<Book> books = Collection.books();
if (books.size() > 9) {
for (Book b : books) {
final String letter = b.firstTitleLetter();
if (letter != null) {
letterSet.add(letter);
}
}
myDoGroupByFirstLetter = books.size() > letterSet.size() * 5 / 4;
List<String> letters = null;
if (Collection.size() > 9) {
letters = Collection.firstTitleLetters();
myDoGroupByFirstLetter = Collection.size() > letters.size() * 5 / 4;
}
if (myDoGroupByFirstLetter) {
for (String letter : letterSet) {
createTitleSubTree(letter);
for (String l : letters) {
createTitleSubTree(l);
}
} else {
for (Book b : books) {
for (Book b : Collection.books()) {
createBookWithAuthorsSubTree(b);
}
}

View file

@ -1,48 +0,0 @@
/*
* Copyright (C) 2009-2013 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.IBookCollection;
import org.geometerplus.fbreader.sort.TitledEntity;
import org.geometerplus.fbreader.tree.FBTree;
public abstract class TitledEntityTree extends LibraryTree {
TitledEntityTree(IBookCollection collection) {
super(collection);
}
TitledEntityTree(LibraryTree parent) {
super(parent);
}
TitledEntityTree(LibraryTree parent, int position) {
super(parent, position);
}
protected abstract TitledEntity getTitledEntity();
@Override
public int compareTo(FBTree tree) {
if (tree instanceof TitledEntityTree) {
return getTitledEntity().compareTo(((TitledEntityTree)tree).getTitledEntity());
}
return super.compareTo(tree);
}
}

View file

@ -26,7 +26,7 @@ import java.util.Map;
import android.annotation.TargetApi;
import android.os.Build;
public abstract class TitledEntity implements Comparable<TitledEntity> {
public abstract class TitledEntity {
private String myTitle;
private String mySortKey;
@ -155,10 +155,6 @@ public abstract class TitledEntity implements Comparable<TitledEntity> {
return Normalizer.normalize(s, Normalizer.Form.NFKD);
}
public int compareTo(TitledEntity entity) {
return getSortKey().compareTo(entity.getSortKey());
}
public String firstTitleLetter() {
final String str = getSortKey();
if ("".equals(str)) {