-
-
Notifications
You must be signed in to change notification settings - Fork 730
/
CategoryImpl.java
116 lines (101 loc) · 3.46 KB
/
CategoryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package net.dv8tion.jda.core.entities.impl;
import gnu.trove.map.TLongObjectMap;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.restaction.InviteAction;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.MiscUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class CategoryImpl extends AbstractChannelImpl<CategoryImpl> implements Category
{
protected final TLongObjectMap<Channel> channels = MiscUtil.newLongMap();
public CategoryImpl(long id, GuildImpl guild)
{
super(id, guild);
}
@Override
public Category getParent()
{
return null;
}
@Override
public int compareTo(Category other)
{
Checks.notNull(other, "Other Category");
if (other.equals(this))
return 0;
Checks.check(getGuild().equals(other.getGuild()), "Cannot compare categories from different guilds!");
if (rawPosition == other.getPositionRaw())
return Long.compare(id, other.getIdLong());
return Integer.compare(rawPosition, other.getPositionRaw());
}
@Override
public ChannelType getType()
{
return ChannelType.CATEGORY;
}
@Override
public List<Member> getMembers()
{
return Collections.unmodifiableList(getChannels().stream()
.map(Channel::getMembers)
.flatMap(List::stream)
.distinct()
.collect(Collectors.toList()));
}
@Override
public int getPosition()
{
//We call getCategories instead of directly accessing the GuildImpl.getCategories because
// getCategories does the sorting logic.
List<Category> channels = guild.getCategories();
for (int i = 0; i < channels.size(); i++)
{
if (channels.get(i) == this)
return i;
}
throw new AssertionError("Somehow when determining position we never found the Category in the Guild's channels? wtf?");
}
@Override
public InviteAction createInvite()
{
throw new UnsupportedOperationException("Cannot create invites for category!");
}
@Override
public RestAction<List<Invite>> getInvites()
{
return new RestAction.EmptyRestAction<>(getJDA(), Collections.emptyList());
}
@Override
public List<Channel> getChannels()
{
List<Channel> channels = new ArrayList<>();
channels.addAll(getTextChannels());
channels.addAll(getVoiceChannels());
return Collections.unmodifiableList(channels);
}
@Override
public List<TextChannel> getTextChannels()
{
return Collections.unmodifiableList(getGuild().getTextChannels().stream()
.filter(channel -> channel.getParent() != null)
.filter(channel -> channel.getParent().equals(this))
.collect(Collectors.toList()));
}
@Override
public List<VoiceChannel> getVoiceChannels()
{
return Collections.unmodifiableList(getGuild().getVoiceChannels().stream()
.filter(channel -> channel.getParent() != null)
.filter(channel -> channel.getParent().equals(this))
.collect(Collectors.toList()));
}
@Override
public String toString()
{
return "GC:" + getName() + '(' + id + ')';
}
}