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 * @author Ashley Scopes 031 * @since 0.0.1 (0.0.1-M7) 032 */ 033@API(since = "0.0.1", status = Status.STABLE) 034public final class JctFileManagerConfigurerChain { 035 036 private static final Logger LOGGER = LoggerFactory.getLogger(JctFileManagerConfigurerChain.class); 037 038 private final Deque<JctFileManagerConfigurer> configurers; 039 040 /** 041 * Initialise this chain. 042 */ 043 public JctFileManagerConfigurerChain() { 044 configurers = new LinkedList<>(); 045 } 046 047 /** 048 * Add a configurer to the start of the chain. 049 * 050 * @param configurer the configurer to add. 051 * @return this chain for further calls. 052 */ 053 public JctFileManagerConfigurerChain addFirst(JctFileManagerConfigurer configurer) { 054 configurers.addFirst(configurer); 055 return this; 056 } 057 058 /** 059 * Add a configurer to the end of the chain. 060 * 061 * @param configurer the configurer to add. 062 * @return this chain for further calls. 063 */ 064 public JctFileManagerConfigurerChain addLast(JctFileManagerConfigurer configurer) { 065 configurers.addLast(configurer); 066 return this; 067 } 068 069 /** 070 * Get an immutable copy of the list of configurers. 071 * 072 * @return the list of configurers. 073 */ 074 public List<JctFileManagerConfigurer> list() { 075 return List.copyOf(configurers); 076 } 077 078 /** 079 * Apply each configurer to the given file manager in order. 080 * 081 * @param fileManager the file manager to configure. 082 * @return the configured file manager to use. This may or may not be the same object as the input 083 * parameter, depending on how the configurers manipulate the input object. 084 */ 085 public JctFileManager configure(JctFileManager fileManager) { 086 for (var configurer : configurers) { 087 if (configurer.isEnabled()) { 088 LOGGER.trace("Applying {} to file manager", configurer); 089 fileManager = configurer.configure(fileManager); 090 } else { 091 LOGGER.trace("Skipping {}", configurer); 092 } 093 } 094 095 return fileManager; 096 } 097}