All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sx.blah.discord.handle.audit.entry.change.ChangeMap Maven / Gradle / Ivy

Go to download

A Java binding for the official Discord API, forked from the inactive https://github.com/nerd/Discord4J. Copyright (c) 2017, Licensed under GNU LGPLv3

The newest version!
/*
 *     This file is part of Discord4J.
 *
 *     Discord4J is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Discord4J is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with Discord4J.  If not, see .
 */

package sx.blah.discord.handle.audit.entry.change;

import sx.blah.discord.api.internal.json.objects.audit.AuditLogChangeObject;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * A map of {@link ChangeKey ChangeKeys} to {@link AuditLogChange AuditLogChanges}.
 *
 * 

This is a utility to ensure type-safe access of audit log changes. As long as only * {@link Collector} is used to construct the backing map, and only the pre-defined {@link ChangeKey ChangeKeys} are * used, type-safe access is guaranteed. */ @SuppressWarnings("unchecked") public class ChangeMap { private final Map, AuditLogChange> backing; public ChangeMap() { this(new HashMap<>()); } private ChangeMap(Map, AuditLogChange> backing) { this.backing = backing; } public AuditLogChange get(ChangeKey key) { return (AuditLogChange) backing.get(key); } /** * A collector to build a {@link ChangeMap} from a stream of {@link AuditLogChangeObject AuditLogChangeObjects}. */ public static class Collector implements java.util.stream.Collector, AuditLogChange>, ChangeMap> { private static final BinaryOperator> mergeFunction = (u, v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }; private Collector() { } @Override public Supplier, AuditLogChange>> supplier() { return HashMap::new; } @Override public BiConsumer, AuditLogChange>, AuditLogChangeObject> accumulator() { return (map, change) -> map.merge( ChangeKey.fromRaw(change.key), new AuditLogChange<>(change.old_value, change.new_value), mergeFunction); } @Override public BinaryOperator, AuditLogChange>> combiner() { return (m1, m2) -> { for (Map.Entry, AuditLogChange> e : m2.entrySet()) { m1.merge(e.getKey(), e.getValue(), mergeFunction); } return m1; }; } @Override public Function, AuditLogChange>, ChangeMap> finisher() { return ChangeMap::new; } @Override public Set characteristics() { return EnumSet.of(Characteristics.UNORDERED); } public static Collector toChangeMap() { return new Collector(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy