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

org.scijava.ops.spi.Nullable Maven / Gradle / Ivy

/*-
 * #%L
 * Service descriptors used by the SciJava Ops framework.
 * %%
 * Copyright (C) 2021 - 2024 SciJava developers.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package org.scijava.ops.spi;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Annotation to mark a parameter as nullable: Ops with nullable parameters
 * should be callable with or without their Nullable arguments
 * 

* This annotation should only be specified on one of the signatures of * the method (only on the {@link FunctionalInterface}'s method, or only on the * Op implementation, etc) for purposes of simplicity and readability. Writing * *

 * public interface BiFunctionWithNullable<I1, I2, I3, O> extends
 * 	Functions.Arity3<>
 * {
 *
 * 	public O apply(I1 in1, I2 in2, @Nullable I3 in3);
 * }
 * 
* * and then writing an implementation * *
 * public class Impl implements BiFunctionWithNullable<Double, Double, Double, Double> {
 * 	public Double apply(Double in1, @Nullable Double in2, Double in3) {
 * 	...
 * 	}
 * }
 * 
* * is confusing and hard to read. Which parameters are nullable in this case? Is * it obvious that {@code in3} is nullable just by looking at {@code Impl}? For * this reason, it should be enforced that the annotation is only on one of the * method signatures. *

* Note also that annotations are currently (as of Java 11) not supported on * lambdas. Nullable parameters are supported on interfaces and class * implementations (including anonymous classes) but the following will not * recognize the nullability of {@code in}: *

* *
 *
 * @OpField(names = "nullableLambda")
 * public final Function<Integer, Float> nullableLambda = (
 * 	@Nullable Integer in) -> {
 * 	Integer nonNullIn = in == null ? 0 : in;
 * 	return nonNullIn + 0.5f;
 * };
 * 
* * See also these SO posts: one, * two * * @author Gabriel Selzer */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Nullable { }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy