Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java

raw symbols SCIP
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.extension;
12
13 import static org.apiguardian.api.API.Status.EXPERIMENTAL;
14 import static org.apiguardian.api.API.Status.STABLE;
15
16 import java.lang.reflect.AnnotatedElement;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import java.util.function.Function;
26
27 import org.apiguardian.api.API;
28 import org.junit.jupiter.api.TestInstance.Lifecycle;
29 import org.junit.jupiter.api.parallel.ExecutionMode;
30 import org.junit.platform.commons.PreconditionViolationException;
31 import org.junit.platform.commons.support.ReflectionSupport;
32 import org.junit.platform.commons.util.Preconditions;
33
34 /**
35 * {@code ExtensionContext} encapsulates the <em>context</em> in which the
36 * current test or container is being executed.
37 *
38 * <p>{@link Extension Extensions} are provided an instance of
39 * {@code ExtensionContext} to perform their work.
40 *
41 * @since 5.0
42 * @see Store
43 * @see Namespace
44 */
45 @API(status = STABLE, since = "5.0")
46 public interface ExtensionContext {
47
48 /**
49 * Get the parent extension context, if available.
50 *
51 * @return an {@code Optional} containing the parent; never {@code null} but
52 * potentially empty
53 * @see #getRoot()
54 */
55 Optional<ExtensionContext> getParent();
56
57 /**
58 * Get the <em>root</em> {@code ExtensionContext}.
59 *
60 * @return the root extension context; never {@code null} but potentially
61 * <em>this</em> {@code ExtensionContext}
62 * @see #getParent()
63 */
64 ExtensionContext getRoot();
65
66 /**
67 * Get the unique ID of the current test or container.
68 *
69 * @return the unique ID of the test or container; never {@code null} or blank
70 */
71 String getUniqueId();
72
73 /**
74 * Get the display name for the current test or container.
75 *
76 * <p>The display name is either a default name or a custom name configured
77 * via {@link org.junit.jupiter.api.DisplayName @DisplayName}.
78 *
79 * <p>For details on default display names consult the Javadoc for
80 * {@link org.junit.jupiter.api.TestInfo#getDisplayName()}.
81 *
82 * <p>Note that display names are typically used for test reporting in IDEs
83 * and build tools and may contain spaces, special characters, and even emoji.
84 *
85 * @return the display name of the test or container; never {@code null} or blank
86 */
87 String getDisplayName();
88
89 /**
90 * Get the set of all tags for the current test or container.
91 *
92 * <p>Tags may be declared directly on the test element or <em>inherited</em>
93 * from an outer context.
94 *
95 * @return the set of tags for the test or container; never {@code null} but
96 * potentially empty
97 */
98 Set<String> getTags();
99
100 /**
101 * Get the {@link AnnotatedElement} corresponding to the current extension
102 * context, if available.
103 *
104 * <p>For example, if the current extension context encapsulates a test
105 * class, test method, test factory method, or test template method, the
106 * annotated element will be the corresponding {@link Class} or {@link Method}
107 * reference.
108 *
109 * <p>Favor this method over more specific methods whenever the
110 * {@code AnnotatedElement} API suits the task at hand &mdash; for example,
111 * when looking up annotations regardless of concrete element type.
112 *
113 * @return an {@code Optional} containing the {@code AnnotatedElement};
114 * never {@code null} but potentially empty
115 * @see #getTestClass()
116 * @see #getTestMethod()
117 */
118 Optional<AnnotatedElement> getElement();
119
120 /**
121 * Get the {@link Class} associated with the current test or container,
122 * if available.
123 *
124 * @return an {@code Optional} containing the class; never {@code null} but
125 * potentially empty
126 * @see #getRequiredTestClass()
127 */
128 Optional<Class<?>> getTestClass();
129
130 /**
131 * Get the <em>required</em> {@link Class} associated with the current test
132 * or container.
133 *
134 * <p>Use this method as an alternative to {@link #getTestClass()} for use
135 * cases in which the test class is required to be present.
136 *
137 * @return the test class; never {@code null}
138 * @throws PreconditionViolationException if the test class is not present
139 * in this {@code ExtensionContext}
140 */
141 default Class<?> getRequiredTestClass() {
142 return Preconditions.notNull(getTestClass().orElse(null),
143 "Illegal state: required test class is not present in the current ExtensionContext");
144 }
145
146 /**
147 * Get the {@link Lifecycle} of the {@linkplain #getTestInstance() test
148 * instance} associated with the current test or container, if available.
149 *
150 * @return an {@code Optional} containing the test instance {@code Lifecycle};
151 * never {@code null} but potentially empty
152 * @since 5.1
153 * @see org.junit.jupiter.api.TestInstance {@code @TestInstance}
154 */
155 @API(status = STABLE, since = "5.1")
156 Optional<Lifecycle> getTestInstanceLifecycle();
157
158 /**
159 * Get the test instance associated with the current test or container,
160 * if available.
161 *
162 * @return an {@code Optional} containing the test instance; never
163 * {@code null} but potentially empty
164 * @see #getRequiredTestInstance()
165 * @see #getTestInstances()
166 */
167 Optional<Object> getTestInstance();
168
169 /**
170 * Get the <em>required</em> test instance associated with the current test
171 * or container.
172 *
173 * <p>Use this method as an alternative to {@link #getTestInstance()} for use
174 * cases in which the test instance is required to be present.
175 *
176 * @return the test instance; never {@code null}
177 * @throws PreconditionViolationException if the test instance is not present
178 * in this {@code ExtensionContext}
179 *
180 * @see #getRequiredTestInstances()
181 */
182 default Object getRequiredTestInstance() {
183 return Preconditions.notNull(getTestInstance().orElse(null),
184 "Illegal state: required test instance is not present in the current ExtensionContext");
185 }
186
187 /**
188 * Get the test instances associated with the current test or container,
189 * if available.
190 *
191 * <p>While top-level tests only have a single test instance, nested tests
192 * have one additional instance for each enclosing test class.
193 *
194 * @return an {@code Optional} containing the test instances; never
195 * {@code null} but potentially empty
196 * @since 5.4
197 * @see #getRequiredTestInstances()
198 */
199 @API(status = STABLE, since = "5.7")
200 Optional<TestInstances> getTestInstances();
201
202 /**
203 * Get the <em>required</em> test instances associated with the current test
204 * or container.
205 *
206 * <p>Use this method as an alternative to {@link #getTestInstances()} for use
207 * cases in which the test instances are required to be present.
208 *
209 * @return the test instances; never {@code null}
210 * @throws PreconditionViolationException if the test instances are not present
211 * in this {@code ExtensionContext}
212 * @since 5.4
213 */
214 @API(status = STABLE, since = "5.7")
215 default TestInstances getRequiredTestInstances() {
216 return Preconditions.notNull(getTestInstances().orElse(null),
217 "Illegal state: required test instances are not present in the current ExtensionContext");
218 }
219
220 /**
221 * Get the {@link Method} associated with the current test, if available.
222 *
223 * @return an {@code Optional} containing the method; never {@code null} but
224 * potentially empty
225 * @see #getRequiredTestMethod()
226 */
227 Optional<Method> getTestMethod();
228
229 /**
230 * Get the <em>required</em> {@link Method} associated with the current test
231 * or container.
232 *
233 * <p>Use this method as an alternative to {@link #getTestMethod()} for use
234 * cases in which the test method is required to be present.
235 *
236 * @return the test method; never {@code null}
237 * @throws PreconditionViolationException if the test method is not present
238 * in this {@code ExtensionContext}
239 */
240 default Method getRequiredTestMethod() {
241 return Preconditions.notNull(getTestMethod().orElse(null),
242 "Illegal state: required test method is not present in the current ExtensionContext");
243 }
244
245 /**
246 * Get the exception that was thrown during execution of the test or container
247 * associated with this {@code ExtensionContext}, if available.
248 *
249 * <p>This method is typically used for logging and tracing purposes. If you
250 * wish to actually <em>handle</em> an exception thrown during test execution,
251 * implement the {@link TestExecutionExceptionHandler} API.
252 *
253 * <p>Unlike the exception passed to a {@code TestExecutionExceptionHandler},
254 * an <em>execution exception</em> returned by this method can be any
255 * exception thrown during the invocation of a {@code @Test} method, its
256 * surrounding {@code @BeforeEach} and {@code @AfterEach} methods, or a
257 * test-level {@link Extension}. Similarly, if this {@code ExtensionContext}
258 * represents a test class, the <em>execution exception</em> returned by
259 * this method can be any exception thrown in a {@code @BeforeAll} or
260 * {@code AfterAll} method or a class-level {@link Extension}.
261 *
262 * <p>Note, however, that this method will never return an exception
263 * swallowed by a {@code TestExecutionExceptionHandler}. Furthermore, if
264 * multiple exceptions have been thrown during test execution, the exception
265 * returned by this method will be the first such exception with all
266 * additional exceptions {@linkplain Throwable#addSuppressed(Throwable)
267 * suppressed} in the first one.
268 *
269 * @return an {@code Optional} containing the exception thrown; never
270 * {@code null} but potentially empty if test execution has not (yet)
271 * resulted in an exception
272 */
273 Optional<Throwable> getExecutionException();
274
275 /**
276 * Get the configuration parameter stored under the specified {@code key}.
277 *
278 * <p>If no such key is present in the {@code ConfigurationParameters} for
279 * the JUnit Platform, an attempt will be made to look up the value as a
280 * JVM system property. If no such system property exists, an attempt will
281 * be made to look up the value in the JUnit Platform properties file.
282 *
283 * @param key the key to look up; never {@code null} or blank
284 * @return an {@code Optional} containing the value; never {@code null}
285 * but potentially empty
286 *
287 * @since 5.1
288 * @see System#getProperty(String)
289 * @see org.junit.platform.engine.ConfigurationParameters
290 */
291 @API(status = STABLE, since = "5.1")
292 Optional<String> getConfigurationParameter(String key);
293
294 /**
295 * Get and transform the configuration parameter stored under the specified
296 * {@code key} using the specified {@code transformer}.
297 *
298 * <p>If no such key is present in the {@code ConfigurationParameters} for
299 * the JUnit Platform, an attempt will be made to look up the value as a
300 * JVM system property. If no such system property exists, an attempt will
301 * be made to look up the value in the JUnit Platform properties file.
302 *
303 * <p>In case the transformer throws an exception, it will be wrapped in a
304 * {@link org.junit.platform.commons.JUnitException} with a helpful message.
305 *
306 * @param key the key to look up; never {@code null} or blank
307 * @param transformer the transformer to apply in case a value is found;
308 * never {@code null}
309 * @return an {@code Optional} containing the value; never {@code null}
310 * but potentially empty
311 *
312 * @since 5.7
313 * @see System#getProperty(String)
314 * @see org.junit.platform.engine.ConfigurationParameters
315 */
316 @API(status = STABLE, since = "5.10")
317 <T> Optional<T> getConfigurationParameter(String key, Function<String, T> transformer);
318
319 /**
320 * Publish a map of key-value pairs to be consumed by an
321 * {@code org.junit.platform.engine.EngineExecutionListener} in order to
322 * supply additional information to the reporting infrastructure.
323 *
324 * @param map the key-value pairs to be published; never {@code null};
325 * keys and values within entries in the map also must not be
326 * {@code null} or blank
327 * @see #publishReportEntry(String, String)
328 * @see #publishReportEntry(String)
329 * @see org.junit.platform.engine.EngineExecutionListener#reportingEntryPublished
330 */
331 void publishReportEntry(Map<String, String> map);
332
333 /**
334 * Publish the specified key-value pair to be consumed by an
335 * {@code org.junit.platform.engine.EngineExecutionListener} in order to
336 * supply additional information to the reporting infrastructure.
337 *
338 * @param key the key of the published pair; never {@code null} or blank
339 * @param value the value of the published pair; never {@code null} or blank
340 * @see #publishReportEntry(Map)
341 * @see #publishReportEntry(String)
342 * @see org.junit.platform.engine.EngineExecutionListener#reportingEntryPublished
343 */
344 default void publishReportEntry(String key, String value) {
345 this.publishReportEntry(Collections.singletonMap(key, value));
346 }
347
348 /**
349 * Publish the specified value to be consumed by an
350 * {@code org.junit.platform.engine.EngineExecutionListener} in order to
351 * supply additional information to the reporting infrastructure.
352 *
353 * <p>This method delegates to {@link #publishReportEntry(String, String)},
354 * supplying {@code "value"} as the key and the supplied {@code value}
355 * argument as the value.
356 *
357 * @param value the value to be published; never {@code null} or blank
358 * @since 5.3
359 * @see #publishReportEntry(Map)
360 * @see #publishReportEntry(String, String)
361 * @see org.junit.platform.engine.EngineExecutionListener#reportingEntryPublished
362 */
363 @API(status = STABLE, since = "5.3")
364 default void publishReportEntry(String value) {
365 this.publishReportEntry("value", value);
366 }
367
368 /**
369 * Get the {@link Store} for the supplied {@link Namespace}.
370 *
371 * <p>Use {@code getStore(Namespace.GLOBAL)} to get the default, global {@link Namespace}.
372 *
373 * <p>A store is bound to its extension context lifecycle. When an extension
374 * context lifecycle ends it closes its associated store. All stored values
375 * that are instances of {@link ExtensionContext.Store.CloseableResource} are
376 * notified by invoking their {@code close()} methods.
377 *
378 * @param namespace the {@code Namespace} to get the store for; never {@code null}
379 * @return the store in which to put and get objects for other invocations
380 * working in the same namespace; never {@code null}
381 * @see Namespace#GLOBAL
382 */
383 Store getStore(Namespace namespace);
384
385 /**
386 * Get the {@link ExecutionMode} associated with the current test or container.
387 *
388 * @return the {@code ExecutionMode} of the test; never {@code null}
389 *
390 * @since 5.8.1
391 * @see org.junit.jupiter.api.parallel.ExecutionMode {@code @ExecutionMode}
392 */
393 @API(status = STABLE, since = "5.8.1")
394 ExecutionMode getExecutionMode();
395
396 /**
397 * Get an {@link ExecutableInvoker} to invoke methods and constructors
398 * with support for dynamic resolution of parameters.
399 *
400 * @since 5.9
401 */
402 @API(status = EXPERIMENTAL, since = "5.9")
403 ExecutableInvoker getExecutableInvoker();
404
405 /**
406 * {@code Store} provides methods for extensions to save and retrieve data.
407 */
408 interface Store {
409
410 /**
411 * Classes implementing this interface indicate that they want to {@link #close}
412 * some underlying resource or resources when the enclosing {@link Store Store}
413 * is closed.
414 *
415 * <p>Note that the {@code CloseableResource} API is only honored for
416 * objects stored within an extension context {@link Store Store}.
417 *
418 * <p>The resources stored in a {@link Store Store} are closed in the
419 * inverse order they were added in.
420 *
421 * @since 5.1
422 */
423 @API(status = STABLE, since = "5.1")
424 interface CloseableResource {
425
426 /**
427 * Close underlying resources.
428 *
429 * @throws Throwable any throwable will be caught and rethrown
430 */
431 void close() throws Throwable;
432
433 }
434
435 /**
436 * Get the value that is stored under the supplied {@code key}.
437 *
438 * <p>If no value is stored in the current {@link ExtensionContext}
439 * for the supplied {@code key}, ancestors of the context will be queried
440 * for a value with the same {@code key} in the {@code Namespace} used
441 * to create this store.
442 *
443 * <p>For greater type safety, consider using {@link #get(Object, Class)}
444 * instead.
445 *
446 * @param key the key; never {@code null}
447 * @return the value; potentially {@code null}
448 * @see #get(Object, Class)
449 * @see #getOrDefault(Object, Class, Object)
450 */
451 Object get(Object key);
452
453 /**
454 * Get the value of the specified required type that is stored under
455 * the supplied {@code key}.
456 *
457 * <p>If no value is stored in the current {@link ExtensionContext}
458 * for the supplied {@code key}, ancestors of the context will be queried
459 * for a value with the same {@code key} in the {@code Namespace} used
460 * to create this store.
461 *
462 * @param key the key; never {@code null}
463 * @param requiredType the required type of the value; never {@code null}
464 * @param <V> the value type
465 * @return the value; potentially {@code null}
466 * @see #get(Object)
467 * @see #getOrDefault(Object, Class, Object)
468 */
469 <V> V get(Object key, Class<V> requiredType);
470
471 /**
472 * Get the value of the specified required type that is stored under
473 * the supplied {@code key}, or the supplied {@code defaultValue} if no
474 * value is found for the supplied {@code key} in this store or in an
475 * ancestor.
476 *
477 * <p>If no value is stored in the current {@link ExtensionContext}
478 * for the supplied {@code key}, ancestors of the context will be queried
479 * for a value with the same {@code key} in the {@code Namespace} used
480 * to create this store.
481 *
482 * @param key the key; never {@code null}
483 * @param requiredType the required type of the value; never {@code null}
484 * @param defaultValue the default value
485 * @param <V> the value type
486 * @return the value; potentially {@code null}
487 * @since 5.5
488 * @see #get(Object, Class)
489 */
490 @API(status = STABLE, since = "5.5")
491 default <V> V getOrDefault(Object key, Class<V> requiredType, V defaultValue) {
492 V value = get(key, requiredType);
493 return (value != null ? value : defaultValue);
494 }
495
496 /**
497 * Get the object of type {@code type} that is present in this
498 * {@code Store} (<em>keyed</em> by {@code type}); and otherwise invoke
499 * the default constructor for {@code type} to generate the object,
500 * store it, and return it.
501 *
502 * <p>This method is a shortcut for the following, where {@code X} is
503 * the type of object we wish to retrieve from the store.
504 *
505 * <pre style="code">
506 * X x = store.getOrComputeIfAbsent(X.class, key -&gt; new X(), X.class);
507 * // Equivalent to:
508 * // X x = store.getOrComputeIfAbsent(X.class);
509 * </pre>
510 *
511 * <p>See {@link #getOrComputeIfAbsent(Object, Function, Class)} for
512 * further details.
513 *
514 * <p>If {@code type} implements {@link ExtensionContext.Store.CloseableResource}
515 * the {@code close()} method will be invoked on the stored object when
516 * the store is closed.
517 *
518 * @param type the type of object to retrieve; never {@code null}
519 * @param <V> the key and value type
520 * @return the object; never {@code null}
521 * @since 5.1
522 * @see #getOrComputeIfAbsent(Object, Function)
523 * @see #getOrComputeIfAbsent(Object, Function, Class)
524 * @see CloseableResource
525 */
526 @API(status = STABLE, since = "5.1")
527 default <V> V getOrComputeIfAbsent(Class<V> type) {
528 return getOrComputeIfAbsent(type, ReflectionSupport::newInstance, type);
529 }
530
531 /**
532 * Get the value that is stored under the supplied {@code key}.
533 *
534 * <p>If no value is stored in the current {@link ExtensionContext}
535 * for the supplied {@code key}, ancestors of the context will be queried
536 * for a value with the same {@code key} in the {@code Namespace} used
537 * to create this store. If no value is found for the supplied {@code key},
538 * a new value will be computed by the {@code defaultCreator} (given
539 * the {@code key} as input), stored, and returned.
540 *
541 * <p>For greater type safety, consider using
542 * {@link #getOrComputeIfAbsent(Object, Function, Class)} instead.
543 *
544 * <p>If the created value is an instance of {@link ExtensionContext.Store.CloseableResource}
545 * the {@code close()} method will be invoked on the stored object when
546 * the store is closed.
547 *
548 * @param key the key; never {@code null}
549 * @param defaultCreator the function called with the supplied {@code key}
550 * to create a new value; never {@code null} but may return {@code null}
551 * @param <K> the key type
552 * @param <V> the value type
553 * @return the value; potentially {@code null}
554 * @see #getOrComputeIfAbsent(Class)
555 * @see #getOrComputeIfAbsent(Object, Function, Class)
556 * @see CloseableResource
557 */
558 <K, V> Object getOrComputeIfAbsent(K key, Function<K, V> defaultCreator);
559
560 /**
561 * Get the value of the specified required type that is stored under the
562 * supplied {@code key}.
563 *
564 * <p>If no value is stored in the current {@link ExtensionContext}
565 * for the supplied {@code key}, ancestors of the context will be queried
566 * for a value with the same {@code key} in the {@code Namespace} used
567 * to create this store. If no value is found for the supplied {@code key},
568 * a new value will be computed by the {@code defaultCreator} (given
569 * the {@code key} as input), stored, and returned.
570 *
571 * <p>If {@code requiredType} implements {@link ExtensionContext.Store.CloseableResource}
572 * the {@code close()} method will be invoked on the stored object when
573 * the store is closed.
574 *
575 * @param key the key; never {@code null}
576 * @param defaultCreator the function called with the supplied {@code key}
577 * to create a new value; never {@code null} but may return {@code null}
578 * @param requiredType the required type of the value; never {@code null}
579 * @param <K> the key type
580 * @param <V> the value type
581 * @return the value; potentially {@code null}
582 * @see #getOrComputeIfAbsent(Class)
583 * @see #getOrComputeIfAbsent(Object, Function)
584 * @see CloseableResource
585 */
586 <K, V> V getOrComputeIfAbsent(K key, Function<K, V> defaultCreator, Class<V> requiredType);
587
588 /**
589 * Store a {@code value} for later retrieval under the supplied {@code key}.
590 *
591 * <p>A stored {@code value} is visible in child {@link ExtensionContext
592 * ExtensionContexts} for the store's {@code Namespace} unless they
593 * overwrite it.
594 *
595 * <p>If the {@code value} is an instance of {@link ExtensionContext.Store.CloseableResource}
596 * the {@code close()} method will be invoked on the stored object when
597 * the store is closed.
598 *
599 * @param key the key under which the value should be stored; never
600 * {@code null}
601 * @param value the value to store; may be {@code null}
602 * @see CloseableResource
603 */
604 void put(Object key, Object value);
605
606 /**
607 * Remove the value that was previously stored under the supplied {@code key}.
608 *
609 * <p>The value will only be removed in the current {@link ExtensionContext},
610 * not in ancestors. In addition, the {@link CloseableResource} API will not
611 * be honored for values that are manually removed via this method.
612 *
613 * <p>For greater type safety, consider using {@link #remove(Object, Class)}
614 * instead.
615 *
616 * @param key the key; never {@code null}
617 * @return the previous value or {@code null} if no value was present
618 * for the specified key
619 * @see #remove(Object, Class)
620 */
621 Object remove(Object key);
622
623 /**
624 * Remove the value of the specified required type that was previously stored
625 * under the supplied {@code key}.
626 *
627 * <p>The value will only be removed in the current {@link ExtensionContext},
628 * not in ancestors. In addition, the {@link CloseableResource} API will not
629 * be honored for values that are manually removed via this method.
630 *
631 * @param key the key; never {@code null}
632 * @param requiredType the required type of the value; never {@code null}
633 * @param <V> the value type
634 * @return the previous value or {@code null} if no value was present
635 * for the specified key
636 * @see #remove(Object)
637 */
638 <V> V remove(Object key, Class<V> requiredType);
639
640 }
641
642 /**
643 * A {@code Namespace} is used to provide a <em>scope</em> for data saved by
644 * extensions within a {@link Store}.
645 *
646 * <p>Storing data in custom namespaces allows extensions to avoid accidentally
647 * mixing data between extensions or across different invocations within the
648 * lifecycle of a single extension.
649 */
650 class Namespace {
651
652 /**
653 * The default, global namespace which allows access to stored data from
654 * all extensions.
655 */
656 public static final Namespace GLOBAL = Namespace.create(new Object());
657
658 /**
659 * Create a namespace which restricts access to data to all extensions
660 * which use the same sequence of {@code parts} for creating a namespace.
661 *
662 * <p>The order of the {@code parts} is significant.
663 *
664 * <p>Internally the {@code parts} are compared using {@link Object#equals(Object)}.
665 */
666 public static Namespace create(Object... parts) {
667 Preconditions.notEmpty(parts, "parts array must not be null or empty");
668 Preconditions.containsNoNullElements(parts, "individual parts must not be null");
669 return new Namespace(new ArrayList<>(Arrays.asList(parts)));
670 }
671
672 private final List<Object> parts;
673
674 private Namespace(List<Object> parts) {
675 this.parts = parts;
676 }
677
678 @Override
679 public boolean equals(Object o) {
680 if (this == o) {
681 return true;
682 }
683 if (o == null || getClass() != o.getClass()) {
684 return false;
685 }
686 Namespace that = (Namespace) o;
687 return this.parts.equals(that.parts);
688 }
689
690 @Override
691 public int hashCode() {
692 return this.parts.hashCode();
693 }
694
695 /**
696 * Create a new namespace by appending the supplied {@code parts} to the
697 * existing sequence of parts in this namespace.
698 *
699 * @return new namespace; never {@code null}
700 * @since 5.8
701 */
702 @API(status = STABLE, since = "5.10")
703 public Namespace append(Object... parts) {
704 Preconditions.notEmpty(parts, "parts array must not be null or empty");
705 Preconditions.containsNoNullElements(parts, "individual parts must not be null");
706 ArrayList<Object> newParts = new ArrayList<>(this.parts.size() + parts.length);
707 newParts.addAll(this.parts);
708 Collections.addAll(newParts, parts);
709 return new Namespace(newParts);
710 }
711 }
712
713 }