Skip to content

Commit

Permalink
Merge #749 into 3.2
Browse files Browse the repository at this point in the history
Use Optional instead of @nullable when appropriate
  • Loading branch information
darichey committed Jul 29, 2020
2 parents e0a915e + b48b5c7 commit 55b3faa
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 64 deletions.
90 changes: 42 additions & 48 deletions core/src/main/java/discord4j/core/object/Embed.java
Expand Up @@ -20,6 +20,7 @@
import discord4j.discordjson.json.*;
import discord4j.discordjson.possible.Possible;
import discord4j.rest.util.Color;
import reactor.util.annotation.Nullable;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -85,8 +86,9 @@ public Optional<String> getTitle() {
* @return The type of embed, if present.
*/
public Type getType() {
// TODO FIXME is this actually Possible?
return Type.of(data.type().get());
return data.type().toOptional()
.map(Type::of)
.orElseThrow(IllegalStateException::new); // type should always be present on received embeds
}

/**
Expand Down Expand Up @@ -289,19 +291,17 @@ public String getText() {
*
* @return The URL of the footer icon (only supports http(s) and attachments).
*/
public String getIconUrl() {
// TODO FIXME: is this actually Possible?
return data.iconUrl().get();
public Optional<String> getIconUrl() {
return data.iconUrl().toOptional();
}

/**
* Gets a proxied URL of the footer icon.
*
* @return A proxied URL of the footer icon.
*/
public String getProxyIconUrl() {
// TODO FIXME: is this actually Possible?
return data.proxyIconUrl().get();
public Optional<String> getProxyIconUrl() {
return data.proxyIconUrl().toOptional();
}
}

Expand Down Expand Up @@ -335,8 +335,8 @@ public Embed getEmbed() {
* @return The source URL of the image (only supports http(s) and attachments).
*/
public String getUrl() {
// TODO FIXME: is this actually Possible?
return data.url().get();
return data.url().toOptional()
.orElseThrow(IllegalStateException::new); // image url should always be present on received embeds
}

/**
Expand All @@ -345,8 +345,8 @@ public String getUrl() {
* @return A proxied URL of the image.
*/
public String getProxyUrl() {
// TODO FIXME: is this actually Possible?
return data.proxyUrl().get();
return data.proxyUrl().toOptional()
.orElseThrow(IllegalStateException::new); // image url should always be present on received embeds
}

/**
Expand All @@ -355,8 +355,8 @@ public String getProxyUrl() {
* @return The height of the image.
*/
public int getHeight() {
// TODO FIXME: is this actually Possible?
return data.height().get();
return data.height().toOptional()
.orElseThrow(IllegalStateException::new);
}

/**
Expand All @@ -365,8 +365,8 @@ public int getHeight() {
* @return The width of the image.
*/
public int getWidth() {
// TODO FIXME: is this actually Possible?
return data.width().get();
return data.width().toOptional()
.orElseThrow(IllegalStateException::new);
}
}

Expand Down Expand Up @@ -400,8 +400,8 @@ public Embed getEmbed() {
* @return The source URL of the thumbnail (only supports http(s) and attachments).
*/
public String getUrl() {
// TODO FIXME: is this actually Possible?
return data.url().get();
return data.url().toOptional()
.orElseThrow(IllegalStateException::new); // thumbnail url should always be present on received embeds
}

/**
Expand All @@ -410,8 +410,8 @@ public String getUrl() {
* @return A proxied URL of the thumbnail.
*/
public String getProxyUrl() {
// TODO FIXME: is this actually Possible?
return data.proxyUrl().get();
return data.proxyUrl().toOptional()
.orElseThrow(IllegalStateException::new); // thumbnail url should always be present on received embeds
}

/**
Expand All @@ -420,8 +420,8 @@ public String getProxyUrl() {
* @return The height of the thumbnail.
*/
public int getHeight() {
// TODO FIXME: is this actually Possible?
return data.height().get();
return data.height().toOptional()
.orElseThrow(IllegalStateException::new);
}

/**
Expand All @@ -430,8 +430,8 @@ public int getHeight() {
* @return The width of the thumbnail.
*/
public int getWidth() {
// TODO FIXME: is this actually Possible?
return data.width().get();
return data.width().toOptional()
.orElseThrow(IllegalStateException::new);
}
}

Expand Down Expand Up @@ -465,8 +465,8 @@ public Embed getEmbed() {
* @return The source URL of the video.
*/
public String getUrl() {
// TODO FIXME: is this actually Possible?
return data.url().get();
return data.url().toOptional()
.orElseThrow(IllegalStateException::new); // video url should always be present on received embeds
}

/**
Expand All @@ -475,8 +475,8 @@ public String getUrl() {
* @return The height of the video.
*/
public int getHeight() {
// TODO FIXME: is this actually Possible?
return data.height().get();
return data.height().toOptional()
.orElseThrow(IllegalStateException::new);
}

/**
Expand All @@ -485,8 +485,8 @@ public int getHeight() {
* @return The width of the video.
*/
public int getWidth() {
// TODO FIXME: is this actually Possible?
return data.width().get();
return data.width().toOptional()
.orElseThrow(IllegalStateException::new);
}
}

Expand Down Expand Up @@ -519,9 +519,8 @@ public Embed getEmbed() {
*
* @return The name of the provider.
*/
public String getName() {
// TODO FIXME: is this actually Possible?
return data.name().get();
public Optional<String> getName() {
return data.name().toOptional();
}

/**
Expand All @@ -530,7 +529,6 @@ public String getName() {
* @return The URL of the provider.
*/
public Optional<String> getUrl() {
// TODO FIXME: is this actually Possible?
return Possible.flatOpt(data.url());
}
}
Expand Down Expand Up @@ -567,39 +565,35 @@ public Embed getEmbed() {
*
* @return The name of the author.
*/
public String getName() {
// TODO FIXME: is this actually Possible?
return data.name().get();
public Optional<String> getName() {
return data.name().toOptional();
}

/**
* Gets the URL of the author.
*
* @return The URL of the author.
*/
public String getUrl() {
// TODO FIXME: is this actually Possible?
return data.url().get();
public Optional<String> getUrl() {
return data.url().toOptional();
}

/**
* Gets the URL of the author icon (only supports http(s) and attachments).
*
* @return The URL of the author icon (only supports http(s) and attachments).
*/
public String getIconUrl() {
// TODO FIXME: is this actually Possible?
return data.iconUrl().get();
public Optional<String> getIconUrl() {
return data.iconUrl().toOptional();
}

/**
* Gets a proxied URL of the author icon.
*
* @return A proxied URL of the author icon.
*/
public String getProxyIconUrl() {
// TODO FIXME: is this actually Possible?
return data.proxyIconUrl().get();
public Optional<String> getProxyIconUrl() {
return data.proxyIconUrl().toOptional();
}
}

Expand Down Expand Up @@ -657,8 +651,8 @@ public String getValue() {
* @return {@code true} if this field should display inline, {@code false} otherwise.
*/
public boolean isInline() {
// TODO FIXME: is this actually Possible?
return data.inline().get();
return data.inline().toOptional()
.orElseThrow(IllegalStateException::new);
}
}

Expand Down
Expand Up @@ -35,8 +35,7 @@ public MessageReference(final GatewayDiscordClient gateway, final MessageReferen
}

public Snowflake getChannelId() {
// TODO FIXME: is this actually Possible?
return Snowflake.of(data.channelId().get());
return Snowflake.of(data.channelId().toOptional().orElseThrow(IllegalStateException::new));
}

public Optional<Snowflake> getGuildId() {
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/discord4j/core/object/VoiceState.java
Expand Up @@ -64,8 +64,10 @@ public GatewayDiscordClient getClient() {
* @return The guild ID this voice state is for.
*/
public Snowflake getGuildId() {
// TODO FIXME: why is this Possible?
return Snowflake.of(data.guildId().get());
// Even though Discord's raw Voice State structure does not include the guild_id key when sent in the
// voice_states for a guild_create, we manually populate the field in GuildDispatchHandlers.guildCreate, so
// it should always be present, making this safe.
return Snowflake.of(data.guildId().toOptional().orElseThrow(IllegalStateException::new));
}

/**
Expand Down
34 changes: 23 additions & 11 deletions core/src/main/java/discord4j/core/object/entity/GuildEmoji.java
Expand Up @@ -18,6 +18,7 @@

import discord4j.core.GatewayDiscordClient;
import discord4j.core.retriever.EntityRetrievalStrategy;
import discord4j.discordjson.json.UserData;
import discord4j.rest.util.Image;
import discord4j.common.util.Snowflake;
import discord4j.core.spec.GuildEmojiEditSpec;
Expand All @@ -42,7 +43,6 @@
* <p>
* <a href="https://discord.com/developers/docs/resources/emoji#emoji-resource">Emoji Resource</a>
*/
// TODO FIXME so many gets
public final class GuildEmoji implements Entity {

/** The path for {@code GuildEmoji} image URLs. */
Expand Down Expand Up @@ -77,7 +77,9 @@ public GatewayDiscordClient getClient() {

@Override
public Snowflake getId() {
return Snowflake.of(data.id().get()); // this is safe for guild emojis
return data.id()
.map(Snowflake::of)
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand All @@ -86,7 +88,8 @@ public Snowflake getId() {
* @return The emoji name.
*/
public String getName() {
return data.name().get();
return data.name()
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand All @@ -95,9 +98,11 @@ public String getName() {
* @return The IDs of the roles this emoji is whitelisted to.
*/
public Set<Snowflake> getRoleIds() {
return data.roles().get().stream()
.map(Snowflake::of)
.collect(Collectors.toSet());
return data.roles().toOptional()
.map(roles -> roles.stream()
.map(Snowflake::of)
.collect(Collectors.toSet()))
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand Down Expand Up @@ -135,9 +140,12 @@ public Flux<Role> getRoles(EntityRetrievalStrategy retrievalStrategy) {
* an error is received, it is emitted through the {@code Mono}.
*/
public Mono<User> getUser() {
UserData user = data.user().toOptional()
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis

return gateway.getRestClient().getEmojiService()
.getGuildEmoji(getGuildId().asLong(), getId().asLong())
.map(data -> new User(gateway, data.user().get()));
.map(data -> new User(gateway, user));
}

/**
Expand All @@ -146,7 +154,8 @@ public Mono<User> getUser() {
* @return {@code true} if this emoji must be wrapped in colons, {@code false} otherwise.
*/
public boolean requiresColons() {
return data.requireColons().get();
return data.requireColons().toOptional()
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand All @@ -155,7 +164,8 @@ public boolean requiresColons() {
* @return {@code true} if this emoji is managed, {@code false} otherwise.
*/
public boolean isManaged() {
return data.managed().get();
return data.managed().toOptional()
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand All @@ -164,7 +174,8 @@ public boolean isManaged() {
* @return {@code true} if this emoji is animated, {@code false} otherwise.
*/
public boolean isAnimated() {
return data.animated().get();
return data.animated().toOptional()
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand All @@ -173,7 +184,8 @@ public boolean isAnimated() {
* @return {@code true} if this emoji is available, {@code false} otherwise (due to loss of Server Boosts for example).
*/
public boolean isAvailable() {
return data.available().get();
return data.available().toOptional()
.orElseThrow(IllegalStateException::new); // this should be safe for guild emojis
}

/**
Expand Down
Expand Up @@ -47,7 +47,8 @@ public TextChannel(GatewayDiscordClient gateway, ChannelData data) {
* @return The amount of seconds an user has to wait before sending another message (0-120).
*/
public int getRateLimitPerUser() {
return getData().rateLimitPerUser().get();
return getData().rateLimitPerUser().toOptional()
.orElseThrow(IllegalStateException::new); // this should be safe for all TextChannels
}

/**
Expand Down

0 comments on commit 55b3faa

Please sign in to comment.