Skip to content

Instantly share code, notes, and snippets.

@zml2008
Created October 3, 2020 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zml2008/5f6de6484eed5b9d8451c5e1625f8572 to your computer and use it in GitHub Desktop.
Save zml2008/5f6de6484eed5b9d8451c5e1625f8572 to your computer and use it in GitHub Desktop.
package org.spongepowered.configurate.objectmapping;
import static java.util.Objects.requireNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.objectmapping.meta.NodeKey;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class FullRecordTest {
/**
* A configuration for a chat plugin.
*
* @param name Server name
* @param serverId Unique id to represent the server
* @param channels Chat channels to configure
*/
public record Config(@Nullable String name, UUID serverId, Set<ChannelDefinition> channels) {
public static final ObjectMapper<Config> MAPPER;
public Config {
if (name == null) {
name = "<unknown>";
}
channels = Set.copyOf(channels);
}
static {
try {
MAPPER = ObjectMapper.factory().get(Config.class);
} catch (final ObjectMappingException ex) {
throw new ExceptionInInitializerError();
}
}
public static Config loadFrom(final ConfigurationNode node) throws ObjectMappingException {
return MAPPER.load(node);
}
public Config name(final String name) {
requireNonNull(name);
return new Config(name, this.serverId, this.channels);
}
public Config serverId(final UUID serverId) {
requireNonNull(serverId);
return new Config(this.name, serverId, this.channels);
}
public Config channels(final Set<ChannelDefinition> channels) {
requireNonNull(channels);
return new Config(this.name, this.serverId, channels);
}
/**
* Save the data in this object to a node.
*
* @param node node to save to
* @throws ObjectMappingException if any data is invalid
*/
public void save(final ConfigurationNode node) throws ObjectMappingException {
MAPPER.save(this, node);
}
}
public record ChannelDefinition(@NodeKey String name, Map<String, String> templates) {
public ChannelDefinition {
templates = Map.copyOf(templates);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment