<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.client;

import com.google.common.base.Objects;
import com.google.protobuf.Any;
import com.google.protobuf.Message;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.spine3.protobuf.AnyPacker.unpack;
import static org.spine3.protobuf.TypeConverter.toAny;

/**
 * A parameter of a {@link Query}.
 *
 * <p>This class may be considered a filter for the query. An instance contains the name of
 * the Entity Column to filter by and the value of the Column.
 *
 * <p>The supported types for querying are {@linkplain Message Message types} and Protobuf
 * primitives.
 *
 * @see org.spine3.protobuf.TypeConverter for the list of supported types
 */
public final class QueryParameter {

    private final String columnName;
    private final Any value;

    private QueryParameter(String columnName, Any value) {
        this.columnName = columnName;
        this.value = value;
    }

    /**
     * Creates new equality {@code QueryParameter}.
     *
     * @param columnName the name of the Entity Column to query by, expressed in a single field
     *                   name with no type info
     * @param value      the requested value of the Entity Column
     * @return new instance of the QueryParameter
     */
    public static QueryParameter eq(String columnName, Object value) {
        checkNotNull(columnName);
        checkNotNull(value);
        final Any wrappedValue = toAny(value);
        final QueryParameter parameter = new QueryParameter(columnName, wrappedValue);
        return parameter;
    }

    /**
     * @return the name of the Entity Column to query by
     */
    public String getColumnName() {
        return columnName;
    }

    /**
     * @return the value of the Entity Column to look for
     */
    public Any getValue() {
        return value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        QueryParameter parameter = (QueryParameter) o;
        return Objects.equal(getColumnName(), parameter.getColumnName()) &&
                Objects.equal(getValue(), parameter.getValue());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getColumnName(), getValue());
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append('(')
          .append(columnName)
          .append(" = ")
          .append(unpack(value))
          .append(')');
        return sb.toString();
    }
}
<commit_msg>Update QueryParameter.<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.client;

import com.google.common.base.Objects;
import com.google.protobuf.Any;
import com.google.protobuf.Message;
import com.google.protobuf.Timestamp;
import org.spine3.protobuf.AnyPacker;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.spine3.client.QueryParameter.Operator.EQUAL;
import static org.spine3.client.QueryParameter.Operator.GREATER_THEN;
import static org.spine3.protobuf.AnyPacker.unpack;
import static org.spine3.protobuf.TypeConverter.toAny;

/**
 * A parameter of a {@link Query}.
 *
 * <p>This class may be considered a filter for the query. An instance contains the name of
 * the Entity Column to filter by, the value of the Column and
 * the {@linkplain Operator comparison operator}.
 *
 * <p>The supported types for querying are {@linkplain Message Message types} and Protobuf
 * primitives.
 *
 * @see org.spine3.protobuf.TypeConverter for the list of supported types
 */
public final class QueryParameter {

    private final String columnName;
    private final Any value;
    private final Operator operator;

    private QueryParameter(String columnName, Any value, Operator operator) {
        this.columnName = columnName;
        this.value = value;
        this.operator = operator;
    }

    /**
     * Creates new equality {@code QueryParameter}.
     *
     * @param columnName the name of the Entity Column to query by, expressed in a single field
     *                   name with no type info
     * @param value      the requested value of the Entity Column
     * @return new instance of QueryParameter
     */
    public static QueryParameter eq(String columnName, Object value) {
        checkNotNull(columnName);
        checkNotNull(value);
        final Any wrappedValue = toAny(value);
        final QueryParameter parameter = new QueryParameter(columnName, wrappedValue, EQUAL);
        return parameter;
    }

    /**
     * Creates new {@code QueryParameter} stating that the stored {@link Timestamp} value represents
     * the time which is later then the given value.
     *
     * <p>The instances created with this method have {@link Operator#GREATER_THEN} operator.
     *
     * @param columnName the name of the Entity Column to query by, expressed in a single field
     *                   name with no type info
     * @param value      the value to compere upon
     * @return new instance of QueryParameter
     */
    public static QueryParameter laterThen(String columnName, Timestamp value) {
        checkNotNull(columnName);
        checkNotNull(value);
        final Any wrappedValue = AnyPacker.pack(value);
        final QueryParameter parameter = new QueryParameter(columnName, wrappedValue, GREATER_THEN);
        return parameter;
    }

    /**
     * @return the name of the Entity Column to query by
     */
    public String getColumnName() {
        return columnName;
    }

    /**
     * @return the value of the Entity Column to look for
     */
    public Any getValue() {
        return value;
    }

    /**
     * Retrieves the comparison operator of this {@code QueryParameter}.
     *
     * <p>At the moment, all the parameters have the {@link Operator#EQUAL equality comparison}
     * except those that {@link #laterThen compare} two {@link Timestamp} instances. Though,
     * this behavior is a subject of change.
     *
     * @return the comparison operator
     */
    public Operator getOperator() {
        return operator;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        QueryParameter parameter = (QueryParameter) o;
        return Objects.equal(getColumnName(), parameter.getColumnName()) &&
                Objects.equal(getValue(), parameter.getValue()) &&
                getOperator() == parameter.getOperator();
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getColumnName(), getValue(), getOperator());
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append('(')
          .append(columnName)
          .append(' ')
          .append(operator)
          .append(' ')
          .append(unpack(value))
          .append(')');
        return sb.toString();
    }

    /**
     * An enumeration of all supported value comparison operators applicable to the Entity Columns.
     */
    public enum Operator {

        /**
         * Equality operator ({@code =}).
         */
        EQUAL("="),

        /**
         * Comparison operator stating that the stored value is greater then ({@code >})
         * the passed value.
         */
        GREATER_THEN(">");

        private final String representation;

        Operator(String repr) {
            this.representation = repr;
        }

        @Override
        public String toString() {
            return representation;
        }
    }
}

{'fraction_non_alphanumeric': 0.08405856455411079, 'fraction_numerical': 0.0019453260980853897, 'mean_word_length': 2.715481171548117, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5448854', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2423, 'n_words': 971}