| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import org.apiguardian.api.API; |
| 16 | import org.junit.platform.commons.util.Preconditions; |
| 17 | |
| 18 | /** |
| 19 | * {@code Named} is a container that associates a name with a given payload. |
| 20 | * |
| 21 | * @param <T> the type of the payload |
| 22 | * |
| 23 | * @since 5.8 |
| 24 | */ |
| 25 | @API(status = STABLE, since = "5.8") |
| 26 | public interface Named<T> { |
| 27 | |
| 28 | /** |
| 29 | * Factory method for creating an instance of {@code Named} based on a |
| 30 | * {@code name} and a {@code payload}. |
| 31 | * |
| 32 | * @param name the name associated with the payload; never {@code null} or |
| 33 | * blank |
| 34 | * @param payload the object that serves as the payload; may be {@code null} |
| 35 | * depending on the use case |
| 36 | * @param <T> the type of the payload |
| 37 | * @return an instance of {@code Named}; never {@code null} |
| 38 | * @see #named(String, java.lang.Object) |
| 39 | */ |
| 40 | static <T> Named<T> of(String name, T payload) { |
| 41 | Preconditions.notBlank(name, "name must not be null or blank"); |
| 42 | |
| 43 | return new Named<T>() { |
| 44 | @Override |
| 45 | public String getName() { |
| 46 | return name; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public T getPayload() { |
| 51 | return payload; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String toString() { |
| 56 | return name; |
| 57 | } |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Factory method for creating an instance of {@code Named} based on a |
| 63 | * {@code name} and a {@code payload}. |
| 64 | * |
| 65 | * <p>This method is an <em>alias</em> for {@link Named#of} and is |
| 66 | * intended to be used when statically imported — for example, via: |
| 67 | * {@code import static org.junit.jupiter.api.Named.named;} |
| 68 | * |
| 69 | * @param name the name associated with the payload; never {@code null} or |
| 70 | * blank |
| 71 | * @param payload the object that serves as the payload; may be {@code null} |
| 72 | * depending on the use case |
| 73 | * @param <T> the type of the payload |
| 74 | * @return an instance of {@code Named}; never {@code null} |
| 75 | */ |
| 76 | static <T> Named<T> named(String name, T payload) { |
| 77 | return of(name, payload); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the name of the payload. |
| 82 | * |
| 83 | * @return the name of the payload; never {@code null} or blank |
| 84 | */ |
| 85 | String getName(); |
| 86 | |
| 87 | /** |
| 88 | * Get the payload. |
| 89 | * |
| 90 | * @return the payload; may be {@code null} depending on the use case |
| 91 | */ |
| 92 | T getPayload(); |
| 93 | |
| 94 | } |