001/*
002 * PermissionsEx
003 * Copyright (C) zml and PermissionsEx contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ca.stellardrift.permissionsex.datastore;
018
019import ca.stellardrift.permissionsex.exception.PermissionsLoadingException;
020import org.immutables.value.Value;
021import org.spongepowered.configurate.ConfigurationNode;
022import org.spongepowered.configurate.serialize.SerializationException;
023
024/**
025 * The deserialized configuration options from a data store.
026 *
027 * <p>This can be loaded to a full data store by querying the associated factory.</p>
028 *
029 * @param <C> Type of the store configuration object
030 * @since 2.0.0
031 */
032@Value.Immutable(builder = false)
033public interface ProtoDataStore<C> {
034
035    static <C> ProtoDataStore<C> of(final String identifier, final C config, final DataStoreFactory<C> factory) {
036        return new ProtoDataStoreImpl<>(identifier, config, factory);
037    }
038
039    /**
040     * Identifier for a single data store instance.
041     *
042     * @return store identifier
043     * @since 2.0.0
044     */
045    @Value.Parameter
046    String identifier();
047
048    /**
049     * The object holding data store configuration.
050     *
051     * @return store configuration
052     * @since 2.0.0
053     */
054    @Value.Parameter
055    C config();
056
057    /**
058     * Factory for the type of data store used.
059     *
060     * @return factory instance
061     * @since 2.0.0
062     */
063    @Value.Parameter
064    DataStoreFactory<C> factory();
065
066    /**
067     * Given a data store's properties, resolve a full data store.
068     *
069     * @return the full data store
070     */
071    default DataStore defrost(final DataStoreContext context) throws PermissionsLoadingException {
072        return this.factory().defrost(context, this);
073    }
074
075    default void serialize(final ConfigurationNode node) throws SerializationException {
076        this.factory().serialize(node, this);
077    }
078
079}