<commit_before>/*
 * Copyright 2017 BlackSource, LLC.
 *
 * 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 tech.blacksource.blacknectar.service;

import com.google.common.collect.Queues;
import com.google.common.io.Resources;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Objects;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import sir.wellington.alchemy.collections.lists.Lists;
import tech.aroma.client.Aroma;
import tech.aroma.client.Urgency;
import tech.blacksource.blacknectar.service.data.SQLQueries;
import tech.blacksource.blacknectar.service.images.ImageLoader;
import tech.blacksource.blacknectar.service.stores.Store;
import tech.sirwellington.alchemy.annotations.arguments.NonEmpty;
import tech.sirwellington.alchemy.annotations.arguments.Positive;
import tech.sirwellington.alchemy.annotations.arguments.Required;
import tech.sirwellington.alchemy.annotations.designs.patterns.BuilderPattern;

import static java.util.Objects.isNull;
import static tech.sirwellington.alchemy.annotations.designs.patterns.BuilderPattern.Role.CLIENT;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.CollectionAssertions.nonEmptyCollection;
import static tech.sirwellington.alchemy.arguments.assertions.NumberAssertions.positiveInteger;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;

/**
 * Designed to find images for a store and load them into "Store Images" Database.
 * <p>
 * To use this class, create {@linkplain Arguments Arguments} using
 * {@linkplain Arguments.Builder#newInstance() an argument builder}.
 *
 * @author SirWellington
 */
@BuilderPattern(role = CLIENT)
class RunLoadImages implements Consumer<RunLoadImages.Arguments>
{

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

    private final Aroma aroma;
    private final JdbcTemplate database;

    @Inject
    RunLoadImages(Aroma aroma, JdbcTemplate database)
    {
        checkThat(aroma, database)
            .are(notNull());
        
        this.aroma = aroma;
        this.database = database;
    }

    @Override
    public void accept(RunLoadImages.Arguments args)
    {
        checkThat(args);

        long startTime = System.currentTimeMillis();
        long sleepTime = args.timeUnit.toMillis(args.frequency);
        int totalStores = args.stores.size();
        int totalStoresProcessed = 0;
        int totalSuccesses = 0;

        while (Lists.notEmpty(args.stores))
        {
            Store nextStore = args.stores.poll();

            if (isNull(nextStore))
            {
                break;
            }

            boolean success = tryToProcessStore(nextStore, args);

            totalStoresProcessed += 1;

            if (success)
            {
                totalSuccesses += 1;
                makeNoteOfCompletion(args, nextStore, totalSuccesses, totalStoresProcessed, totalStores);
            }<commit_msg>#18 - Adds argument checks<commit_after>/*
 * Copyright 2017 BlackSource, LLC.
 *
 * 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 tech.blacksource.blacknectar.service;

import com.google.common.collect.Queues;
import com.google.common.io.Resources;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Objects;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import sir.wellington.alchemy.collections.lists.Lists;
import tech.aroma.client.Aroma;
import tech.aroma.client.Urgency;
import tech.blacksource.blacknectar.service.data.SQLQueries;
import tech.blacksource.blacknectar.service.images.ImageLoader;
import tech.blacksource.blacknectar.service.stores.Store;
import tech.sirwellington.alchemy.annotations.arguments.NonEmpty;
import tech.sirwellington.alchemy.annotations.arguments.Positive;
import tech.sirwellington.alchemy.annotations.arguments.Required;
import tech.sirwellington.alchemy.annotations.designs.patterns.BuilderPattern;

import static java.util.Objects.isNull;
import static tech.sirwellington.alchemy.annotations.designs.patterns.BuilderPattern.Role.CLIENT;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.CollectionAssertions.nonEmptyCollection;
import static tech.sirwellington.alchemy.arguments.assertions.NumberAssertions.positiveInteger;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;

/**
 * Designed to find images for a store and load them into "Store Images" Database.
 * <p>
 * To use this class, create {@linkplain Arguments Arguments} using
 * {@linkplain Arguments.Builder#newInstance() an argument builder}.
 *
 * @author SirWellington
 */
@BuilderPattern(role = CLIENT)
class RunLoadImages implements Consumer<RunLoadImages.Arguments>
{

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

    private final Aroma aroma;
    private final JdbcTemplate database;

    @Inject
    RunLoadImages(Aroma aroma, JdbcTemplate database)
    {
        checkThat(aroma, database)
            .are(notNull());
        
        this.aroma = aroma;
        this.database = database;
    }

    @Override
    public void accept(RunLoadImages.Arguments args)
    {
        checkThat(args).is(notNull());

        long startTime = System.currentTimeMillis();
        long sleepTime = args.timeUnit.toMillis(args.frequency);
        int totalStores = args.stores.size();
        int totalStoresProcessed = 0;
        int totalSuccesses = 0;

        while (Lists.notEmpty(args.stores))
        {
            Store nextStore = args.stores.poll();

            if (isNull(nextStore))
            {
                break;
            }

            boolean success = tryToProcessStore(nextStore, args);

            totalStoresProcessed += 1;

            if (success)
            {
                totalSuccesses += 1;
                makeNoteOfCompletion(args, nextStore, totalSuccesses, totalStoresProcessed, totalStores);
            }
{'fraction_non_alphanumeric': 0.08968105065666042, 'fraction_numerical': 0.00375234521575985, 'mean_word_length': 3.8024024024024023, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4231239', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2216, 'n_words': 541}