<commit_before>/*
 * #%L
 * lb-core
 * %%
 * Copyright (C) 2013 - 2015 <NAME>
 * %%
 * 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.
 * #L%
 */
package com.garethahealy.camel.file.loadbalancer.filter;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultPriorityFileFilterFactory implements PriorityFileFilterFactory {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultPriorityFileFilterFactory.class);

    private int amountOfWatchers;
    private AtomicInteger counter = new AtomicInteger(0);
    private ConcurrentMap<Integer, PriorityFileFilter> holder;

    public DefaultPriorityFileFilterFactory(int amountOfWatchers) {
        this.amountOfWatchers = amountOfWatchers;
    }

    @Override
    public void init() {
        if (amountOfWatchers <= 0) {
            throw new IllegalArgumentException("AmountOfWatchers is less/equal to 0. Must be positive");
        }

        if (holder == null) {
            holder = new ConcurrentHashMap<Integer, PriorityFileFilter>();
        }

        holder.clear();
        counter.set(0);

        for (int i = 0; i < amountOfWatchers; i++) {
            holder.put(i, new PriorityFileFilter(i));
        }

        LOG.info("Created a holder of '{}' as amountOfWatchers is '{}'", holder.size(), amountOfWatchers);
    }

    @Override
    public PriorityFileFilter get() {
        if (holder == null || holder.size() <= 0) {
            throw new IllegalArgumentException("Have you called init?, because holder is null");
        }

        if (counter.get() >= holder.size()) {
            throw new IllegalArgumentException("Get called too many times. Counter is '"
                                               + counter.get() + "' but holder only has '" + holder.size()
                                               + "' with amount of watchers as '" + amountOfWatchers + "'");
        }

        return holder.get(counter.getAndIncrement());
    }

    @Override
    public int getAmountOfWatchers() {
        return amountOfWatchers;
    }

    @Override
    public void resetCountersOnFilters() {
        if (holder != null) {
            for (PriorityFileFilter current : holder.values()) {
                current.resetCounter();
            }
        }
    }
}<commit_msg>Changed methods to be synchronized, so that if its called at same time, we don't hit issues<commit_after>/*
 * #%L
 * lb-core
 * %%
 * Copyright (C) 2013 - 2015 <NAME>
 * %%
 * 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.
 * #L%
 */
package com.garethahealy.camel.file.loadbalancer.filter;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultPriorityFileFilterFactory implements PriorityFileFilterFactory {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultPriorityFileFilterFactory.class);

    private int amountOfWatchers;
    private AtomicInteger counter = new AtomicInteger(0);
    private ConcurrentMap<Integer, PriorityFileFilter> holder;

    public DefaultPriorityFileFilterFactory(int amountOfWatchers) {
        this.amountOfWatchers = amountOfWatchers;
    }

    @Override
    public synchronized void init() {
        if (amountOfWatchers <= 0) {
            throw new IllegalArgumentException("AmountOfWatchers is less/equal to 0. Must be positive");
        }

        if (holder == null) {
            holder = new ConcurrentHashMap<Integer, PriorityFileFilter>();
        }

        holder.clear();
        counter.set(0);

        for (int i = 0; i < amountOfWatchers; i++) {
            holder.put(i, new PriorityFileFilter(i));
        }

        LOG.info("Created a holder of '{}' as amountOfWatchers is '{}'", holder.size(), amountOfWatchers);
    }

    @Override
    public synchronized PriorityFileFilter get() {
        if (holder == null || holder.size() <= 0) {
            throw new IllegalArgumentException("Have you called init?, because holder is null");
        }

        if (counter.get() >= holder.size()) {
            throw new IllegalArgumentException("Get called too many times. Counter is '"
                                               + counter.get() + "' but holder only has '" + holder.size()
                                               + "' with amount of watchers as '" + amountOfWatchers + "'");
        }

        return holder.get(counter.getAndIncrement());
    }

    @Override
    public synchronized int getAmountOfWatchers() {
        return amountOfWatchers;
    }

    @Override
    public synchronized void resetCountersOnFilters() {
        if (holder != null) {
            for (PriorityFileFilter current : holder.values()) {
                current.resetCounter();
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        return EqualsBuilder.reflectionEquals(this, o);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
}
{'fraction_non_alphanumeric': 0.0967741935483871, 'fraction_numerical': 0.006733479486376448, 'mean_word_length': 2.6833910034602075, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '146264', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1622, 'n_words': 510}