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 java.io.IOException; 022import java.nio.file.Files; 023import java.nio.file.Path; 024import org.jdrupes.builder.api.Cleanliness; 025import org.jdrupes.builder.api.Generator; 026import org.jdrupes.builder.api.Project; 027import org.jdrupes.builder.api.Resource; 028import org.jdrupes.builder.api.ResourceRequest; 029import org.jdrupes.builder.api.ResourceType; 030import static org.jdrupes.builder.api.ResourceType.CleanlinessType; 031 032// TODO: Auto-generated Javadoc 033/// A base implementation of a [Generator]. 034/// 035public abstract class AbstractGenerator extends AbstractProvider 036 implements Generator { 037 038 private final Project project; 039 private String name; 040 041 /// Instantiates a new abstract generator. 042 /// 043 /// @param project the project 044 /// 045 public AbstractGenerator(Project project) { 046 this.project = project; 047 name = getClass().getSimpleName(); 048 if (name.isBlank()) { 049 name = "Adapted " + getClass().getSuperclass().getSimpleName(); 050 } 051 } 052 053 /// Sets the name of the generator. 054 /// 055 /// @param name the name 056 /// @return the generator 057 /// 058 public AbstractGenerator name(String name) { 059 this.name = name; 060 return this; 061 } 062 063 /// Name. 064 /// 065 /// @return the string 066 /// 067 @Override 068 public String name() { 069 return name; 070 } 071 072 /// Project. 073 /// 074 /// @return the project 075 /// 076 @Override 077 public final Project project() { 078 return project; 079 } 080 081 /// Short for `project().newResource(type, args)`. 082 /// 083 /// @param <T> the generic type 084 /// @param type the type 085 /// @param args the args 086 /// @return the t 087 /// 088 protected <T extends Resource> T newResource(ResourceType<T> type, 089 Object... args) { 090 return project.newResource(type, args); 091 } 092 093 /// If the request includes [Cleanliness] deletes the given files 094 /// and returns `true`. 095 /// 096 /// @param requested the requested resource 097 /// @param files the files 098 /// @return true, if successful 099 /// 100 protected boolean cleanup(ResourceRequest<?> requested, Path... files) { 101 if (!requested.collects(CleanlinessType)) { 102 return false; 103 } 104 for (Path file : files) { 105 try { 106 Files.deleteIfExists(file); 107 } catch (IOException e) { 108 log.warning(() -> file + " cannot be deleted."); 109 } 110 } 111 return true; 112 } 113 114 /// To string. 115 /// 116 /// @return the string 117 /// 118 @Override 119 public String toString() { 120 return name + " in project " + project().name(); 121 } 122 123}