sx.blah.discord.handle.audit.entry.change.ChangeMap Maven / Gradle / Ivy
Show all versions of Discord4J Show documentation
/*
* 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