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

code simplification

This commit is contained in:
Nikolay Pultsin 2011-04-17 00:18:54 +01:00
parent 1c0251070f
commit 9299d4c222

View file

@ -83,16 +83,18 @@ public class NetworkLibrary {
void onNewLink(INetworkLink link); void onNewLink(INetworkLink link);
} }
public final ZLStringOption NetworkSearchPatternOption = new ZLStringOption("NetworkSearch", "Pattern", ""); public final ZLStringOption NetworkSearchPatternOption =
new ZLStringOption("NetworkSearch", "Pattern", "");
private final ArrayList<INetworkLink> myLinks = new ArrayList<INetworkLink>(); // that's important to keep this list synchronized
// it can be used from background thread
private final List<INetworkLink> myLinks =
Collections.synchronizedList(new ArrayList<INetworkLink>());
public List<String> languageCodes() { public List<String> languageCodes() {
final TreeSet<String> languageSet = new TreeSet<String>(); final TreeSet<String> languageSet = new TreeSet<String>();
synchronized (myLinks) { for (INetworkLink link : myLinks) {
for (INetworkLink link : myLinks) { languageSet.add(link.getLanguage());
languageSet.add(link.getLanguage());
}
} }
return new ArrayList<String>(languageSet); return new ArrayList<String>(languageSet);
} }
@ -140,12 +142,10 @@ public class NetworkLibrary {
private List<INetworkLink> activeLinks() { private List<INetworkLink> activeLinks() {
final LinkedList<INetworkLink> filteredList = new LinkedList<INetworkLink>(); final LinkedList<INetworkLink> filteredList = new LinkedList<INetworkLink>();
final Collection<String> codes = activeLanguageCodes(); final Collection<String> codes = activeLanguageCodes();
synchronized (myLinks) { for (INetworkLink link : myLinks) {
for (INetworkLink link : myLinks) { if (link instanceof ICustomNetworkLink ||
if (link instanceof ICustomNetworkLink || codes.contains(link.getLanguage())) {
codes.contains(link.getLanguage())) { filteredList.add(link);
filteredList.add(link);
}
} }
} }
return filteredList; return filteredList;
@ -169,7 +169,7 @@ 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(link); myLinks.add(link);
} }
}); });
} catch (ZLNetworkException e) { } catch (ZLNetworkException e) {
@ -189,7 +189,7 @@ public class NetworkLibrary {
final ICustomNetworkLink link = new OPDSCustomLink( final ICustomNetworkLink link = new OPDSCustomLink(
id, siteName, title, summary, infos id, siteName, title, summary, infos
); );
addLinkInternal(link); myLinks.add(link);
} }
} }
} }
@ -200,15 +200,13 @@ public class NetworkLibrary {
} }
private void removeAllLoadedLinks() { private void removeAllLoadedLinks() {
synchronized (myLinks) { final LinkedList<INetworkLink> toRemove = new LinkedList<INetworkLink>();
final LinkedList<INetworkLink> toRemove = new LinkedList<INetworkLink>(); for (INetworkLink link : myLinks) {
for (INetworkLink link : myLinks) { if (!(link instanceof ICustomNetworkLink)) {
if (!(link instanceof ICustomNetworkLink)) { toRemove.add(link);
toRemove.add(link);
}
} }
myLinks.removeAll(toRemove);
} }
myLinks.removeAll(toRemove);
} }
/*private void testDate(ATOMDateConstruct date1, ATOMDateConstruct date2) { /*private void testDate(ATOMDateConstruct date1, ATOMDateConstruct date2) {
@ -247,10 +245,8 @@ public class NetworkLibrary {
} }
} }
} }
final List<INetworkLink> linksCopy; // we create this copy to prevent long operations on synchronized list
synchronized (myLinks) { final List<INetworkLink> linksCopy = new ArrayList<INetworkLink>(myLinks);
linksCopy = new ArrayList<INetworkLink>(myLinks);
}
for (INetworkLink link : linksCopy) { for (INetworkLink link : linksCopy) {
if (link instanceof ICustomNetworkLink) { if (link instanceof ICustomNetworkLink) {
final ICustomNetworkLink customLink = (ICustomNetworkLink)link; final ICustomNetworkLink customLink = (ICustomNetworkLink)link;
@ -269,24 +265,20 @@ public class NetworkLibrary {
// synchronize() method MUST be called after this method // synchronize() method MUST be called after this method
public void finishBackgroundUpdate() { public void finishBackgroundUpdate() {
synchronized (myBackgroundLock) { synchronized (myBackgroundLock) {
synchronized (myLinks) { if (myBackgroundLinks != null) {
if (myBackgroundLinks != null) { removeAllLoadedLinks();
removeAllLoadedLinks(); myLinks.addAll(myBackgroundLinks);
myLinks.addAll(myBackgroundLinks);
}
invalidateChildren();
} }
invalidateChildren();
} }
} }
public String rewriteUrl(String url, boolean externalUrl) { public String rewriteUrl(String url, boolean externalUrl) {
final String host = ZLNetworkUtil.hostFromUrl(url).toLowerCase(); final String host = ZLNetworkUtil.hostFromUrl(url).toLowerCase();
synchronized (myLinks) { for (INetworkLink link : myLinks) {
for (INetworkLink link : myLinks) { if (host.contains(link.getSiteName())) {
if (host.contains(link.getSiteName())) { url = link.rewriteUrl(url, externalUrl);
url = link.rewriteUrl(url, externalUrl);
}
} }
} }
return url; return url;
@ -319,56 +311,54 @@ public class NetworkLibrary {
FBTree currentNode = null; FBTree currentNode = null;
int nodeCount = 0; int nodeCount = 0;
synchronized (myLinks) { final ArrayList<INetworkLink> links = new ArrayList<INetworkLink>(activeLinks());
final ArrayList<INetworkLink> links = new ArrayList<INetworkLink>(activeLinks()); Collections.sort(links, new LinksComparator());
Collections.sort(links, new LinksComparator()); for (int i = 0; i < links.size(); ++i) {
for (int i = 0; i < links.size(); ++i) { INetworkLink link = links.get(i);
INetworkLink link = links.get(i); boolean processed = false;
boolean processed = false; while (currentNode != null || nodeIterator.hasNext()) {
while (currentNode != null || nodeIterator.hasNext()) { if (currentNode == null) {
if (currentNode == null) { currentNode = nodeIterator.next();
currentNode = nodeIterator.next(); }
} if (!(currentNode instanceof NetworkCatalogTree)) {
if (!(currentNode instanceof NetworkCatalogTree)) { toRemove.add(currentNode);
currentNode = null;
++nodeCount;
continue;
}
final INetworkLink nodeLink = ((NetworkCatalogTree)currentNode).Item.Link;
if (link == nodeLink) {
if (linkIsChanged(link)) {
toRemove.add(currentNode); toRemove.add(currentNode);
currentNode = null;
++nodeCount;
continue;
}
final INetworkLink nodeLink = ((NetworkCatalogTree)currentNode).Item.Link;
if (link == nodeLink) {
if (linkIsChanged(link)) {
toRemove.add(currentNode);
} else {
processed = true;
}
currentNode = null;
++nodeCount;
break;
} else { } else {
INetworkLink newNodeLink = null; processed = true;
for (int j = i; j < links.size(); ++j) { }
final INetworkLink jlnk = links.get(j); currentNode = null;
if (nodeLink == jlnk) { ++nodeCount;
newNodeLink = jlnk; break;
break; } else {
} INetworkLink newNodeLink = null;
} for (int j = i; j < links.size(); ++j) {
if (newNodeLink == null || linkIsChanged(nodeLink)) { final INetworkLink jlnk = links.get(j);
toRemove.add(currentNode); if (nodeLink == jlnk) {
currentNode = null; newNodeLink = jlnk;
++nodeCount;
} else {
break; break;
} }
} }
if (newNodeLink == null || linkIsChanged(nodeLink)) {
toRemove.add(currentNode);
currentNode = null;
++nodeCount;
} else {
break;
}
} }
if (!processed) { }
makeValid(link); if (!processed) {
final int nextIndex = nodeIterator.nextIndex(); makeValid(link);
new NetworkCatalogRootTree(myRootTree, link, nodeCount++).Item.onDisplayItem(); final int nextIndex = nodeIterator.nextIndex();
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1); new NetworkCatalogRootTree(myRootTree, link, nodeCount++).Item.onDisplayItem();
} nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
} }
} }
@ -450,14 +440,12 @@ public class NetworkLibrary {
} }
}; };
synchronized (myLinks) { for (INetworkLink link : activeLinks()) {
for (INetworkLink link : activeLinks()) { final NetworkOperationData data = link.createOperationData(synchronizedListener);
final NetworkOperationData data = link.createOperationData(synchronizedListener); final ZLNetworkRequest request = link.simpleSearchRequest(pattern, data);
final ZLNetworkRequest request = link.simpleSearchRequest(pattern, data); if (request != null) {
if (request != null) { dataList.add(data);
dataList.add(data); requestList.add(request);
requestList.add(request);
}
} }
} }
@ -478,25 +466,17 @@ public class NetworkLibrary {
} }
} }
private <T extends INetworkLink> void addLinkInternal(T link) {
synchronized (myLinks) {
myLinks.add(link);
}
}
public void addCustomLink(ICustomNetworkLink link) { public void addCustomLink(ICustomNetworkLink link) {
final int id = link.getId(); final int id = link.getId();
if (id == ICustomNetworkLink.INVALID_ID) { if (id == ICustomNetworkLink.INVALID_ID) {
addLinkInternal(link); myLinks.add(link);
} else { } else {
synchronized (myLinks) { for (int i = myLinks.size() - 1; i >= 0; --i) {
for (int i = myLinks.size() - 1; i >= 0; --i) { final INetworkLink l = myLinks.get(i);
final INetworkLink l = myLinks.get(i); if (l instanceof ICustomNetworkLink &&
if (l instanceof ICustomNetworkLink && ((ICustomNetworkLink)l).getId() == id) {
((ICustomNetworkLink)l).getId() == id) { myLinks.set(i, link);
myLinks.set(i, link); break;
break;
}
} }
} }
} }
@ -505,9 +485,7 @@ public class NetworkLibrary {
} }
public void removeCustomLink(ICustomNetworkLink link) { public void removeCustomLink(ICustomNetworkLink link) {
synchronized (myLinks) { myLinks.remove(link);
myLinks.remove(link);
}
NetworkDatabase.Instance().deleteCustomLink(link); NetworkDatabase.Instance().deleteCustomLink(link);
invalidateChildren(); invalidateChildren();
} }