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.junit;
017
018import io.github.ascopes.jct.compilers.JctCompiler;
019import org.apiguardian.api.API;
020import org.apiguardian.api.API.Status;
021
022/**
023 * Strategy for setting a version on a JUnit compiler annotation.
024 *
025 * @author Ashley Scopes
026 * @since 0.0.1
027 */
028@API(since = "0.0.1", status = Status.STABLE)
029public enum VersionStrategy {
030
031  /**
032   * Set the {@link JctCompiler#release release}.
033   */
034  RELEASE(
035      (compiler, version) -> compiler
036          .release(version)
037          .name(compiler.getName() + " (release = Java " + version + ")")
038  ),
039
040  /**
041   * Set the {@link JctCompiler#source source}.
042   */
043  SOURCE(
044      (compiler, version) -> compiler
045          .source(version)
046          .name(compiler.getName() + " (source = Java " + version + ")")
047  ),
048
049  /**
050   * Set the {@link JctCompiler#target target}.
051   */
052  TARGET(
053      (compiler, version) -> compiler
054          .target(version)
055          .name(compiler.getName() + " (target = Java " + version + ")")
056  ),
057
058  /**
059   * Set the {@link JctCompiler#source source} and {@link JctCompiler#target target}.
060   */
061  SOURCE_AND_TARGET(
062      (compiler, version) -> compiler
063          .source(version)
064          .target(version)
065          .name(compiler.getName() + " (source and target = Java " + version + ")")
066  );
067
068  private final VersionConfigurer versionConfigurer;
069
070  VersionStrategy(VersionConfigurer versionConfigurer) {
071    this.versionConfigurer = versionConfigurer;
072  }
073
074  /**
075   * Set the given version on the compiler, according to the strategy in use.
076   *
077   * @param compiler the compiler to configure.
078   * @param version  the version to set.
079   */
080  public void configureCompiler(JctCompiler compiler, int version) {
081    versionConfigurer.configure(compiler, version);
082  }
083
084  @FunctionalInterface
085  private interface VersionConfigurer {
086    void configure(JctCompiler compiler, int version);
087  }
088}