mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
code simplification
This commit is contained in:
parent
d1f6a9eab7
commit
0128d981ee
2 changed files with 34 additions and 185 deletions
|
@ -113,12 +113,12 @@ class CustomCatalogDialog extends NetworkDialog {
|
||||||
}
|
}
|
||||||
|
|
||||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||||
if (myLink != null && library.hasCustomLinkTitle(myTitle, (ICustomNetworkLink) myLink)) {
|
if (myLink != null && library.hasCustomLinkTitle(myTitle, myLink)) {
|
||||||
final String err = myResource.getResource("titleAlreadyExists").getValue();
|
final String err = myResource.getResource("titleAlreadyExists").getValue();
|
||||||
sendError(true, false, err);
|
sendError(true, false, err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (library.hasCustomLinkSite(siteName, (ICustomNetworkLink) myLink)) {
|
if (library.hasCustomLinkSite(siteName, myLink)) {
|
||||||
final String err = myResource.getResource("siteAlreadyExists").getValue();
|
final String err = myResource.getResource("siteAlreadyExists").getValue();
|
||||||
sendError(true, false, err);
|
sendError(true, false, err);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -43,145 +43,6 @@ public class NetworkLibrary {
|
||||||
return ourInstance;
|
return ourInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CompositeList extends AbstractSequentialList<INetworkLink> {
|
|
||||||
private final ArrayList<ArrayList<? extends INetworkLink>> myLists;
|
|
||||||
private final Comparator<INetworkLink> myComparator = new LinksComparator();
|
|
||||||
|
|
||||||
public CompositeList(ArrayList<ArrayList<? extends INetworkLink>> lists) {
|
|
||||||
myLists = lists;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Iterator implements ListIterator<INetworkLink> {
|
|
||||||
private int myIndex;
|
|
||||||
private ArrayList<Integer> myPositions;
|
|
||||||
|
|
||||||
private final INetworkLink getNextByIndex(int index) {
|
|
||||||
final int position = myPositions.get(index);
|
|
||||||
return (position < myLists.get(index).size()) ?
|
|
||||||
myLists.get(index).get(position) :
|
|
||||||
null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private final INetworkLink getPrevByIndex(int index) {
|
|
||||||
final int position = myPositions.get(index);
|
|
||||||
return (position > 0) ?
|
|
||||||
myLists.get(index).get(position - 1) :
|
|
||||||
null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterator() {
|
|
||||||
myPositions = new ArrayList<Integer>(Collections.nCopies(myLists.size(), 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterator(Iterator it) {
|
|
||||||
myIndex = it.myIndex;
|
|
||||||
myPositions = new ArrayList<Integer>(it.myPositions);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasNext() {
|
|
||||||
return myIndex < size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasPrevious() {
|
|
||||||
return myIndex > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int nextIndex() {
|
|
||||||
return myIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int previousIndex() {
|
|
||||||
return myIndex - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public INetworkLink next() {
|
|
||||||
final int size = myLists.size();
|
|
||||||
if (size == 0) {
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
int nextIndex = -1;
|
|
||||||
INetworkLink nextLink = null;;
|
|
||||||
for (nextIndex = 0; nextIndex < size; ++nextIndex) {
|
|
||||||
nextLink = getNextByIndex(nextIndex);
|
|
||||||
if (nextLink != null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nextLink == null) {
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
for (int i = nextIndex + 1; i < size; ++i) {
|
|
||||||
INetworkLink link = getNextByIndex(i);
|
|
||||||
if (link != null && myComparator.compare(link, nextLink) < 0) {
|
|
||||||
nextLink = link;
|
|
||||||
nextIndex = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
myPositions.set(nextIndex, myPositions.get(nextIndex) + 1);
|
|
||||||
++myIndex;
|
|
||||||
return nextLink;
|
|
||||||
}
|
|
||||||
|
|
||||||
public INetworkLink previous() {
|
|
||||||
final int size = myLists.size();
|
|
||||||
if (size == 0) {
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
int prevIndex = -1;
|
|
||||||
INetworkLink prevLink = null;;
|
|
||||||
for (prevIndex = 0; prevIndex < size; ++prevIndex) {
|
|
||||||
prevLink = getPrevByIndex(prevIndex);
|
|
||||||
if (prevLink != null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (prevLink == null) {
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
for (int i = prevIndex + 1; i < size; ++i) {
|
|
||||||
INetworkLink link = getPrevByIndex(i);
|
|
||||||
if (link != null && myComparator.compare(link, prevLink) >= 0) {
|
|
||||||
prevLink = link;
|
|
||||||
prevIndex = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
myPositions.set(prevIndex, myPositions.get(prevIndex) - 1);
|
|
||||||
--myIndex;
|
|
||||||
return prevLink;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void add(INetworkLink arg0) { throw new UnsupportedOperationException(); }
|
|
||||||
public void remove() { throw new UnsupportedOperationException(); }
|
|
||||||
public void set(INetworkLink arg0) { throw new UnsupportedOperationException(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ListIterator<INetworkLink> listIterator(int location) {
|
|
||||||
if (location < 0 || location > size()) {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
Iterator it = new Iterator();
|
|
||||||
while (location-- > 0) {
|
|
||||||
it.next();
|
|
||||||
}
|
|
||||||
return it;
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns a copy of iterator
|
|
||||||
public ListIterator<INetworkLink> listIterator(ListIterator<INetworkLink> it) {
|
|
||||||
return new Iterator((Iterator)it);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
int size = 0;
|
|
||||||
for (ArrayList<? extends INetworkLink> list: myLists) {
|
|
||||||
size += list.size();
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class LinksComparator implements Comparator<INetworkLink> {
|
private static class LinksComparator implements Comparator<INetworkLink> {
|
||||||
private static String filterLinkTitle(String title) {
|
private static String filterLinkTitle(String title) {
|
||||||
for (int index = 0; index < title.length(); ++index) {
|
for (int index = 0; index < title.length(); ++index) {
|
||||||
|
@ -223,9 +84,7 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
public final ZLStringOption NetworkSearchPatternOption = new ZLStringOption("NetworkSearch", "Pattern", "");
|
public final ZLStringOption NetworkSearchPatternOption = new ZLStringOption("NetworkSearch", "Pattern", "");
|
||||||
|
|
||||||
private final ArrayList<INetworkLink> myLoadedLinks = new ArrayList<INetworkLink>();
|
private final ArrayList<INetworkLink> myLinks = new ArrayList<INetworkLink>();
|
||||||
private final ArrayList<ICustomNetworkLink> myCustomLinks = new ArrayList<ICustomNetworkLink>();
|
|
||||||
private final CompositeList myLinks;
|
|
||||||
|
|
||||||
private final RootTree myRootTree = new RootTree();
|
private final RootTree myRootTree = new RootTree();
|
||||||
|
|
||||||
|
@ -234,10 +93,6 @@ public class NetworkLibrary {
|
||||||
private boolean myUpdateVisibility;
|
private boolean myUpdateVisibility;
|
||||||
|
|
||||||
private NetworkLibrary() {
|
private NetworkLibrary() {
|
||||||
ArrayList<ArrayList<? extends INetworkLink>> linksList = new ArrayList<ArrayList<? extends INetworkLink>>();
|
|
||||||
linksList.add(myLoadedLinks);
|
|
||||||
linksList.add(myCustomLinks);
|
|
||||||
myLinks = new CompositeList(linksList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean myIsAlreadyInitialized;
|
private boolean myIsAlreadyInitialized;
|
||||||
|
@ -250,13 +105,11 @@ public class NetworkLibrary {
|
||||||
try {
|
try {
|
||||||
OPDSLinkReader.loadOPDSLinks(OPDSLinkReader.CACHE_LOAD, new OnNewLinkListener() {
|
OPDSLinkReader.loadOPDSLinks(OPDSLinkReader.CACHE_LOAD, new OnNewLinkListener() {
|
||||||
public void onNewLink(INetworkLink link) {
|
public void onNewLink(INetworkLink link) {
|
||||||
addLinkInternal(myLoadedLinks, link, comparator);
|
addLinkInternal(link);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (ZLNetworkException e) {
|
} catch (ZLNetworkException e) {
|
||||||
synchronized (myLinks) {
|
removeAllLoadedLinks();
|
||||||
myLoadedLinks.clear();
|
|
||||||
}
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,7 +121,7 @@ public class NetworkLibrary {
|
||||||
String title, String summary, String icon, Map<String, String> links) {
|
String title, String summary, String icon, Map<String, String> links) {
|
||||||
final ICustomNetworkLink link = OPDSLinkReader.createCustomLink(id, siteName, title, summary, icon, links);
|
final ICustomNetworkLink link = OPDSLinkReader.createCustomLink(id, siteName, title, summary, icon, links);
|
||||||
if (link != null) {
|
if (link != null) {
|
||||||
addLinkInternal(myCustomLinks, link, comparator);
|
addLinkInternal(link);
|
||||||
link.setSaveLinkListener(myChangesListener);
|
link.setSaveLinkListener(myChangesListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,6 +150,18 @@ public class NetworkLibrary {
|
||||||
myIsAlreadyInitialized = true;
|
myIsAlreadyInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void removeAllLoadedLinks() {
|
||||||
|
synchronized (myLinks) {
|
||||||
|
final LinkedList<INetworkLink> toRemove = new LinkedList<INetworkLink>();
|
||||||
|
for (INetworkLink link : myLinks) {
|
||||||
|
if (!(link instanceof ICustomNetworkLink)) {
|
||||||
|
toRemove.add(link);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
myLinks.removeAll(myLinks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*private void testDate(ATOMDateConstruct date1, ATOMDateConstruct date2) {
|
/*private void testDate(ATOMDateConstruct date1, ATOMDateConstruct date2) {
|
||||||
String sign = " == ";
|
String sign = " == ";
|
||||||
final int diff = date1.compareTo(date2);
|
final int diff = date1.compareTo(date2);
|
||||||
|
@ -330,8 +195,6 @@ public class NetworkLibrary {
|
||||||
if (myBackgroundLinks != null) {
|
if (myBackgroundLinks != null) {
|
||||||
if (myBackgroundLinks.isEmpty()) {
|
if (myBackgroundLinks.isEmpty()) {
|
||||||
myBackgroundLinks = null;
|
myBackgroundLinks = null;
|
||||||
} else {
|
|
||||||
Collections.sort(myBackgroundLinks, new LinksComparator());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -348,8 +211,8 @@ public class NetworkLibrary {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
synchronized (myLinks) {
|
synchronized (myLinks) {
|
||||||
myLoadedLinks.clear();
|
removeAllLoadedLinks();
|
||||||
myLoadedLinks.addAll(myBackgroundLinks);
|
myLinks.addAll(myBackgroundLinks);
|
||||||
updateChildren();
|
updateChildren();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -409,9 +272,9 @@ public class NetworkLibrary {
|
||||||
int nodeCount = 0;
|
int nodeCount = 0;
|
||||||
|
|
||||||
synchronized (myLinks) {
|
synchronized (myLinks) {
|
||||||
ListIterator<INetworkLink> it = myLinks.listIterator();
|
Collections.sort(myLinks, new LinksComparator());
|
||||||
while (it.hasNext()) {
|
for (int i = 0; i < myLinks.size(); ++i) {
|
||||||
INetworkLink link = it.next();
|
INetworkLink link = myLinks.get(i);
|
||||||
/*if (!link.OnOption.getValue()) {
|
/*if (!link.OnOption.getValue()) {
|
||||||
continue;
|
continue;
|
||||||
}*/
|
}*/
|
||||||
|
@ -437,9 +300,8 @@ public class NetworkLibrary {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
INetworkLink newNodeLink = null;
|
INetworkLink newNodeLink = null;
|
||||||
ListIterator<INetworkLink> jt = myLinks.listIterator(it);
|
for (int j = i; j < myLinks.size(); ++j) {
|
||||||
while (jt.hasNext()) {
|
final INetworkLink jlnk = myLinks.get(j);
|
||||||
final INetworkLink jlnk = jt.next();
|
|
||||||
if (linksEqual(nodeLink, jlnk)) {
|
if (linksEqual(nodeLink, jlnk)) {
|
||||||
newNodeLink = jlnk;
|
newNodeLink = jlnk;
|
||||||
break;
|
break;
|
||||||
|
@ -489,9 +351,6 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
public void synchronize() {
|
public void synchronize() {
|
||||||
if (myUpdateChildren || myInvalidateChildren) {
|
if (myUpdateChildren || myInvalidateChildren) {
|
||||||
if (myInvalidateChildren) {
|
|
||||||
Collections.sort(myCustomLinks, new LinksComparator());
|
|
||||||
}
|
|
||||||
myUpdateChildren = false;
|
myUpdateChildren = false;
|
||||||
myInvalidateChildren = false;
|
myInvalidateChildren = false;
|
||||||
makeUpToDate();
|
makeUpToDate();
|
||||||
|
@ -560,40 +419,30 @@ public class NetworkLibrary {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private <T extends INetworkLink> void addLinkInternal(ArrayList<T> list, T link, LinksComparator comparator) {
|
private <T extends INetworkLink> void addLinkInternal(T link) {
|
||||||
synchronized (myLinks) {
|
synchronized (myLinks) {
|
||||||
final int index = Collections.binarySearch(list, link, comparator);
|
myLinks.add(link);
|
||||||
if (index >= 0) {
|
|
||||||
return;
|
|
||||||
//throw new RuntimeException("Unable to add link with duplicated title to the library");
|
|
||||||
}
|
|
||||||
final int insertAt = -index - 1;
|
|
||||||
list.add(insertAt, link);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCustomLink(ICustomNetworkLink link) {
|
public void addCustomLink(ICustomNetworkLink link) {
|
||||||
addLinkInternal(myCustomLinks, link, new LinksComparator());
|
addLinkInternal(link);
|
||||||
link.setSaveLinkListener(myChangesListener);
|
link.setSaveLinkListener(myChangesListener);
|
||||||
link.saveLink();
|
link.saveLink();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeCustomLink(ICustomNetworkLink link) {
|
public void removeCustomLink(ICustomNetworkLink link) {
|
||||||
synchronized (myLinks) {
|
synchronized (myLinks) {
|
||||||
final int index = Collections.binarySearch(myCustomLinks, link, new LinksComparator());
|
myLinks.remove(link);
|
||||||
if (index < 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
myCustomLinks.remove(index);
|
|
||||||
}
|
}
|
||||||
NetworkDatabase.Instance().deleteCustomLink(link);
|
NetworkDatabase.Instance().deleteCustomLink(link);
|
||||||
link.setSaveLinkListener(null);
|
link.setSaveLinkListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasCustomLinkTitle(String title, ICustomNetworkLink exeptFor) {
|
public boolean hasCustomLinkTitle(String title, INetworkLink exceptFor) {
|
||||||
synchronized (myLinks) {
|
synchronized (myLinks) {
|
||||||
for (INetworkLink link: myLinks) {
|
for (INetworkLink link: myLinks) {
|
||||||
if (link != exeptFor && link.getTitle().equals(title)) {
|
if (link != exceptFor && link.getTitle().equals(title)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -601,10 +450,10 @@ public class NetworkLibrary {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasCustomLinkSite(String siteName, ICustomNetworkLink exeptFor) {
|
public boolean hasCustomLinkSite(String siteName, INetworkLink exceptFor) {
|
||||||
synchronized (myLinks) {
|
synchronized (myLinks) {
|
||||||
for (INetworkLink link: myLinks) {
|
for (INetworkLink link: myLinks) {
|
||||||
if (link != exeptFor && link.getSiteName().equals(siteName)) {
|
if (link != exceptFor && link.getSiteName().equals(siteName)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -614,7 +463,7 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
public List<String> languages() {
|
public List<String> languages() {
|
||||||
final TreeSet<String> languageSet = new TreeSet<String>();
|
final TreeSet<String> languageSet = new TreeSet<String>();
|
||||||
for (INetworkLink link : myLoadedLinks) {
|
for (INetworkLink link : myLinks) {
|
||||||
languageSet.add(link.getLanguage());
|
languageSet.add(link.getLanguage());
|
||||||
}
|
}
|
||||||
return new ArrayList(languageSet);
|
return new ArrayList(languageSet);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue