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

me.redtea.carcadex.repo.builder.impl.RepoBuilderImpl Maven / Gradle / Ivy

There is a newer version: 2.0.1-BETA
Show newest version
package me.redtea.carcadex.repo.builder.impl;

import me.redtea.carcadex.repo.MutableRepo;
import me.redtea.carcadex.repo.builder.exception.NotConfiguredException;
import me.redtea.carcadex.repo.builder.RepoBuilder;
import me.redtea.carcadex.repo.decorator.CacheRepoDecorator;
import me.redtea.carcadex.repo.decorator.impl.*;
import me.redtea.carcadex.repo.impl.CacheRepo;
import me.redtea.carcadex.repo.impl.schema.SchemaRepo;
import me.redtea.carcadex.schema.SchemaStrategy;
import me.redtea.carcadex.schema.file.impl.binary.BinarySchemaStrategy;
import me.redtea.carcadex.schema.file.impl.serialize.SerializeSchemaStrategy;
import me.redtea.carcadex.serializer.CommonSerializer;
import org.bukkit.plugin.Plugin;
import org.slf4j.Logger;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

public class RepoBuilderImpl implements RepoBuilder  {
    private final EmptyMutableDecorator rootDecorator = new EmptyMutableDecorator<>();
    private CacheRepoDecorator lastDecorator = rootDecorator;

    /*
    FOLDER CONFIGURE
     */
    protected File folder;
    private String filename;

    @Override
    public RepoBuilder folder(String filename) {
        if(plugin != null) plugin(plugin);
        this.filename = filename;
        return this;
    }

    private Plugin plugin;
    @Override
    public RepoBuilder plugin(Plugin plugin) {
        this.plugin = plugin;
        if(folder != null) return this;
        if(filename == null) filename = "carcadex-data-folder";
        folder(new File(plugin.getDataFolder(), filename));
        return this;
    }

    @Override
    public RepoBuilder folder(File folder) {
        this.folder = folder;
        return this;
    }

    /*
    SCHEMA CONFIGURE
     */

    private SchemaStrategy schema;

    @Override
    public RepoBuilder serializer(CommonSerializer serializer) {
        if(folder == null) throw new NotConfiguredException("Before set serializer you must configure folder!");
        schema = new SerializeSchemaStrategy<>(folder, serializer);
        return this;
    }

    @Override
    public RepoBuilder binary() {
        if(folder == null) throw new NotConfiguredException("Before set binary flag you must configure folder!");
        schema = new BinarySchemaStrategy<>(folder);
        return this;
    }

    @Override
    public RepoBuilder schema(SchemaStrategy schemaStrategy) {
        schema = schemaStrategy;
        return this;
    }

    /*
    DECORATORS
     */

    @Override
    public RepoBuilder logging() {
        lastDecorator = new LoggingDecorator<>(lastDecorator);
        return this;
    }

    @Override
    public RepoBuilder debugLogging() {
        lastDecorator = new LoggingDecorator<>(lastDecorator, true);
        return this;
    }

    @Override
    public RepoBuilder logging(Logger logger) {
        lastDecorator = new LoggingDecorator<>(lastDecorator, logger, false);
        return this;
    }

    @Override
    public RepoBuilder debugLogging(Logger logger) {
        lastDecorator = new LoggingDecorator<>(lastDecorator, logger, true);
        return this;
    }

    @Override
    public RepoBuilder autoSave(long period) {
        lastDecorator = new AutoSaveDecorator<>(lastDecorator, 0, period);
        return this;
    }

    @Override
    public RepoBuilder sync() {
        lastDecorator = new SynchronizedDecorator<>(lastDecorator);
        return this;
    }

    @Override
    public RepoBuilder cacheCollectionAll() {
        lastDecorator = new CacheAllDecorator<>(lastDecorator);
        return this;
    }

    /*
    FLAGS
     */
    private boolean concurrent = false;

    @Override
    public RepoBuilder concurrent() {
        concurrent = true;
        return this;
    }

    /*
    BUILD
     */

    @Override
    public MutableRepo build() {
        if(schema == null) throw new NotConfiguredException("You must set schema!");
        CacheRepo result = new SchemaRepo<>(schema, concurrent ? new ConcurrentHashMap<>() : new HashMap<>());
        rootDecorator.setRepo(result);
        return rootDecorator;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy