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

org.apache.flink.table.runtime.typeutils.SortedMapTypeInfo Maven / Gradle / Ivy

Go to download

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 org.apache.flink.table.runtime.typeutils;

import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.util.Preconditions;

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

/**
 * The type information for sorted maps.
 *
 * @param  The type of the keys in the map.
 * @param  The type of the values in the map.
 */
@PublicEvolving
public class SortedMapTypeInfo extends AbstractMapTypeInfo> {

    private static final long serialVersionUID = 1L;

    /** The comparator for the keys in the map. */
    private final Comparator comparator;

    public SortedMapTypeInfo(
            TypeInformation keyTypeInfo,
            TypeInformation valueTypeInfo,
            Comparator comparator) {
        super(keyTypeInfo, valueTypeInfo);

        Preconditions.checkNotNull(comparator, "The comparator cannot be null.");
        this.comparator = comparator;
    }

    public SortedMapTypeInfo(Class keyClass, Class valueClass, Comparator comparator) {
        super(keyClass, valueClass);

        Preconditions.checkNotNull(comparator, "The comparator cannot be null.");
        this.comparator = comparator;
    }

    public SortedMapTypeInfo(Class keyClass, Class valueClass) {
        super(keyClass, valueClass);

        Preconditions.checkArgument(
                Comparable.class.isAssignableFrom(keyClass),
                "The key class must be comparable when no comparator is given.");
        this.comparator = new ComparableComparator<>();
    }

    // ------------------------------------------------------------------------

    @SuppressWarnings("unchecked")
    @Override
    public Class> getTypeClass() {
        return (Class>) (Class) SortedMap.class;
    }

    @Override
    public TypeSerializer> createSerializer(ExecutionConfig config) {
        TypeSerializer keyTypeSerializer = keyTypeInfo.createSerializer(config);
        TypeSerializer valueTypeSerializer = valueTypeInfo.createSerializer(config);

        return new SortedMapSerializer<>(comparator, keyTypeSerializer, valueTypeSerializer);
    }

    @Override
    public boolean canEqual(Object obj) {
        return null != obj && getClass() == obj.getClass();
    }

    @Override
    public boolean equals(Object o) {
        if (!super.equals(o)) {
            return false;
        }

        SortedMapTypeInfo that = (SortedMapTypeInfo) o;

        return comparator.equals(that.comparator);
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + comparator.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "SortedMapTypeInfo{"
                + "comparator="
                + comparator
                + ", keyTypeInfo="
                + getKeyTypeInfo()
                + ", valueTypeInfo="
                + getValueTypeInfo()
                + "}";
    }

    // --------------------------------------------------------------------------

    /** The default comparator for comparable types. */
    private static class ComparableComparator implements Comparator, Serializable {
        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        public int compare(K obj1, K obj2) {
            return ((Comparable) obj1).compareTo(obj2);
        }

        @Override
        public boolean equals(Object o) {
            return (o == this) || (o != null && o.getClass() == getClass());
        }

        @Override
        public int hashCode() {
            return "ComparableComparator".hashCode();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy