Skip to content

Commit

Permalink
Merge pull request #21 from w3stling/search-misses-not-cached
Browse files Browse the repository at this point in the history
Search misses not cached
  • Loading branch information
w3stling authored Dec 17, 2022
2 parents 48b95cb + afd7296 commit 41b37d2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
85 changes: 58 additions & 27 deletions src/main/java/com/apptasticsoftware/lei/IsinLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,34 @@ public Optional<String> getIsinByCusip(String cusip) {
return Optional.empty();
}

return sendRequestFunctions.stream()
var isin = pullCacheResult(cusip);
if (isin != null) {
return Optional.of(isin);
}

isin = sendRequestFunctions.stream()
.map(f -> {
var isin = f.apply(CUSIP_URL, "cusip=US" + cusip);
if (isin != null) {
return isin;
var isinNumber = f.apply(CUSIP_URL, "cusip=US" + cusip);
if (isinNumber != null) {
return isinNumber;
}

isin = f.apply(CUSIP_URL, "cusip=CA" + cusip);
if (isin != null) {
return isin;
isinNumber = f.apply(CUSIP_URL, "cusip=CA" + cusip);
if (isinNumber != null) {
return isinNumber;
}

return f.apply(CUSIP_URL, "cusip=BM" + cusip);
})
.filter(Objects::nonNull)
.findFirst();
.findFirst()
.orElse(null);

putCacheResult(cusip, isin);
if (isin != null && isin.isEmpty()) {
isin = null;
}
return Optional.ofNullable(isin);
}

/**
Expand All @@ -87,25 +99,32 @@ public Optional<String> getIsinBySedol(String sedol) {
return Optional.empty();
}

return sendRequestFunctions.stream()
var isin = pullCacheResult(sedol);
if (isin != null) {
return Optional.of(isin);
}

isin = sendRequestFunctions.stream()
.map(f -> {
String isin = f.apply(SEDOL_URL, "sedol=GB" + sedol);
if (isin != null) {
return isin;
var isinNumber = f.apply(SEDOL_URL, "sedol=GB" + sedol);
if (isinNumber != null) {
return isinNumber;
}

return f.apply(SEDOL_URL, "sedol=IE" + sedol);
})
.filter(Objects::nonNull)
.findFirst();
}
.findFirst()
.orElse(null);

String sendRequest1(String url, String data) {
var res = cache.get(data);
if (res != null) {
return res;
putCacheResult(sedol, isin);
if (isin != null && isin.isEmpty()) {
isin = null;
}
return Optional.ofNullable(isin);
}

String sendRequest1(String url, String data) {
var referer = CUSIP_URL.equals(url) ? "https://www.isindb.com/convert-cusip-to-isin/" : "https://www.isindb.com/convert-sedol-to-isin/";
String isin = null;

Expand Down Expand Up @@ -141,7 +160,6 @@ String sendRequest1(String url, String data) {

var text = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
isin = getIsin(text);
cacheResult(data, isin);
} catch (InterruptedException e) {
var logger = Logger.getLogger(LOGGER);
logger.severe(e.getMessage());
Expand All @@ -155,11 +173,6 @@ String sendRequest1(String url, String data) {
}

String sendRequest2(String url, String data) {
var res = cache.get(data);
if (res != null) {
return res;
}

var referer = CUSIP_URL.equals(url) ? "https://www.isindb.com/convert-cusip-to-isin/" : "https://www.isindb.com/convert-sedol-to-isin/";
String isin = null;

Expand Down Expand Up @@ -190,7 +203,6 @@ String sendRequest2(String url, String data) {
var document = response.parse();
var text = document.body().html();
isin = getIsin(text);
cacheResult(data, isin);
} catch (Exception e) {
var logger = Logger.getLogger(LOGGER);
logger.severe(e.getMessage());
Expand All @@ -200,10 +212,19 @@ String sendRequest2(String url, String data) {
}

private String getIsin(String text) {
if (text.toLowerCase().contains("add isin to database")) {
return "";
}

var isin = parseIsin1(text);
if (isin == null) {
isin = parseIsin2(text);
}

if (isin != null && !IsinCodeValidator.isValid(isin)) {
return "";
}

return isin;
}

Expand All @@ -223,10 +244,20 @@ String parseIsin2(String text) {
return null;
}

private void cacheResult(String key, String value) {
if (value == null || cache.containsKey(key)) {
private String pullCacheResult(String key) {
var value = cache.get(key);
if (value != null && value.isEmpty()) {
value = null;
}
return value;
}
private void putCacheResult(String key, String value) {
if (cache.containsKey(key)) {
return;
}
if (value == null) {
value = "";
}
cache.put(key, value);
if (cache.size() > cacheSize) {
cache.pollLastEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void lookupByCusip() {
assertTrue(lookup.getIsinByCusip("931142103").isPresent());
assertTrue(lookup.getIsinByCusip("931142103").isPresent());
assertFalse(lookup.getIsinByCusip("").isPresent());
assertFalse(lookup.getIsinByCusip("0@7833105").isPresent());
}

@Test
Expand Down

0 comments on commit 41b37d2

Please sign in to comment.