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

Add Mentions#getMentions/isMentioned overloads accepting Collection #2737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 60 additions & 2 deletions src/main/java/net/dv8tion/jda/api/entities/Mentions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.jetbrains.annotations.Unmodifiable;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;

/**
Expand Down Expand Up @@ -395,7 +398,29 @@ public interface Mentions
*/
@Nonnull
@Unmodifiable
List<IMentionable> getMentions(@Nonnull Message.MentionType... types);
List<IMentionable> getMentions(@Nonnull Collection<Message.MentionType> types);

/**
* Combines all instances of {@link net.dv8tion.jda.api.entities.IMentionable IMentionable}
* filtered by the specified {@link net.dv8tion.jda.api.entities.Message.MentionType MentionType} values.
* <br>If a {@link Member} is available, it will be taken in favor of a {@link User}.
* This only provides either the Member or the User instance, rather than both.
*
* <p>If no MentionType values are given, all types are used.
*
* @param types
* {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} to include
*
* @return Immutable list of filtered {@link net.dv8tion.jda.api.entities.IMentionable IMentionable} instances
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
default List<IMentionable> getMentions(@Nonnull Message.MentionType... types)
{
if (types == null || types.length == 0)
return getMentions(EnumSet.allOf(Message.MentionType.class));
return getMentions(EnumSet.copyOf(Arrays.asList(types)));
}

/**
* Checks if given {@link net.dv8tion.jda.api.entities.IMentionable IMentionable}
Expand All @@ -414,7 +439,40 @@ public interface Mentions
* The types to include when checking whether this type was mentioned.
* This will be used with {@link #getMentions(Message.MentionType...) getMentions(MentionType...)}
*
* @throws IllegalArgumentException
* If the mentionable or the types are {@code null}
*
* @return True, if the given mentionable was mentioned in this message
*/
boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Collection<Message.MentionType> types);

/**
* Checks if given {@link net.dv8tion.jda.api.entities.IMentionable IMentionable}
* was mentioned in any way (@User, @everyone, @here, @Role).
* <br>If no filtering {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} are
* specified, all types are used.
*
* <p>{@link Message.MentionType#HERE MentionType.HERE} and {@link Message.MentionType#EVERYONE MentionType.EVERYONE}
* will only be checked, if the given {@link net.dv8tion.jda.api.entities.IMentionable IMentionable} is of type
* {@link net.dv8tion.jda.api.entities.User User} or {@link net.dv8tion.jda.api.entities.Member Member}.
* <br>Online status of Users/Members is <b>NOT</b> considered when checking {@link Message.MentionType#HERE MentionType.HERE}.
*
* @param mentionable
* The mentionable entity to check on.
* @param types
* The types to include when checking whether this type was mentioned.
* This will be used with {@link #getMentions(Message.MentionType...) getMentions(MentionType...)}
*
* @throws IllegalArgumentException
* If the mentionable is {@code null}
*
* @return True, if the given mentionable was mentioned in this message
*/
boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Message.MentionType... types);
@SuppressWarnings("ConstantConditions")
default boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Message.MentionType... types)
{
if (types == null || types.length == 0)
return isMentioned(mentionable, EnumSet.allOf(Message.MentionType.class));
return isMentioned(mentionable, EnumSet.copyOf(Arrays.asList(types)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.MentionTypeUtil;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BagUtils;
import org.apache.commons.collections4.bag.HashBag;
Expand Down Expand Up @@ -217,14 +218,12 @@ public Bag<Member> getMembersBag()

@Nonnull
@Override
public List<IMentionable> getMentions(@Nonnull Message.MentionType... types)
public List<IMentionable> getMentions(@Nonnull Collection<Message.MentionType> types)
{
if (types.length == 0)
return getMentions(Message.MentionType.values());
Checks.notNull(types, "Mention Types");

List<IMentionable> mentions = new ArrayList<>();
// Convert to set to avoid duplicates
EnumSet<Message.MentionType> set = EnumSet.of(types[0], types);
for (Message.MentionType type : set)
for (Message.MentionType type : MentionTypeUtil.getEffectiveMentionTypes(types))
{
switch (type)
{
Expand All @@ -250,14 +249,12 @@ public List<IMentionable> getMentions(@Nonnull Message.MentionType... types)
}

@Override
public boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Message.MentionType... types)
public boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Collection<Message.MentionType> types)
{
Checks.notNull(types, "Mention Types");
if (types.length == 0)
return isMentioned(mentionable, Message.MentionType.values());

String id = mentionable.getId();
for (Message.MentionType type : types)
for (Message.MentionType type : MentionTypeUtil.getEffectiveMentionTypes(types))
{
switch (type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.MentionTypeUtil;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.bag.HashBag;
Expand Down Expand Up @@ -224,14 +225,12 @@ public Bag<SlashCommandReference> getSlashCommandsBag()

@Nonnull
@Override
@SuppressWarnings("ConstantConditions")
public List<IMentionable> getMentions(@Nonnull Message.MentionType... types)
public List<IMentionable> getMentions(@Nonnull Collection<Message.MentionType> types)
{
if (types == null || types.length == 0)
return getMentions(Message.MentionType.values());
Checks.notNull(types, "Mention Types");

List<IMentionable> mentions = new ArrayList<>();
// Conversion to set to prevent duplication of types
for (Message.MentionType type : EnumSet.of(types[0], types))
for (Message.MentionType type : MentionTypeUtil.getEffectiveMentionTypes(types))
{
switch (type)
{
Expand Down Expand Up @@ -267,12 +266,11 @@ public List<IMentionable> getMentions(@Nonnull Message.MentionType... types)
}

@Override
public boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Message.MentionType... types)
public boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull Collection<Message.MentionType> types)
{
Checks.notNull(types, "Mention Types");
if (types.length == 0)
return isMentioned(mentionable, Message.MentionType.values());
for (Message.MentionType type : types)

for (Message.MentionType type : MentionTypeUtil.getEffectiveMentionTypes(types))
{
switch (type)
{
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/MentionTypeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.internal.utils;

import net.dv8tion.jda.api.entities.Message.MentionType;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;

public final class MentionTypeUtil
{
@Nonnull
public static Set<MentionType> getEffectiveMentionTypes(@Nonnull Collection<MentionType> types)
{
if (types.isEmpty())
return EnumSet.allOf(MentionType.class);

return types instanceof Set
? (Set<MentionType>) types
: EnumSet.copyOf(types);
}
}
Loading