All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.autores.handling.Handler Maven / Gradle / Ivy

There is a newer version: 11.0.35-beta
Show newest version
// Copyright 2023 https://github.com/autores-uk/autores/blob/main/LICENSE.txt
// SPDX-License-Identifier: Apache-2.0
package uk.autores.handling;

import java.util.*;
import java.util.function.Consumer;

import static java.util.stream.Collectors.toMap;

/**
 * Processes resources from the class path.
 * Implementations MUST:
 * 
    *
  • Be public
  • *
  • Have a public no-args constructor
  • *
  • Be available as compiled types on the compiler classpath
  • *
* Implementations inherit the limitations of {@link javax.annotation.processing.Processor}. * That is: *
    *
  1. The result of processing a given input is not a function of the presence or absence of other inputs (orthogonality)
  2. *
  3. Processing the same input produces the same output (consistency)
  4. *
  5. Processing input A followed by processing input B is equivalent to processing B then A (commutativity)
  6. *
  7. Processing an input does not rely on the presence of the output of other annotation processors (independence)
  8. *
* * @see ResourceFiles#handler() */ public interface Handler { /** * Handles the context resources. * * @param context processing context * @throws Exception any exception * @see Context#resources() */ void handle(Context context) throws Exception; /** * The configuration supported by this handler. * * @return empty set by default */ default Set config() { return Collections.emptySet(); } /** *

* Validates the {@link Context#config()}. Override to change behaviour. *

*

* The default implementation returns true if: *

*
    *
  • Key names are unique
  • *
  • Key names match {@link #config()} {@link ConfigDef#key()}
  • *
  • Values pass {@link #config()} {@link ConfigDef#isValid(String)}
  • *
* * @param config handler configuration * @param errorReporter error message consumer * @return true if the config is valid for this handler */ default boolean validConfig(List config, Consumer errorReporter) { Map defs = config() .stream() .collect(toMap(ConfigDef::key, cd -> cd)); Set configured = new HashSet<>(); for (Config option : config) { String key = option.key(); if (configured.contains(key)) { errorReporter.accept("Duplicate config '" + option + "'"); return false; } configured.add(key); ConfigDef def = defs.get(key); if (def == null) { errorReporter.accept("Unknown config '" + option + "'"); return false; } if (!def.isValid(option.value())) { errorReporter.accept("Invalid config '" + option + "'"); return false; } } return true; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy