001/*
002 * Copyright (C) 2022 - 2023, the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.github.ascopes.jct.filemanagers.config;
017
018import io.github.ascopes.jct.filemanagers.JctFileManager;
019import java.util.Deque;
020import java.util.LinkedList;
021import java.util.List;
022import org.apiguardian.api.API;
023import org.apiguardian.api.API.Status;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * A chain of configurers to apply to a file manager.
029 *
030 * <p>Note that modifications to instances of this class are not
031 * threadsafe (e.g. adding new configurers to the chain). Invoking
032 * the configurer chain is also not threadsafe by design.
033 *
034 * @author Ashley Scopes
035 * @since 0.0.1 (0.0.1-M7)
036 */
037@API(since = "0.0.1", status = Status.STABLE)
038public final class JctFileManagerConfigurerChain {
039
040  private static final Logger LOGGER = LoggerFactory.getLogger(JctFileManagerConfigurerChain.class);
041
042  private final Deque<JctFileManagerConfigurer> configurers;
043
044  /**
045   * Initialise this chain.
046   */
047  public JctFileManagerConfigurerChain() {
048    configurers = new LinkedList<>();
049  }
050
051  /**
052   * Add a configurer to the start of the chain.
053   *
054   * @param configurer the configurer to add.
055   * @return this chain for further calls.
056   */
057  public JctFileManagerConfigurerChain addFirst(JctFileManagerConfigurer configurer) {
058    configurers.addFirst(configurer);
059    return this;
060  }
061
062  /**
063   * Add a configurer to the end of the chain.
064   *
065   * @param configurer the configurer to add.
066   * @return this chain for further calls.
067   */
068  public JctFileManagerConfigurerChain addLast(JctFileManagerConfigurer configurer) {
069    configurers.addLast(configurer);
070    return this;
071  }
072
073  /**
074   * Get an immutable copy of the list of configurers.
075   *
076   * @return the list of configurers.
077   */
078  public List<JctFileManagerConfigurer> list() {
079    return List.copyOf(configurers);
080  }
081
082  /**
083   * Apply each configurer to the given file manager in order.
084   *
085   * @param fileManager the file manager to configure.
086   * @return the configured file manager to use. This may or may not be the same object as the input
087   *     parameter, depending on how the configurers manipulate the input object.
088   */
089  public JctFileManager configure(JctFileManager fileManager) {
090    for (var configurer : configurers) {
091      if (configurer.isEnabled()) {
092        LOGGER.trace("Applying {} to file manager", configurer);
093        fileManager = configurer.configure(fileManager);
094      } else {
095        LOGGER.trace("Skipping {}", configurer);
096      }
097    }
098
099    return fileManager;
100  }
101}