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

com.hazelcast.org.apache.calcite.util.ImmutableNullableMap Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show 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.hazelcast.org.apache.calcite.util;

import com.hazelcast.com.google.common.collect.ImmutableMap;
import com.hazelcast.com.google.common.collect.ImmutableSortedMap;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * An immutable map that may contain null values.
 *
 * 

If the map cannot contain null values, use {@link ImmutableMap}. * * @param Key type * @param Value type */ public abstract class ImmutableNullableMap extends AbstractMap { private static final Map SINGLETON_MAP = Collections.singletonMap(0, 0); private ImmutableNullableMap() { } /** * Returns an immutable map containing the given elements. * *

Behavior is as {@link ImmutableMap#copyOf(Iterable)} * except that this map allows nulls. */ @SuppressWarnings({"JdkObsolete", "unchecked", "rawtypes"}) public static Map copyOf(Map map) { if (map instanceof ImmutableNullableMap || map instanceof ImmutableMap || map == Collections.emptyMap() || map == Collections.emptyNavigableMap() || map.getClass() == SINGLETON_MAP.getClass()) { return (Map) map; } if (map instanceof SortedMap) { final SortedMap sortedMap = (SortedMap) map; try { Comparator comparator = sortedMap.comparator(); if (comparator == null) { return ImmutableSortedMap.copyOf(sortedMap); } else { return ImmutableSortedMap.copyOf(sortedMap, comparator); } } catch (NullPointerException e) { // Make an effectively immutable map by creating a mutable copy // and wrapping it to prevent modification. Unfortunately, if we see // it again we will not recognize that it is immutable and we will make // another copy. return Collections.unmodifiableNavigableMap(new TreeMap<>(sortedMap)); } } else { try { return ImmutableMap.copyOf(map); } catch (NullPointerException e) { // Make an effectively immutable map by creating a mutable copy // and wrapping it to prevent modification. Unfortunately, if we see // it again we will not recognize that it is immutable and we will make // another copy. return Collections.unmodifiableMap(new HashMap<>(map)); } } } /** * Returns an immutable navigable map containing the given entries. * *

Behavior is as {@link ImmutableSortedMap#copyOf(Map)} * except that this map allows nulls. */ @SuppressWarnings({"JdkObsolete", "unchecked", "rawtypes"}) public static Map copyOf( SortedMap map) { if (map instanceof ImmutableNullableMap || map instanceof ImmutableMap || map == Collections.emptyMap() || map == Collections.emptyNavigableMap()) { return (Map) map; } final SortedMap sortedMap = (SortedMap) map; try { Comparator comparator = sortedMap.comparator(); if (comparator == null) { return ImmutableSortedMap.copyOf(sortedMap); } else { return ImmutableSortedMap.copyOf(sortedMap, comparator); } } catch (NullPointerException e) { // Make an effectively immutable map by creating a mutable copy // and wrapping it to prevent modification. Unfortunately, if we see // it again we will not recognize that it is immutable and we will make // another copy. return Collections.unmodifiableNavigableMap(new TreeMap<>(sortedMap)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy