001/*
002 * Copyright (C) 2022 - 2024, 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.LinkedList;
020import java.util.List;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * A collection representing a chain of configurers to apply to a file manager.
026 *
027 * <p>This is used to provide a set of ordered configuration operations to perform on newly
028 * created file managers to prepare them for consumption in test cases. Common operations
029 * may include:
030 *
031 * <ul>
032 *   <li>Automatically configuring the annotation processor paths;</li>
033 *   <li>Creating empty locations to prevent compilers raising exceptions;</li>
034 *   <li>Installing logging interceptors.</li>
035 * </ul>
036 *
037 * <p>When configuring a file manager with this chain, each configurer is invoked in the
038 * provided order. Configurers themselves must return a file manager as a result of the
039 * configure operation. This operation may return the input file manager if the operation
040 * mutated the input, or it may return a different file manager instance. In the latter
041 * case, subsequent configuration operations internally will use the new file manager.
042 * The last returned file manager in the chain will be used as the final returned result.
043 * This enables configurers to wrap file managers in proxies or delegating implementations
044 * to intercept or override existing behaviours.
045 *
046 * <p>This class is not thread-safe.
047 *
048 * @author Ashley Scopes
049 * @since 0.0.1
050 */
051public final class JctFileManagerConfigurerChain {
052
053  private static final Logger log = LoggerFactory.getLogger(JctFileManagerConfigurerChain.class);
054
055  private final LinkedList<JctFileManagerConfigurer> configurers;
056
057  /**
058   * Initialise this chain.
059   */
060  public JctFileManagerConfigurerChain() {
061    configurers = new LinkedList<>();
062  }
063
064  /**
065   * Add a configurer to the start of the chain.
066   *
067   * @param configurer the configurer to add.
068   * @return this chain for further calls.
069   */
070  public JctFileManagerConfigurerChain addFirst(JctFileManagerConfigurer configurer) {
071    configurers.addFirst(configurer);
072    return this;
073  }
074
075  /**
076   * Add a configurer to the end of the chain.
077   *
078   * @param configurer the configurer to add.
079   * @return this chain for further calls.
080   */
081  public JctFileManagerConfigurerChain addLast(JctFileManagerConfigurer configurer) {
082    configurers.addLast(configurer);
083    return this;
084  }
085
086  /**
087   * Get an immutable copy of the list of configurers.
088   *
089   * @return the list of configurers.
090   */
091  public List<JctFileManagerConfigurer> list() {
092    return List.copyOf(configurers);
093  }
094
095  /**
096   * Apply each configurer to the given file manager in order.
097   *
098   * @param fileManager the file manager to configure.
099   * @return the configured file manager to use. This may or may not be the same object as the input
100   *     parameter, depending on how the configurers manipulate the input object.
101   */
102  public JctFileManager configure(JctFileManager fileManager) {
103    for (var configurer : configurers) {
104      if (configurer.isEnabled()) {
105        log.debug("Applying {} to file manager {}", configurer, fileManager);
106        // Configurers can totally replace the existing file manager
107        // if they choose.
108        fileManager = configurer.configure(fileManager);
109      } else {
110        log.trace("Skipping {}", configurer);
111      }
112    }
113    return fileManager;
114  }
115}