I have the following class:
public class UpdateData {
public static boolean updateHasMessage(Update update) {
return update.hasMessage() && update.getMessage().hasText();
}
public static boolean updateHasCallbackQuery(Update update) {
return update.hasCallbackQuery() && (update.getCallbackQuery().getData()!= null);
}
public static Long getChatId(Update update) {
if (updateHasMessage(update)) {
return update.getMessage().getChatId();
}
else if (updateHasCallbackQuery(update)) {
return update.getCallbackQuery().getMessage().getChatId();
}
else {
return null;
}
}
public static String getUserName(Update update) {
if (updateHasMessage(update)) {
return update.getMessage().getFrom().getUserName();
}
else if (updateHasCallbackQuery(update)){
return update.getCallbackQuery().getFrom().getUserName();
}
else {
return null;
}
}
public static String getInputUserData(Update update) {
if (updateHasMessage(update)) {
return update.getMessage().getText();
}
else if (updateHasCallbackQuery(update)){
return update.getCallbackQuery().getData();
}
else {
return null;
}
}
public static String getCallBackId(Update update) {
return update.getCallbackQuery().getId();
}
public static Integer getMessageId(Update update) {
if (updateHasMessage(update)) {
return update.getMessage().getMessageId();
}
else if (updateHasCallbackQuery(update)){
return update.getCallbackQuery().getMessage().getMessageId();
}
else {
return null;
}
}
public static String getCallBackData(Update update) {
return update.getCallbackQuery().getData();
}
}
As you can see I have similar block code in most methods:
if (updateHasMessage(update)) {
...
}
else if (updateHasCallbackQuery(update)) {
...
}
else {}
They return unique information and have various stuff inside, but this is very similar to code duplication. Do I need to rewrite this or just stay with it? If need to rewrite, then how?
UPDATE
Update is a Telegram API object.
update
andupdate.getCallbackQuery()
have the same interface) \$\endgroup\$