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

com.hazelcast.org.apache.calcite.linq4j.Nullness Maven / Gradle / Ivy

There is a newer version: 5.4.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.linq4j;

import com.hazelcast.org.checkerframework.checker.initialization.qual.UnderInitialization;
import com.hazelcast.org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import com.hazelcast.org.checkerframework.checker.nullness.qual.NonNull;
import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;
import com.hazelcast.org.checkerframework.dataflow.qual.Pure;

/**
 * The methods in this class allow to cast nullable reference to a non-nullable one.
 * This is an internal class, and it is not meant to be used as a public API.
 *
 * 

The class enables to remove checker-qual runtime dependency, and helps IDEs to see * the resulting types of {@code castNonNull} better. */ @SuppressWarnings({"cast.unsafe", "RedundantCast", "contracts.postcondition.not.satisfied"}) public class Nullness { private Nullness() { } /** * Allows you to treat a nullable type as non-nullable with no assertions. * *

It is useful in the case you have a nullable lately-initialized field * like the following: * *


   * class Wrapper<T> {
   *   @Nullable T value;
   * }
   * 
* *

That signature allows you to use {@code Wrapper} with both nullable or * non-nullable types: {@code Wrapper<@Nullable Integer>} * vs {@code Wrapper}. Suppose you need to implement * *


   * T get() { return value; }
   * 
* *

The issue is checkerframework does not permit that because {@code T} * has unknown nullability, so the following needs to be used: * *


   * T get() { return sneakyNull(value); }
   * 
* * @param the type of the reference * @param ref a reference of @Nullable type, that is non-null at run time * * @return the argument, cast to have the type qualifier @NonNull */ @Pure public static @EnsuresNonNull("#1") @NonNull T castNonNull( @Nullable T ref) { //noinspection ConstantConditions return (@NonNull T) ref; } /** * Allows you to treat an uninitialized or under-initialization object as * initialized with no assertions. * * @param The type of the reference * @param ref A reference that was @Uninitialized at some point but is * now fully initialized * * @return the argument, cast to have type qualifier @Initialized */ @SuppressWarnings({"unchecked"}) @Pure public static T castToInitialized(@UnderInitialization T ref) { // To throw CheckerFramework off the scent, we put the object into an array, // cast the array to an Object, and cast back to an array. Object src = new Object[] {ref}; Object[] dest = (Object[]) src; return (T) dest[0]; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy