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