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

com.arakelian.jdbc.model.Index Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.arakelian.jdbc.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class Index {
    private static final Comparator SORT_BY_SEQUENCE = new Comparator() {
        @Override
        public int compare(final IndexField o1, final IndexField o2) {
            return o1.getSequence() - o2.getSequence();
        }
    };

    private static final String[] NO_FIELDS = new String[0];

    private String indexName;

    private final List fields;

    public Index() {
        this.fields = new ArrayList<>();
    }

    public Index(final String indexName) {
        this();
        this.indexName = indexName;
    }

    public Index(final String indexName, final List fields) {
        this.indexName = indexName;
        this.fields = fields;
    }

    public void appendFieldNames(final StringBuilder tb, final String descendingFlag) {
        for (int i = 0, size = getFieldCount(); i < size; i++) {
            if (i != 0) {
                tb.append(", ");
            }
            final IndexField indexField = fields.get(i);
            tb.append(indexField.getColumnName());
            if (!indexField.isAscending() && !StringUtils.isEmpty(descendingFlag)) {
                tb.append(descendingFlag);
            }
        }
    }

    public int getFieldCount() {
        return fields != null ? fields.size() : 0;
    }

    public final String[] getFieldNames() {
        if (fields == null || fields.size() == 0) {
            return NO_FIELDS;
        }
        final int size = fields.size();
        final String[] names = new String[size];
        for (int i = 0; i < size; i++) {
            names[i] = fields.get(i).getColumnName();
        }
        return names;
    }

    public final List getFields() {
        return fields != null ? fields : Collections. emptyList();
    }

    public final String getIndexName() {
        return indexName;
    }

    public final void setIndexName(final String indexName) {
        this.indexName = indexName;
    }

    public void sortBySequence() {
        if (fields != null) {
            Collections.sort(fields, SORT_BY_SEQUENCE);
        }
    }

    @Override
    public String toString() {
        final StringBuilder tb = new StringBuilder();
        appendFieldNames(tb, null);
        return tb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy