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.ArrayDeque; 020import java.util.Deque; 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>While not designed for concurrent use, all operations are shielded by a 031 * mutex to guard against potentially confusing behaviour should this component 032 * be shared across multiple threads. 033 * 034 * @author Ashley Scopes 035 * @since 0.0.1 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 Object lock; 043 private final Deque<JctFileManagerConfigurer> configurers; 044 045 /** 046 * Initialise this chain. 047 */ 048 public JctFileManagerConfigurerChain() { 049 lock = new Object(); 050 configurers = new ArrayDeque<>(16); 051 } 052 053 /** 054 * Add a configurer to the start of the chain. 055 * 056 * @param configurer the configurer to add. 057 * @return this chain for further calls. 058 */ 059 public JctFileManagerConfigurerChain addFirst(JctFileManagerConfigurer configurer) { 060 synchronized (lock) { 061 configurers.addFirst(configurer); 062 } 063 return this; 064 } 065 066 /** 067 * Add a configurer to the end of the chain. 068 * 069 * @param configurer the configurer to add. 070 * @return this chain for further calls. 071 */ 072 public JctFileManagerConfigurerChain addLast(JctFileManagerConfigurer configurer) { 073 synchronized (lock) { 074 configurers.addLast(configurer); 075 } 076 return this; 077 } 078 079 /** 080 * Get an immutable copy of the list of configurers. 081 * 082 * @return the list of configurers. 083 */ 084 public List<JctFileManagerConfigurer> list() { 085 synchronized (lock) { 086 return List.copyOf(configurers); 087 } 088 } 089 090 /** 091 * Apply each configurer to the given file manager in order. 092 * 093 * @param fileManager the file manager to configure. 094 * @return the configured file manager to use. This may or may not be the same object as the input 095 * parameter, depending on how the configurers manipulate the input object. 096 */ 097 public JctFileManager configure(JctFileManager fileManager) { 098 synchronized (lock) { 099 for (var configurer : configurers) { 100 if (configurer.isEnabled()) { 101 LOGGER.debug("Applying {} to file manager", configurer); 102 fileManager = configurer.configure(fileManager); 103 } else { 104 LOGGER.trace("Skipping {}", configurer); 105 } 106 } 107 } 108 109 return fileManager; 110 } 111}