001/*
002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de)
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 de.cuioss.test.generator.internal.net.java.quickcheck.characteristic;
017
018import de.cuioss.test.generator.internal.net.java.quickcheck.Characteristic;
019import lombok.Getter;
020
021/**
022 * {@link AbstractCharacteristic} is an implementation of {@link Characteristic}
023 * with {@link Classification} handling. The methods
024 * {@link AbstractCharacteristic#classify(Object)} and
025 * {@link AbstractCharacteristic#classify(boolean, Object)} can be used to
026 * classify test data.
027 *
028 * @param <T> Type of generated random test instances.
029 */
030@SuppressWarnings("ProhibitedExceptionDeclared")
031public abstract class AbstractCharacteristic<T> implements Characteristic<T> {
032
033    @Getter
034    private final Classification classification = new Classification();
035    private final String name;
036
037    protected AbstractCharacteristic() {
038        this(null);
039    }
040
041    protected AbstractCharacteristic(String name) {
042        this.name = name;
043    }
044
045    /**
046     * {@inheritDoc}
047     *
048     * This method will call {@link AbstractCharacteristic#doSpecify(Object)}.
049     */
050    @Override
051    public void specify(T any) throws Throwable {
052        doSpecify(any);
053        this.classification.call();
054    }
055
056    /**
057     * Add a classification with the given key if the predicate is true.
058     *
059     * @param predicate      Predicate for the classification.
060     * @param classification classification key.
061     */
062    protected void classify(boolean predicate, Object classification) {
063        this.classification.doClassify(predicate, classification);
064    }
065
066    /**
067     * Add a classification with the given key.
068     *
069     * @param classification classification key.
070     */
071    protected void classify(Object classification) {
072        classify(true, classification);
073    }
074
075    /**
076     * Implement this method to specify the characteristic
077     * ({@link Characteristic#specify(Object)}).
078     */
079    protected abstract void doSpecify(T any) throws Throwable;
080
081    @Override
082    public void setUp() throws Exception {
083    }
084
085    @Override
086    public void tearDown() throws Exception {
087    }
088
089    @Override
090    public String name() {
091        return name;
092    }
093}