001/* 002 * JDrupes Builder 003 * Copyright (C) 2025 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.builder.core; 020 021import com.google.common.flogger.FluentLogger; 022import java.io.IOException; 023import java.nio.file.Files; 024import java.nio.file.Path; 025import org.jdrupes.builder.api.Cleanliness; 026import org.jdrupes.builder.api.Generator; 027import org.jdrupes.builder.api.Project; 028import org.jdrupes.builder.api.Renamable; 029import org.jdrupes.builder.api.Resource; 030import org.jdrupes.builder.api.ResourceRequest; 031import org.jdrupes.builder.api.ResourceType; 032import static org.jdrupes.builder.api.ResourceType.CleanlinessType; 033 034/// A base implementation of a [Generator]. 035/// 036public abstract class AbstractGenerator extends AbstractProvider 037 implements Generator, Renamable { 038 039 private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 040 private final Project project; 041 042 /// Instantiates a new abstract generator. 043 /// 044 /// @param project the project 045 /// 046 public AbstractGenerator(Project project) { 047 this.project = project; 048 } 049 050 /// Project. 051 /// 052 /// @return the project 053 /// 054 @Override 055 public final Project project() { 056 return project; 057 } 058 059 @Override 060 public AbstractGenerator name(String name) { 061 rename(name); 062 return this; 063 } 064 065 /// Short for `project().newResource(type, args)`. 066 /// 067 /// @param <T> the generic type 068 /// @param type the type 069 /// @param args the args 070 /// @return the t 071 /// 072 @Override 073 protected <T extends Resource> T newResource(ResourceType<T> type, 074 Object... args) { 075 return project.newResource(type, args); 076 } 077 078 /// If the request includes [Cleanliness] deletes the given files 079 /// and returns `true`. 080 /// 081 /// @param requested the requested resource 082 /// @param files the files 083 /// @return true, if successful 084 /// 085 protected boolean cleanup(ResourceRequest<?> requested, Path... files) { 086 if (!requested.accepts(CleanlinessType)) { 087 return false; 088 } 089 for (Path file : files) { 090 try { 091 Files.deleteIfExists(file); 092 } catch (IOException e) { 093 logger.atWarning().log("%s cannot be deleted.", file); 094 } 095 } 096 return true; 097 } 098 099 /// To string. 100 /// 101 /// @return the string 102 /// 103 @Override 104 public String toString() { 105 return name() + " in project " + project().name(); 106 } 107 108}