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
1c0251070f
commit
9299d4c222
1 changed files with 84 additions and 106 deletions
|
@ -83,17 +83,19 @@ 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,14 +142,12 @@ 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,7 +200,6 @@ 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)) {
|
||||||
|
@ -209,7 +208,6 @@ public class NetworkLibrary {
|
||||||
}
|
}
|
||||||
myLinks.removeAll(toRemove);
|
myLinks.removeAll(toRemove);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*private void testDate(ATOMDateConstruct date1, ATOMDateConstruct date2) {
|
/*private void testDate(ATOMDateConstruct date1, ATOMDateConstruct date2) {
|
||||||
String sign = " == ";
|
String sign = " == ";
|
||||||
|
@ -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,7 +265,6 @@ 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);
|
||||||
|
@ -277,18 +272,15 @@ public class NetworkLibrary {
|
||||||
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,7 +311,6 @@ 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) {
|
||||||
|
@ -370,7 +361,6 @@ public class NetworkLibrary {
|
||||||
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
|
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
while (currentNode != null || nodeIterator.hasNext()) {
|
while (currentNode != null || nodeIterator.hasNext()) {
|
||||||
if (currentNode == null) {
|
if (currentNode == null) {
|
||||||
|
@ -450,7 +440,6 @@ 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);
|
||||||
|
@ -459,7 +448,6 @@ public class NetworkLibrary {
|
||||||
requestList.add(request);
|
requestList.add(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
while (requestList.size() != 0) {
|
while (requestList.size() != 0) {
|
||||||
ZLNetworkManager.Instance().perform(requestList);
|
ZLNetworkManager.Instance().perform(requestList);
|
||||||
|
@ -478,18 +466,11 @@ 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 &&
|
||||||
|
@ -499,15 +480,12 @@ public class NetworkLibrary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
NetworkDatabase.Instance().saveCustomLink(link);
|
NetworkDatabase.Instance().saveCustomLink(link);
|
||||||
invalidateChildren();
|
invalidateChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue