Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return immutable lists when documented #2607

Merged
merged 3 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/api/entities/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ default ImageProxy getBanner()
* <p>This will only check cached members!
* <br>See {@link net.dv8tion.jda.api.utils.MemberCachePolicy MemberCachePolicy}
*
* @return Possibly-immutable list of members who boost this guild
* @return Immutable list of members who boost this guild
*/
@Nonnull
List<Member> getBoosters();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import net.dv8tion.jda.internal.entities.ReceivedMessage;
import net.dv8tion.jda.internal.requests.FunctionalCallback;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.IOUtil;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -698,7 +699,7 @@ default List<ActionRow> getActionRows()
.stream()
.filter(ActionRow.class::isInstance)
.map(ActionRow.class::cast)
.collect(Collectors.toList());
.collect(Helpers.toUnmodifiableList());
}

/**
Expand All @@ -714,7 +715,7 @@ default List<Button> getButtons()
return getComponents().stream()
.map(LayoutComponent::getButtons)
.flatMap(List::stream)
.collect(Collectors.toList());
.collect(Helpers.toUnmodifiableList());
}

/**
Expand Down Expand Up @@ -765,7 +766,7 @@ default List<Button> getButtonsByLabel(@Nonnull String label, boolean ignoreCase
filter = b -> label.equals(b.getLabel());
return getButtons().stream()
.filter(filter)
.collect(Collectors.toList());
.collect(Helpers.toUnmodifiableList());
}

/**
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/net/dv8tion/jda/api/entities/StageInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import net.dv8tion.jda.api.entities.channel.concrete.StageChannel;
import net.dv8tion.jda.api.managers.StageInstanceManager;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* A Stage Instance holds information about a live stage.
Expand Down Expand Up @@ -77,15 +76,15 @@ public interface StageInstance extends ISnowflake
* <p>Only {@link StageChannel#isModerator(Member) stage moderators} can promote or invite speakers.
* A stage moderator can move between speaker and audience at any time.
*
* @return {@link List} of {@link Member Members} which can speak in this stage instance
* @return Immutable {@link List} of {@link Member Members} which can speak in this stage instance
*/
@Nonnull
default List<Member> getSpeakers()
{
return Collections.unmodifiableList(getChannel().getMembers()
return getChannel().getMembers()
.stream()
.filter(member -> !member.getVoiceState().isSuppressed()) // voice states should not be null since getMembers() checks only for connected members in the channel
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

/**
Expand All @@ -98,15 +97,15 @@ default List<Member> getSpeakers()
* <p>Only {@link StageChannel#isModerator(Member) stage moderators} can promote or invite speakers.
* A stage moderator can move between speaker and audience at any time.
*
* @return {@link List} of {@link Member Members} which cannot speak in this stage instance
* @return Immutable {@link List} of {@link Member Members} which cannot speak in this stage instance
*/
@Nonnull
default List<Member> getAudience()
{
return Collections.unmodifiableList(getChannel().getMembers()
return getChannel().getMembers()
.stream()
.filter(member -> member.getVoiceState().isSuppressed()) // voice states should not be null since getMembers() checks only for connected members in the channel
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.managers.channel.attribute.IPermissionContainerManager;
import net.dv8tion.jda.api.requests.restaction.PermissionOverrideAction;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Represents a {@link GuildChannel} that uses {@link net.dv8tion.jda.api.entities.PermissionOverride Permission Overrides}.
Expand Down Expand Up @@ -89,9 +88,9 @@ public interface IPermissionContainer extends GuildChannel
@Nonnull
default List<PermissionOverride> getMemberPermissionOverrides()
{
return Collections.unmodifiableList(getPermissionOverrides().stream()
return getPermissionOverrides().stream()
.filter(PermissionOverride::isMemberOverride)
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

/**
Expand All @@ -105,9 +104,9 @@ default List<PermissionOverride> getMemberPermissionOverrides()
@Nonnull
default List<PermissionOverride> getRolePermissionOverrides()
{
return Collections.unmodifiableList(getPermissionOverrides().stream()
return getPermissionOverrides().stream()
.filter(PermissionOverride::isRoleOverride)
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import net.dv8tion.jda.api.requests.restaction.pagination.ThreadChannelPaginationAction;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Abstraction of all channel types, which can contain or manage {@link ThreadChannel ThreadChannels}.
Expand Down Expand Up @@ -59,11 +58,10 @@ public interface IThreadContainer extends GuildChannel, IPermissionContainer
@Nonnull
default List<ThreadChannel> getThreadChannels()
{
return Collections.unmodifiableList(
getGuild().getThreadChannelCache().applyStream(stream ->
stream.filter(thread -> thread.getParentChannel() == this)
.collect(Collectors.toList())
));
return getGuild().getThreadChannelCache().applyStream(stream ->
stream.filter(thread -> thread.getParentChannel() == this)
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Represents a channel category in the official Discord API.
Expand Down Expand Up @@ -80,11 +78,11 @@ default List<GuildChannel> getChannels()
@Nonnull
default List<TextChannel> getTextChannels()
{
return Collections.unmodifiableList(getGuild().getTextChannelCache().applyStream(stream ->
return getGuild().getTextChannelCache().applyStream(stream ->
stream.filter(channel -> equals(channel.getParentCategory()))
.sorted()
.collect(Collectors.toList())
));
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand All @@ -96,11 +94,11 @@ default List<TextChannel> getTextChannels()
@Nonnull
default List<NewsChannel> getNewsChannels()
{
return Collections.unmodifiableList(getGuild().getNewsChannelCache().applyStream(stream ->
return getGuild().getNewsChannelCache().applyStream(stream ->
stream.filter(channel -> equals(channel.getParentCategory()))
.sorted()
.collect(Collectors.toList())
));
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand All @@ -111,11 +109,11 @@ default List<NewsChannel> getNewsChannels()
@Nonnull
default List<ForumChannel> getForumChannels()
{
return Collections.unmodifiableList(getGuild().getForumChannelCache().applyStream(stream ->
return getGuild().getForumChannelCache().applyStream(stream ->
stream.filter(channel -> equals(channel.getParentCategory()))
.sorted()
.collect(Collectors.toList())
));
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand All @@ -126,11 +124,11 @@ default List<ForumChannel> getForumChannels()
@Nonnull
default List<MediaChannel> getMediaChannels()
{
return Collections.unmodifiableList(getGuild().getMediaChannelCache().applyStream(stream ->
return getGuild().getMediaChannelCache().applyStream(stream ->
stream.filter(channel -> equals(channel.getParentCategory()))
.sorted()
.collect(Collectors.toList())
));
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand All @@ -142,11 +140,11 @@ default List<MediaChannel> getMediaChannels()
@Nonnull
default List<VoiceChannel> getVoiceChannels()
{
return Collections.unmodifiableList(getGuild().getVoiceChannelCache().applyStream(stream ->
return getGuild().getVoiceChannelCache().applyStream(stream ->
stream.filter(channel -> equals(channel.getParentCategory()))
.sorted()
.collect(Collectors.toList())
));
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand All @@ -158,11 +156,11 @@ default List<VoiceChannel> getVoiceChannels()
@Nonnull
default List<StageChannel> getStageChannels()
{
return Collections.unmodifiableList(getGuild().getStageChannelCache().applyStream(stream ->
return getGuild().getStageChannelCache().applyStream(stream ->
stream.filter(channel -> equals(channel.getParentCategory()))
.sorted()
.collect(Collectors.toList())
));
.collect(Helpers.toUnmodifiableList())
);
}

/**
Expand Down Expand Up @@ -447,13 +445,13 @@ default List<StageChannel> getStageChannels()
@Override
default List<Member> getMembers()
{
return Collections.unmodifiableList(getChannels().stream()
return getChannels().stream()
.filter(IMemberContainer.class::isInstance)
.map(IMemberContainer.class::cast)
.map(IMemberContainer::getMembers)
.flatMap(List::stream)
.distinct()
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
import net.dv8tion.jda.api.utils.data.SerializableData;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -74,7 +75,7 @@ default List<ActionComponent> getActionComponents()
return getComponents().stream()
.filter(ActionComponent.class::isInstance)
.map(ActionComponent.class::cast)
.collect(Collectors.toList());
.collect(Helpers.toUnmodifiableList());
}

/**
Expand All @@ -85,11 +86,10 @@ default List<ActionComponent> getActionComponents()
@Nonnull
default List<Button> getButtons()
{
return Collections.unmodifiableList(
getComponents().stream()
return getComponents().stream()
.filter(Button.class::isInstance)
.map(Button.class::cast)
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package net.dv8tion.jda.api.interactions.components.selections;

import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.stream.Collectors;

/**
* Component Interaction for a {@link StringSelectMenu}.
Expand All @@ -42,7 +42,7 @@ public interface StringSelectInteraction extends SelectMenuInteraction<String, S
* This resolves the selected {@link #getValues() values} to the representative {@link SelectOption SelectOption} instances.
* <br>It is recommended to check {@link #getValues()} directly instead of using the options.
*
* @return {@link List} of the selected options
* @return Immutable {@link List} of the selected options
*/
@Nonnull
default List<SelectOption> getSelectedOptions()
Expand All @@ -52,6 +52,6 @@ default List<SelectOption> getSelectedOptions()
return menu.getOptions()
.stream()
.filter(it -> values.contains(it.getValue()))
.collect(Collectors.toList());
.collect(Helpers.toUnmodifiableList());
}
}
6 changes: 3 additions & 3 deletions src/main/java/net/dv8tion/jda/api/sharding/ShardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.dv8tion.jda.internal.requests.CompletedRestAction;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.cache.UnifiedChannelCacheView;

import javax.annotation.CheckReturnValue;
Expand Down Expand Up @@ -428,11 +429,10 @@ default List<Guild> getGuilds()
default List<Guild> getMutualGuilds(@Nonnull final Collection<User> users)
{
Checks.noneNull(users, "users");
return Collections.unmodifiableList(
this.getGuildCache().stream()
return this.getGuildCache().stream()
.filter(guild -> users.stream()
.allMatch(guild::isMember))
.collect(Collectors.toList()));
.collect(Helpers.toUnmodifiableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.SerializableData;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.IOUtil;

import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;

import static net.dv8tion.jda.api.utils.messages.MessageEditBuilder.*;

Expand Down Expand Up @@ -351,12 +351,10 @@ public synchronized DataObject toData()
@Nonnull
public synchronized List<FileUpload> getFiles()
{
return Collections.unmodifiableList(
files.stream()
.filter(FileUpload.class::isInstance)
.map(FileUpload.class::cast)
.collect(Collectors.toList())
);
return files.stream()
.filter(FileUpload.class::isInstance)
.map(FileUpload.class::cast)
.collect(Helpers.toUnmodifiableList());
}

@Override
Expand Down
Loading
Loading