<commit_before>/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.server.storage.jdbc.entity.visibility;

import com.google.common.annotations.VisibleForTesting;
import org.spine3.Internal;
import org.spine3.server.entity.Visibility;
import org.spine3.server.storage.VisibilityField;
import org.spine3.server.storage.jdbc.entity.visibility.query.CreateVisibilityTableQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.InsertAndMarkEntityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.InsertVisibilityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.MarkEntityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.SelectVisibilityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.UpdateVisibilityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.table.VisibilityTable;
import org.spine3.server.storage.jdbc.util.DataSourceWrapper;
import org.spine3.server.storage.jdbc.util.IdColumn;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.spine3.server.storage.VisibilityField.archived;
import static org.spine3.server.storage.VisibilityField.deleted;

/**
 * @author <NAME>.
 */
@Internal
class VisibilityHandlingStorageQueryFactoryImpl<I> extends AbstractVisibilityHandlingStorageQueryFactory<I> {

    private final DataSourceWrapper dataSource;
    private final String tableName;
    private final IdColumn<I> idColumn;

    VisibilityHandlingStorageQueryFactoryImpl(DataSourceWrapper dataSource,
                                              String tableName,
                                              IdColumn<I> idColumn) {
        super();
        this.dataSource = checkNotNull(dataSource);
        this.tableName = checkNotNull(tableName);
        this.idColumn = checkNotNull(idColumn);
    }

    @Override
    public CreateVisibilityTableQuery newCreateVisibilityTableQuery() {
        final CreateVisibilityTableQuery.Builder builder =
                CreateVisibilityTableQuery.newBuilder()
                                            .setDataSource(dataSource)
                                            .setLogger(getLogger())
                                            .setTableName(VisibilityTable.TABLE_NAME);
        return builder.build();
    }

    @Override
    public InsertVisibilityQuery newInsertVisibilityQuery(I id, Visibility visibility) {
        final InsertVisibilityQuery.Builder builder =
                InsertVisibilityQuery.newBuilder()
                                       .setDataSource(dataSource)
                                       .setLogger(getLogger())
                                       .setId(id)
                                       .setVisibility(visibility);
        return builder.build();
    }

    @Override
    public SelectVisibilityQuery newSelectVisibilityQuery(I id) {
        final SelectVisibilityQuery.Builder builder =
                SelectVisibilityQuery.newBuilder()
                                       .setDataSource(dataSource)
                                       .setLogger(getLogger())
                                       .setId(id);
        return builder.build();
    }

    @Override
    public UpdateVisibilityQuery newUpdateVisibilityQuery(I id, Visibility visibility) {
        final UpdateVisibilityQuery.Builder builder = UpdateVisibilityQuery.<I>newBuilder()
                .setDataSource(dataSource)
                .setLogger(getLogger())
                .setId(id)
                .setVisibility(visibility);
        return builder.build();
    }

    @Override
    public MarkEntityQuery<I> newMarkArchivedQuery(I id) {
        return newMarkQuery(id, archived);
    }

    @Override
    public MarkEntityQuery<I> newMarkDeletedQuery(I id) {
        return newMarkQuery(id, deleted);
    }

    @Override
    public InsertAndMarkEntityQuery<I> newMarkArchivedNewEntityQuery(I id) {
        return newInsertAndMarkEntityQuery(id, archived);
    }

    @Override
    public InsertAndMarkEntityQuery<I> newMarkDeletedNewEntityQuery(I id) {
        return newInsertAndMarkEntityQuery(id, deleted);
    }

    @VisibleForTesting<commit_msg>Add some javadoc and format code.<commit_after>/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.server.storage.jdbc.entity.visibility;

import com.google.common.annotations.VisibleForTesting;
import org.spine3.Internal;
import org.spine3.server.entity.Visibility;
import org.spine3.server.storage.VisibilityField;
import org.spine3.server.storage.jdbc.entity.visibility.query.CreateVisibilityTableQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.InsertAndMarkEntityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.InsertVisibilityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.MarkEntityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.SelectVisibilityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.query.UpdateVisibilityQuery;
import org.spine3.server.storage.jdbc.entity.visibility.table.VisibilityTable;
import org.spine3.server.storage.jdbc.util.DataSourceWrapper;
import org.spine3.server.storage.jdbc.util.IdColumn;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.spine3.server.storage.VisibilityField.archived;
import static org.spine3.server.storage.VisibilityField.deleted;

/**
 * A default implementation of the {@link VisibilityHandlingStorageQueryFactory}.
 *
 * <p>The factory generates queries to the given table.
 *
 * @author <NAME>.
 * @see VisibilityQueryFactories
 */
@Internal
class VisibilityHandlingStorageQueryFactoryImpl<I> extends AbstractVisibilityHandlingStorageQueryFactory<I> {

    private final DataSourceWrapper dataSource;
    private final String tableName;
    private final IdColumn<I> idColumn;

    VisibilityHandlingStorageQueryFactoryImpl(DataSourceWrapper dataSource,
                                              String tableName,
                                              IdColumn<I> idColumn) {
        super();
        this.dataSource = checkNotNull(dataSource);
        this.tableName = checkNotNull(tableName);
        this.idColumn = checkNotNull(idColumn);
    }

    @Override
    public CreateVisibilityTableQuery newCreateVisibilityTableQuery() {
        final CreateVisibilityTableQuery.Builder builder =
                CreateVisibilityTableQuery.newBuilder()
                                          .setDataSource(dataSource)
                                          .setLogger(getLogger())
                                          .setTableName(VisibilityTable.TABLE_NAME);
        return builder.build();
    }

    @Override
    public InsertVisibilityQuery newInsertVisibilityQuery(I id, Visibility visibility) {
        final InsertVisibilityQuery.Builder builder =
                InsertVisibilityQuery.newBuilder()
                                     .setDataSource(dataSource)
                                     .setLogger(getLogger())
                                     .setId(id)
                                     .setVisibility(visibility);
        return builder.build();
    }

    @Override
    public SelectVisibilityQuery newSelectVisibilityQuery(I id) {
        final SelectVisibilityQuery.Builder builder =
                SelectVisibilityQuery.newBuilder()
                                     .setDataSource(dataSource)
                                     .setLogger(getLogger())
                                     .setId(id);
        return builder.build();
    }

    @Override
    public UpdateVisibilityQuery newUpdateVisibilityQuery(I id, Visibility visibility) {
        final UpdateVisibilityQuery.Builder builder =
                UpdateVisibilityQuery.<I>newBuilder()
                                     .setDataSource(dataSource)
                                     .setLogger(getLogger())
                                     .setId(id)
                                     .setVisibility(visibility);
        return builder.build();
    }

    @Override
    public MarkEntityQuery<I> newMarkArchivedQuery(I id) {
        return newMarkQuery(id, archived);
    }

    @Override
    public MarkEntityQuery<I> newMarkDeletedQuery(I id) {
        return newMarkQuery(id, deleted);
    }

    @Override
    public InsertAndMarkEntityQuery<I> newMarkArchivedNewEntityQuery(I id) {
        return newInsertAndMarkEntityQuery(id, archived);
    }

    @Override
    public InsertAndMarkEntityQuery<I> newMarkDeletedNewEntityQuery(I id) {
        return newInsertAndMarkEntityQuery(id, deleted);
    }

    @VisibleForTesting
{'fraction_non_alphanumeric': 0.07366933584550164, 'fraction_numerical': 0.003579839849269901, 'mean_word_length': 2.663216011042098, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7595906', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2502, 'n_words': 639}