Я решил это, реализовав объект-оболочку вокруг него:
public UrlShortenerService replaceUrls(String input, String messageId, String type, Handler<AsyncResult<String>> resultHandler) {
Text text = new Text(input);
Observable
.fromIterable(text.getOriginalUrlsDistinct()::iterator)
.flatMap(
longUrl -> {
return mongoUrlService
.rxFindOrCreateSet(longUrl)
.toObservable();
}
)
.flatMap(
sets -> {
Map.Entry entry = sets.iterator().next();
text.addProcessedUrl(entry.getKey().toString(), buildShortUrl(entry.getValue().toString(), messageId, type));
return Observable.just(true);
}
)
.toList()
.subscribe(
allCompleted -> {
String completeText = text.replaceAll();
resultHandler.handle(Future.succeededFuture(completeText));
},
throwable -> {
throwable.printStackTrace();
resultHandler.handle(Future.failedFuture(throwable));
}
);
return this;
}
Класс оболочки (называемый Текст):
public class Text {
private static final Logger LOGGER = LoggerFactory.getLogger(Text.class);
private String inputText;
private HashMap<String, String> processedUrls;
private Matcher matcher;
private List<String> originalUrls;
public Text(String text){
this.inputText = text;
String pattern = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
this.matcher = Pattern.compile(pattern).matcher(text);
originalUrls = new ArrayList<>();
processedUrls = new HashMap<>();
findAllUrls();
}
public void addOriginalUrl(String url){
originalUrls.add(url);
}
public List<String> getOriginalUrls(){
return originalUrls;
}
public ArrayList<String> getOriginalUrlsDistinct(){
return new ArrayList(new HashSet(originalUrls));
}
public void addProcessedUrl(String originalUrl, String processedUrl){
processedUrls.put(originalUrl, processedUrl);
}
public String replaceAll(){
String result = "";
String text = inputText;
for(HashMap.Entry<String, String> val : processedUrls.entrySet())
{
text = text.replace(val.getKey(), val.getValue());
}
return text;
}
private void findAllUrls(){
while (matcher.find()){
String url = matcher.group();
addOriginalUrl(url);
}
}
}
У кого-нибудь есть лучшее решение?