как я могу передать Function
как объект в java, которая получает 2 параметра или больше?
У меня есть этот код:
public String getSrcAfterWait(final By by) {
String currentSrc;
try {
currentSrc = tryToGetSrc(by);
} catch (StaleElementReferenceException ex)
{
currentSrc = tryToGetSrc(by);
}
return currentSrc;
}
private String tryToGetSrc(By by) {
WebElement webElement = getElementAfterWaitForDisplay2(by);
String currentSrc = null;
if (webElement != null) {
currentSrc = webElement.getAttribute("src");
}
return currentSrc;
}
private String tryToGetText(final By by) {
String currentSrc = null;
WebElement webElement = getElementAfterWaitForDisplay2(by);
if (webElement != null) {
currentSrc = webElement.getText();
}
return currentSrc;
}
public String getButtonTextAfterWait(final By by) {
String currentText;
try {
currentText = tryToGetText(by);
} catch (StaleElementReferenceException ex)
{
currentText = tryToGetText(by);
}
return currentText;
}
и я хотел обобщить его так:
public <T,V> V tryGetAttribute(final By by, Function<T,V> getFunc) {
WebElement webElement = getElementAfterWaitForDisplay2(by);
V answer = null;
if (webElement != null) {
try {
answer = getFunc.apply(webElement, getFunc);//
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return answer;
}
public String getButtonTextAfterWait(final By by) {
String currentText;
Function<WebElement, String> getFunc = webElement -> webElement.getText();
try {
currentText = tryGetAttribute(by, getFunc);
} catch (StaleElementReferenceException ex) {
currentText = tryGetAttribute(by, getFunc);
}
return currentText;
}
но я не вижу возможности передать функцию с 2 входными параметрами.
Есть ли способ или неэффективно абстрагироваться?
По крайней мере, в Java8 существует BiFunction
Видеть:
https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html
Если вам нужно больше аргументов; что мешает вам просто создать свой собственный "TripleFunction" и т.д.?
Другой вариант - определить новый класс, который объединяет два/три/... аргумента, которые вы должны передать.
"src"
. Аббревиатура отсутствует, просто используется оригинальное имя. (2) пожалуйста, посмотрите этот пост stackoverflow.com/questions/29590715/… . Как бы вы реализовали предложенное решение в противном случае?