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

io.streamnative.pulsar.handlers.kop.schemaregistry.model.SubjectKeyComparator Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
 */
/**
 * 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 io.streamnative.pulsar.handlers.kop.schemaregistry.model;

import java.io.Serializable;
import java.util.Comparator;

public class SubjectKeyComparator implements Comparator, Serializable {

    @Override
    @SuppressWarnings("unchecked")
    public int compare(K o1, K o2) {
        if (o1 instanceof SubjectKey s1 && o2 instanceof SubjectKey s2) {
            int cmp = s1.keyType.compareTo(s2.keyType);
            if (cmp != 0) {
                return cmp;
            }
            if (s1.getSubject() == null && s2.getSubject() == null) {
                return 0;
            } else if (s1.getSubject() == null) {
                return -1;
            } else if (s2.getSubject() == null) {
                return 1;
            }
            cmp = s1.getSubject().compareTo(s2.getSubject());
            if (cmp != 0) {
                return cmp;
            }
            if (s1 instanceof SchemaKey && s2 instanceof SchemaKey) {
                SchemaKey sk1 = (SchemaKey) o1;
                SchemaKey sk2 = (SchemaKey) o2;
                return sk1.getVersion() - sk2.getVersion();
            } else {
                return 0;
            }
        } else {
            return ((Comparable) o1).compareTo(o2);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy