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

ru.cwcode.commands.extra.ExtraCommandBuilder Maven / Gradle / Ivy

package ru.cwcode.commands.extra;

import ru.cwcode.commands.Command;
import ru.cwcode.commands.api.Sender;
import ru.cwcode.commands.extra.builder.CommandBuilder;
import ru.cwcode.commands.extra.builder.CreateCommandBuilder;
import ru.cwcode.commands.extra.builder.DeleteCommandBuilder;
import ru.cwcode.commands.extra.builder.list.ListCommandBuilder;
import ru.cwcode.commands.extra.builder.update.UpdateCommandBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

public class ExtraCommandBuilder {
  
  private final RepositoryAccessor repositoryAccessor;
  private final List> builders = new ArrayList<>();
  
  public ExtraCommandBuilder(RepositoryAccessor repositoryAccessor) {
    this.repositoryAccessor = repositoryAccessor;
  }
  
  public static  ExtraCommandBuilder of(BiFunction elementToKey,
                                                                           BiFunction keyToElement,
                                                                           Function> keys) {
    
    return new ExtraCommandBuilder<>(
       new RepositoryAccessor<>(elementToKey, keyToElement, s -> s, s -> s, keys)
    );
  }
  
  public static  ExtraCommandBuilder of(BiFunction elementToKey,
                                                                         BiFunction keyToElement,
                                                                         Function keyToString,
                                                                         Function stringToKey,
                                                                         Function> keys) {
    
    return new ExtraCommandBuilder<>(
       new RepositoryAccessor<>(elementToKey, keyToElement, keyToString, stringToKey, keys)
    );
  }
  
  public >
  ExtraCommandBuilder builder(Function, B> builderCreator, Consumer builder) {
    
    B b = builderCreator.apply(repositoryAccessor);
    
    builder.accept(b);
    builders.add(b);
    
    return this;
  }
  
  public ExtraCommandBuilder delete(Consumer> builder) {
    return builder(DeleteCommandBuilder::new, builder);
  }
  
  public ExtraCommandBuilder update(Consumer> builder) {
    return builder(UpdateCommandBuilder::new, builder);
  }
  
  public ExtraCommandBuilder create(Consumer> builder) {
    return builder(CreateCommandBuilder::new, builder);
  }
  
  public ExtraCommandBuilder list(Consumer> builder) {
    return builder(ListCommandBuilder::new, builder);
  }
  
  public Command build(String commandName) {
    Command command = new Command(commandName);
    
    builders.forEach(builder -> builder.register(command));
    
    return command;
  }
}