All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scassandra.http.client.PrimingRequest Maven / Gradle / Ivy

/*
 * Copyright (C) 2014 Christopher Batey
 *
 * 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.scassandra.http.client;

import java.util.*;

public final class PrimingRequest {

    transient PrimingRequestBuilder.PrimeType primeType;

    public static class PrimingRequestBuilder {

        PrimeType type;

        static enum PrimeType {
            QUERY, PREPARED
        }

        private PrimingRequestBuilder(PrimeType type) {
            this.type = type;
        }

        private Consistency[] consistency;
        private ColumnTypes[] variableTypes;
        private Map columnTypes;
        private String query;
        private String queryPattern;
        private List> rows;
        private Result result = Result.success;
        private Long fixedDelay;

        public PrimingRequestBuilder withQuery(String query) {
            this.query = query;
            return this;
        }

        public PrimingRequestBuilder withQueryPattern(String queryPattern) {
            this.queryPattern = queryPattern;
            return this;
        }


        public PrimingRequestBuilder withFixedDelay(long fixedDelay) {
            this.fixedDelay = fixedDelay;
            return this;
        }

        public PrimingRequestBuilder withRows(List> rows) {
            this.rows = rows;
            return this;
        }

        @SafeVarargs
        public final PrimingRequestBuilder withRows(Map... rows) {
            this.rows = Arrays.asList(rows);
            return this;
        }

        public PrimingRequestBuilder withResult(Result result) {
            this.result = result;
            return this;
        }

        public PrimingRequest build() {

            if (PrimeType.QUERY.equals(this.type) && this.variableTypes != null) {
                throw new IllegalStateException("Variable types only applicable for a prepared statement prime. Not a query prime");
            }

            if (query != null && queryPattern != null) {
                throw new IllegalStateException("Can't specify query and queryPattern");
            }

            if (query == null && queryPattern == null) {
                throw new IllegalStateException("Must set either query or queryPattern for PrimingRequest");
            }

            List consistencies = this.consistency == null ? null : Arrays.asList(this.consistency);

            List> rowsDefaultedToEmptyForSuccess = this.rows;

            if (result == Result.success && rows == null) {
                rowsDefaultedToEmptyForSuccess = Collections.emptyList();
            }
            return new PrimingRequest(type, query, queryPattern, consistencies, rowsDefaultedToEmptyForSuccess, result, columnTypes, variableTypes, fixedDelay);
        }

        public PrimingRequestBuilder withConsistency(Consistency... consistencies) {
            consistency = consistencies;
            return this;
        }

        public PrimingRequestBuilder withColumnTypes(Map types) {
            this.columnTypes = types;
            return this;
        }

        public PrimingRequestBuilder withVariableTypes(ColumnTypes... variableTypes) {
            this.variableTypes = variableTypes;
            return this;
        }
    }

    public static PrimingRequestBuilder queryBuilder() {
        return new PrimingRequestBuilder(PrimingRequestBuilder.PrimeType.QUERY);
    }

    public static PrimingRequestBuilder preparedStatementBuilder() {
        return new PrimingRequestBuilder(PrimingRequestBuilder.PrimeType.PREPARED);
    }

    private final When when;
    private final Then then;

    private PrimingRequest(PrimingRequestBuilder.PrimeType primeType, String query, String queryPattern, List consistency, List> rows, Result result, Map columnTypes, ColumnTypes[] variableTypes, Long fixedDelay) {
        this.primeType = primeType;
        this.when = new When(query, queryPattern, consistency);
        this.then = new Then(rows, result, columnTypes, variableTypes, fixedDelay);
    }

    public When getWhen() {
        return when;
    }

    public Then getThen() {
        return then;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PrimingRequest that = (PrimingRequest) o;

        if (then != null ? !then.equals(that.then) : that.then != null) return false;
        if (when != null ? !when.equals(that.when) : that.when != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = when != null ? when.hashCode() : 0;
        result = 31 * result + (then != null ? then.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "PrimingRequest{" +
                "when='" + when + '\'' +
                ", then=" + then +
                '}';
    }

    public final static class Then {
        private final ColumnTypes[] variable_types;
        private final List> rows;
        private final Result result;
        private final Map column_types;
        private final Long fixedDelay;

        private Then(List> rows, Result result, Map column_types, ColumnTypes[] variable_types, Long fixedDelay) {
            this.rows = rows;
            this.result = result;
            this.column_types = column_types;
            this.variable_types = variable_types;
            this.fixedDelay = fixedDelay;
        }

        @Override
        public int hashCode() {
            return Objects.hash(rows, result, column_types, fixedDelay) + Arrays.hashCode(variable_types);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            final Then other = (Then) obj;
            return Arrays.equals(this.variable_types, other.variable_types) &&
                    Objects.equals(this.rows, other.rows) &&
                    Objects.equals(this.result, other.result) &&
                    Objects.equals(this.column_types, other.column_types) &&
                    Objects.equals(this.fixedDelay, other.fixedDelay);
        }

        @Override
        public String toString() {
            return "Then{" +
                    "variable_types=" + Arrays.toString(variable_types) +
                    ", rows=" + rows +
                    ", result=" + result +
                    ", column_types=" + column_types +
                    ", fixedDelay=" + fixedDelay +
                    '}';
        }

        public ColumnTypes[] getVariableTypes() {
            return variable_types;
        }

        public List> getRows() {
            return Collections.unmodifiableList(rows);
        }

        public Result getResult() {
            return result;
        }

        public Map getColumnTypes() {
            return Collections.unmodifiableMap(column_types);
        }

        public long getFixedDelay() {
            return fixedDelay;
        }
    }

    public final static class When {
        private final String query;
        private final String queryPattern;
        private final List consistency;

        private When(String query, String queryPattern, List consistency) {
            this.query = query;
            this.consistency = consistency;
            this.queryPattern = queryPattern;
        }

        @Override
        public String toString() {
            return "When{" +
                    "query='" + query + '\'' +
                    ", queryPattern='" + queryPattern + '\'' +
                    ", consistency=" + consistency +
                    '}';
        }

        @Override
        public int hashCode() {
            return Objects.hash(query, queryPattern, consistency);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            final When other = (When) obj;
            return Objects.equals(this.query, other.query) && Objects.equals(this.queryPattern, other.queryPattern) && Objects.equals(this.consistency, other.consistency);
        }

        public String getQuery() {
            return query;
        }

        public List getConsistency() {
            return Collections.unmodifiableList(consistency);
        }
    }

    public static enum Consistency {
        ANY,
        ONE,
        TWO,
        THREE,
        QUORUM,
        ALL,
        LOCAL_QUORUM,
        EACH_QUORUM,
        SERIAL,
        LOCAL_SERIAL,
        LOCAL_ONE
    }

    public static enum Result {
        success,
        read_request_timeout,
        unavailable,
        write_request_timeout
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy