Skip to content

Commit

Permalink
Use guava
Browse files Browse the repository at this point in the history
  • Loading branch information
SgrAlpha committed Apr 12, 2020
1 parent 0a60e57 commit e545b26
Show file tree
Hide file tree
Showing 87 changed files with 536 additions and 675 deletions.
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<name>Telegram Bot API</name>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.sgr.telegram.bot.api.http;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkNotNull;

import io.sgr.telegram.bot.api.models.http.ApiResponse;

Expand All @@ -37,11 +37,9 @@ class CompletableFutureBasedCallAdapter<T> implements CallAdapter<ApiResponse<T>
private final Logger logger;

CompletableFutureBasedCallAdapter(@Nonnull final Type responseType, final boolean retry, @Nonnull final Logger logger) {
notNull(responseType, "Missing response type!");
this.responseType = responseType;
this.responseType = checkNotNull(responseType, "Missing response type!");
this.retry = retry;
notNull(logger, "Missing logger!");
this.logger = logger;
this.logger = checkNotNull(logger, "Missing logger!");
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.sgr.telegram.bot.api.http;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkNotNull;

import io.sgr.telegram.bot.api.exceptions.ApiCallException;
import io.sgr.telegram.bot.api.models.http.ApiErrorResponse;
Expand All @@ -43,11 +43,9 @@ class CompletableFutureBasedCallback<T> implements Callback<ApiResponse<T>> {
private final Logger logger;

CompletableFutureBasedCallback(@Nonnull final CompletableFuture<T> future, final boolean retry, @Nonnull final Logger logger) {
notNull(future, "Missing future!");
this.future = future;
this.future = checkNotNull(future, "Missing future!");
this.retry = retry;
notNull(logger, "Missing logger!");
this.logger = logger;
this.logger = checkNotNull(logger, "Missing logger!");
}

@Override public void onResponse(@Nonnull final Call<ApiResponse<T>> call, @Nonnull final Response<ApiResponse<T>> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.sgr.telegram.bot.api.http;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkNotNull;

import io.sgr.telegram.bot.api.models.http.ApiResponse;

Expand All @@ -39,8 +39,7 @@ public class DefaultCallAdapterFactory extends CallAdapter.Factory {

public DefaultCallAdapterFactory(final boolean retry, @Nonnull final Logger logger) {
this.retry = retry;
notNull(logger, "Missing logger!");
this.logger = logger;
this.logger = checkNotNull(logger, "Missing logger!");
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions api/src/main/java/io/sgr/telegram/bot/api/models/Audio.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -63,12 +64,11 @@ public Audio(
@JsonProperty("mime_type") final String mimeType,
@JsonProperty("file_size") final long fileSize,
@JsonProperty("thumb") final PhotoSize thumb) {
notEmptyString(fileId, "File ID should be provided");
checkArgument(!isNullOrEmpty(fileId), "File ID should be provided");
this.fileId = fileId;
checkArgument(!isNullOrEmpty(fileUniqueId), "File unique ID should be provided");
this.fileUniqueId = fileUniqueId;
if (duration < 0) {
throw new IllegalArgumentException("Duration should be greater than or equal to zero");
}
checkArgument(duration >= 0, "Duration should be greater than or equal to zero");
this.duration = duration;
this.performer = performer;
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -65,13 +66,12 @@ public CallbackQuery(
@JsonProperty("chat_instance") String chatInstance,
@JsonProperty("data") String data,
@JsonProperty("game_short_name") String gameShortName) {
notEmptyString(id, "ID should be provided");
checkArgument(!isNullOrEmpty(id), "ID should be provided");
this.id = id;
notNull(from, "Sender should be provided");
this.from = from;
this.from = checkNotNull(from, "Sender should be provided");
this.message = message;
this.inlineMessageId = inlineMessageId;
notEmptyString(chatInstance, "Chat instance should be provided");
checkArgument(!isNullOrEmpty(chatInstance), "Chat instance should be provided");
this.chatInstance = chatInstance;
this.data = data;
this.gameShortName = gameShortName;
Expand Down
8 changes: 3 additions & 5 deletions api/src/main/java/io/sgr/telegram/bot/api/models/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkNotNull;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -69,7 +69,7 @@ public class Chat {
*/
@JsonCreator
public Chat(
@JsonProperty("id") Long id,
@JsonProperty("id") long id,
@JsonProperty("type") ChatType type,
@JsonProperty("title") String title,
@JsonProperty("username") String username,
Expand All @@ -83,13 +83,11 @@ public Chat(
@JsonProperty("slow_mode_delay") Integer slowModeDelay,
@JsonProperty("sticker_set_name") String stickerSetName,
@JsonProperty("can_set_sticker_set") Boolean canSetStickerSet) {
notNull(id, "Chat ID should be provided.");
if (Math.abs(id) > 1e13) {
throw new IllegalArgumentException(String.format("Unique identifier for this chat should not exceeding 1e13 by absolute value, but got %d", id));
}
this.id = id;
notNull(type, "ChatType should be provided.");
this.type = type;
this.type = checkNotNull(type, "ChatType should be provided.");
this.title = title;
this.username = username;
this.firstName = firstName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkNotNull;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -100,10 +100,8 @@ public ChatMember(
@JsonProperty("can_send_polls") final Boolean canSendPolls,
@JsonProperty("can_send_other_messages") final Boolean canSendOtherMessage,
@JsonProperty("can_add_web_page_previews") final Boolean canAddWebPagePreviews) {
notNull(user, "User should be provided.");
this.user = user;
notNull(status, "Chat isMember status should be provided.");
this.status = status;
this.user = checkNotNull(user, "User should be provided.");
this.status = checkNotNull(status, "Chat isMember status should be provided.");
this.customTitle = customTitle;
this.untilDate = untilDate;
this.canBeEdited = canBeEdited;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import com.fasterxml.jackson.annotation.JsonCreator;

Expand All @@ -44,7 +45,7 @@ public enum ChatMemberStatus {
*/
@JsonCreator
public static ChatMemberStatus fromString(final String str) {
notEmptyString(str, "Cannot parse ChatMemberStatus from null or empty string!");
checkArgument(!isNullOrEmpty(str), "Cannot parse ChatMemberStatus from null or empty string!");
return ChatMemberStatus.valueOf(str.toUpperCase(Locale.ENGLISH));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import com.fasterxml.jackson.annotation.JsonCreator;

Expand All @@ -42,7 +43,7 @@ public enum ChatType {
*/
@JsonCreator
public static ChatType fromString(final String str) {
notEmptyString(str, "Cannot parse ChatType from null or empty string!");
checkArgument(!isNullOrEmpty(str), "Cannot parse ChatType from null or empty string!");
return ChatType.valueOf(str.toUpperCase(Locale.ENGLISH));
}

Expand Down
7 changes: 4 additions & 3 deletions api/src/main/java/io/sgr/telegram/bot/api/models/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -55,9 +56,9 @@ public Contact(
@JsonProperty("last_name") String lastName,
@JsonProperty("user_id") Long userId,
@JsonProperty("vcard") final String vCard) {
notEmptyString(phoneNumber, "Phone number should be provided.");
checkArgument(!isNullOrEmpty(phoneNumber), "Phone number should be provided.");
this.phoneNumber = phoneNumber;
notEmptyString(phoneNumber, "First name should be provided.");
checkArgument(!isNullOrEmpty(firstName), "First name should be provided.");
this.firstName = firstName;
this.lastName = lastName;
this.userId = userId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -57,8 +58,9 @@ public Document(
@JsonProperty("file_name") final String fileName,
@JsonProperty("mime_type") final String mimeType,
@JsonProperty("file_size") final long fileSize) {
notEmptyString(fileId, "File ID should be provided");
checkArgument(!isNullOrEmpty(fileId), "File ID should be provided");
this.fileId = fileId;
checkArgument(!isNullOrEmpty(fileUniqueId), "File unique ID should be provided");
this.fileUniqueId = fileUniqueId;
this.thumb = thumb;
this.fileName = fileName;
Expand Down
7 changes: 4 additions & 3 deletions api/src/main/java/io/sgr/telegram/bot/api/models/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;
import io.sgr.telegram.bot.api.utils.Preconditions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -50,8 +50,9 @@ public File(
@JsonProperty("file_unique_id") final String fileUniqueId,
@JsonProperty("file_size") final Long fileSize,
@JsonProperty("file_path") final String filePath) {
notEmptyString(fileId, "File ID should be provided");
checkArgument(!isNullOrEmpty(fileId), "File ID should be provided");
this.fileId = fileId;
checkArgument(!isNullOrEmpty(fileUniqueId), "File unique ID should be provided");
this.fileUniqueId = fileUniqueId;
this.fileSize = fileSize;
this.filePath = filePath;
Expand Down
9 changes: 3 additions & 6 deletions api/src/main/java/io/sgr/telegram/bot/api/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkNotNull;

import io.sgr.telegram.bot.api.models.game.Game;
import io.sgr.telegram.bot.api.models.markups.InlineKeyboardButton;
import io.sgr.telegram.bot.api.models.markups.InlineKeyboardMarkup;
import io.sgr.telegram.bot.api.models.sticker.Sticker;
import io.sgr.telegram.bot.api.utils.JsonUtil;
Expand Down Expand Up @@ -142,7 +141,7 @@ public class Message {
* @param replyMarkup Optional. Inline keyboard attached to the message. login_url buttons are represented as ordinary url buttons.
*/
public Message(
@JsonProperty("message_id") Long id,
@JsonProperty("message_id") long id,
@JsonProperty("from") final User from,
@JsonProperty("date") final Long date,
@JsonProperty("chat") final Chat chat,
Expand Down Expand Up @@ -185,11 +184,9 @@ public Message(
@JsonProperty("pinned_message") final Message pinned,
@JsonProperty("connected_website") final String connectedWebsite,
@JsonProperty("reply_markup") final InlineKeyboardMarkup replyMarkup) {
notNull(id, "Message ID should be provided.");
this.id = id;
this.from = from;
notNull(date, "Message sent date should be provided.");
this.date = date;
this.date = checkNotNull(date, "Message sent date should be provided.");
this.chat = chat;
this.forwardFrom = forwardFrom;
this.forwardFromChat = forwardFromChat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -60,12 +61,10 @@ public MessageEntity(
@JsonProperty("url") final String url,
@JsonProperty("user") final User user,
@JsonProperty("language") final String language) {
notEmptyString(type, "Message entity type should be provided.");
checkArgument(!isNullOrEmpty(type), "Message entity type should be provided.");
this.type = type;
notNull(offset, "Message entity offset should be provided.");
this.offset = offset;
notNull(length, "Message entity length should be provided.");
this.length = length;
this.offset = checkNotNull(offset, "Message entity offset should be provided.");
this.length = checkNotNull(length, "Message entity length should be provided.");
this.url = url;
this.user = user;
this.language = language;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notEmptyString;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import io.sgr.telegram.bot.api.utils.JsonUtil;

Expand Down Expand Up @@ -56,8 +57,9 @@ public PhotoSize(
@JsonProperty("width") final int width,
@JsonProperty("height") final int height,
@JsonProperty("file_size") final Integer fileSize) {
notEmptyString(fileId, "File ID should be provided.");
checkArgument(!isNullOrEmpty(fileId), "File ID should be provided.");
this.fileId = fileId;
checkArgument(!isNullOrEmpty(fileUniqueId), "File unique ID should be provided");
this.fileUniqueId = fileUniqueId;
this.width = width;
this.height = height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package io.sgr.telegram.bot.api.models;

import static io.sgr.telegram.bot.api.utils.Preconditions.notNull;

import io.sgr.telegram.bot.api.utils.JsonUtil;

import com.fasterxml.jackson.annotation.JsonCreator;
Expand All @@ -44,12 +42,10 @@ public class TelegramError {
*/
@JsonCreator
public TelegramError(
@JsonProperty("ok") Boolean ok,
@JsonProperty("error_code") Integer errorCode,
@JsonProperty("ok") boolean ok,
@JsonProperty("error_code") int errorCode,
@JsonProperty("description") String description) {
notNull(ok, "Status should be provided.");
this.ok = ok;
notNull(ok, "Error code should be provided.");
this.errorCode = errorCode;
this.description = description;
}
Expand Down
Loading

0 comments on commit e545b26

Please sign in to comment.