<commit_before>/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fusesource.fabric.api.scr;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * An abstract base class for validatable components.
 *
 * @author Thomas.Diesler<EMAIL>
 * @since 13-Sep-2013
 */
public abstract class AbstractComponent implements Validatable {

    private final AtomicBoolean active = new AtomicBoolean();

    public void activateComponent() {
        active.set(true);
    }

    public void deactivateComponent() {
        active.set(false);
    }

    @Override
    public boolean isValid() {
        return active.get();
    }

    @Override
    public void assertValid() {
        if (isValid() == false) {
            RuntimeException rte = new InvalidComponentException();
            rte.printStackTrace();
            throw rte;<commit_msg>Make AbstractComponent use a volatile flag instead of AtomicBoolean<commit_after>/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fusesource.fabric.api.scr;


/**
 * An abstract base class for validatable components.
 * {@link}
 * @author <EMAIL>
 * @since 13-Sep-2013
 */
public abstract class AbstractComponent implements Validatable {

    /* This uses volatile to make sure that every thread sees the last written value
     *
     * - The use of AtomicBoolean would be wrong because it does not guarantee that state written
     *   prior to an unsynchronized call to activateComponent() is also seen
     *
     * - Synchronizing all methods in here would also work, but would effectively cause
     *   a lock acquisition on every public method in every component
     */
    private volatile boolean active;

    public void activateComponent() {
        active = true;
    }

    public void deactivateComponent() {
        active = false;
    }

    @Override
    public boolean isValid() {
        return active;
    }

    @Override
    public void assertValid() {
        if (!active) {
            RuntimeException rte = new InvalidComponentException();
            rte.printStackTrace();
            throw rte;
{'fraction_non_alphanumeric': 0.08070500927643785, 'fraction_numerical': 0.006184291898577613, 'mean_word_length': 3.0086741016109046, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894794', 'n_tokens_mistral': 830, 'n_tokens_neox': 800, 'n_words': 344}