001package ball.game.scrabble;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * %%
006 * Copyright (C) 2010 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.annotation.ServiceProviderFor;
022import ball.annotation.processing.AnnotatedProcessor;
023import ball.annotation.processing.For;
024import javax.annotation.processing.Processor;
025import javax.annotation.processing.RoundEnvironment;
026import javax.lang.model.element.Element;
027import javax.lang.model.element.TypeElement;
028import lombok.NoArgsConstructor;
029import lombok.ToString;
030
031import static java.util.stream.Collectors.toSet;
032import static javax.tools.Diagnostic.Kind.ERROR;
033
034/**
035 * {@link Processor} implementation to check {@link Class}es annotated with
036 * {@link LetterPremium} or {@link WordPremium}:
037 * <ol>
038 *   <li value="1">Are an instance of {@link SQ},</li>
039 *   <li value="2">Concrete, and</li>
040 *   <li value="3">
041 *      Are annotated with only one of {@link LetterPremium} or
042 *      {@link WordPremium}
043 *   </li>
044 * </ol>
045 *
046 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
047 */
048@ServiceProviderFor({ Processor.class })
049@For({ LetterPremium.class, WordPremium.class })
050@NoArgsConstructor @ToString
051public class PremiumProcessor extends AnnotatedProcessor {
052    @Override
053    public void process(RoundEnvironment roundEnv, TypeElement annotation, Element element) {
054        super.process(roundEnv, annotation, element);
055
056        switch (element.getKind()) {
057        case CLASS:
058            var set =
059                getSupportedAnnotationTypeList().stream()
060                .filter(t -> element.getAnnotation(t) != null)
061                .map(t -> asTypeElement(t))
062                .collect(toSet());
063
064            set.remove(annotation);
065
066            if (! set.isEmpty()) {
067                print(ERROR, element,
068                      "%s annotated with @%s but is also annotated with @%s",
069                      element.getKind(), annotation.getSimpleName(), set.iterator().next().getSimpleName());
070            }
071            break;
072
073        default:
074            break;
075        }
076    }
077}