Java Code Examples for gnu.trove.map.TLongObjectMap#get()

The following examples show how to use gnu.trove.map.TLongObjectMap#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemberChunkManager.java    From JDA with Apache License 2.0 6 votes vote down vote up
private List<Member> toMembers(DataObject chunk)
{
    EntityBuilder builder = guild.getJDA().getEntityBuilder();
    DataArray memberArray = chunk.getArray("members");
    TLongObjectMap<DataObject> presences = chunk.optArray("presences").map(it ->
        builder.convertToUserMap(o -> o.getObject("user").getUnsignedLong("id"), it)
    ).orElseGet(TLongObjectHashMap::new);
    List<Member> collect = new ArrayList<>(memberArray.length());
    for (int i = 0; i < memberArray.length(); i++)
    {
        DataObject json = memberArray.getObject(i);
        long userId = json.getObject("user").getUnsignedLong("id");
        DataObject presence = presences.get(userId);
        MemberImpl member = builder.createMember(guild, json, null, presence);
        builder.updateMemberCache(member);
        collect.add(member);
    }
    return collect;
}
 
Example 2
Source File: CommitCanvasN5.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private static <T> T computeIfAbsent(final TLongObjectMap<T> map, final long key, final Supplier<T> fallback)
{
	T t = map.get(key);
	if (t == null)
	{
		t = fallback.get();
		map.put(key, t);
	}
	return t;
}
 
Example 3
Source File: LatestExchangeRates.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ExchangeRate getRate(Currency fromCurrency, Currency toCurrency) {
    if (fromCurrency.id == toCurrency.id) {
        return ExchangeRate.ONE;
    }
    TLongObjectMap<ExchangeRate> rateMap = getMapFor(fromCurrency.id);
    ExchangeRate rate = rateMap.get(toCurrency.id);
    if (rate == null) {
        rate = ExchangeRate.NA;
        rateMap.put(toCurrency.id, rate);
    }
    return rate;
}
 
Example 4
Source File: HistoryExchangeRates.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
private SortedSet<ExchangeRate> getSetFor(TLongObjectMap<SortedSet<ExchangeRate>> rates, long date) {
    SortedSet<ExchangeRate> s = rates.get(date);
    if (s == null) {
        s = new TreeSet<ExchangeRate>();
        rates.put(date, s);
    }
    return s;
}
 
Example 5
Source File: GuildListener.java    From SkyBot with GNU Affero General Public License v3.0 5 votes vote down vote up
private void handleVcAutoRole(Guild guild, Member member, VoiceChannel channel, boolean remove) {
    final Member self = guild.getSelfMember();
    final long guildId = guild.getIdLong();

    final TLongObjectMap<TLongLongMap> vcAutoRoleCache = variables.getVcAutoRoleCache();

    if (!vcAutoRoleCache.containsKey(guildId)) {
        return;
    }

    final TLongLongMap vcToRolePair = vcAutoRoleCache.get(guildId);

    if (vcToRolePair.get(channel.getIdLong()) > 0) {
        final Role role = guild.getRoleById(vcToRolePair.get(channel.getIdLong()));

        if (role != null && self.canInteract(member) && self.canInteract(role) && self.hasPermission(Permission.MANAGE_ROLES)) {
            if (remove) {
                guild
                    .removeRoleFromMember(member, role)
                    .reason("VC auto role removed")
                    .queue();
            } else {
                guild
                    .addRoleToMember(member, role)
                    .reason("VC auto role applied")
                    .queue();
            }
        }
    }
}
 
Example 6
Source File: GuildMembersChunkHandler.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Override
protected Long handleInternally(DataObject content)
{
    final long guildId = content.getLong("guild_id");
    DataArray members = content.getArray("members");
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild != null)
    {
        if (api.getClient().getChunkManager().handleChunk(guildId, content))
            return null;
        WebSocketClient.LOG.debug("Received member chunk for guild that is already in cache. GuildId: {} Count: {} Index: {}/{}",
                guildId, members.length(), content.getInt("chunk_index"), content.getInt("chunk_count"));
        // Chunk handling
        EntityBuilder builder = getJDA().getEntityBuilder();
        TLongObjectMap<DataObject> presences = content.optArray("presences").map(it ->
            builder.convertToUserMap(o -> o.getObject("user").getUnsignedLong("id"), it)
        ).orElseGet(TLongObjectHashMap::new);
        for (int i = 0; i < members.length(); i++)
        {
            DataObject object = members.getObject(i);
            long userId = object.getObject("user").getUnsignedLong("id");
            DataObject presence = presences.get(userId);
            MemberImpl member = builder.createMember(guild, object, null, presence);
            builder.updateMemberCache(member);
        }
        return null;
    }
    getJDA().getGuildSetupController().onMemberChunk(guildId, content);
    return null;
}
 
Example 7
Source File: EventCache.java    From JDA with Apache License 2.0 5 votes vote down vote up
public synchronized void cache(Type type, long triggerId, long responseTotal, DataObject event, CacheConsumer handler)
{
    TLongObjectMap<List<CacheNode>> triggerCache =
            eventCache.computeIfAbsent(type, k -> new TLongObjectHashMap<>());

    List<CacheNode> items = triggerCache.get(triggerId);
    if (items == null)
    {
        items = new LinkedList<>();
        triggerCache.put(triggerId, items);
    }

    items.add(new CacheNode(responseTotal, event, handler));
}
 
Example 8
Source File: GuildSetupNode.java    From JDA with Apache License 2.0 4 votes vote down vote up
private void updateAudioManagerReference(GuildImpl guild)
{
    JDAImpl api = getController().getJDA();
    AbstractCacheView<AudioManager> managerView = api.getAudioManagersView();
    try (UnlockHook hook = managerView.writeLock())
    {
        TLongObjectMap<AudioManager> audioManagerMap = managerView.getMap();
        AudioManagerImpl mng = (AudioManagerImpl) audioManagerMap.get(id);
        if (mng == null)
            return;
        ConnectionListener listener = mng.getConnectionListener();
        final AudioManagerImpl newMng = new AudioManagerImpl(guild);
        newMng.setSelfMuted(mng.isSelfMuted());
        newMng.setSelfDeafened(mng.isSelfDeafened());
        newMng.setQueueTimeout(mng.getConnectTimeout());
        newMng.setSendingHandler(mng.getSendingHandler());
        newMng.setReceivingHandler(mng.getReceivingHandler());
        newMng.setConnectionListener(listener);
        newMng.setAutoReconnect(mng.isAutoReconnect());

        if (mng.isConnected())
        {
            final long channelId = mng.getConnectedChannel().getIdLong();

            final VoiceChannel channel = api.getVoiceChannelById(channelId);
            if (channel != null)
            {
                if (mng.isConnected())
                    mng.closeAudioConnection(ConnectionStatus.ERROR_CANNOT_RESUME);
            }
            else
            {
                //The voice channel is not cached. It was probably deleted.
                api.getClient().removeAudioConnection(id);
                if (listener != null)
                    listener.onStatusChange(ConnectionStatus.DISCONNECTED_CHANNEL_DELETED);
            }
        }
        audioManagerMap.put(id, newMng);
    }
}