Repository navigation
Expand file tree
/
Copy pathFixedInstance.java
More file actions
103 lines (85 loc) · 2.62 KB
/
Copy pathFixedInstance.java
File metadata and controls
103 lines (85 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.a2aproject.sdk.server;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Iterator;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.UnsatisfiedResolutionException;
import jakarta.enterprise.util.TypeLiteral;
import org.jspecify.annotations.Nullable;
/**
* A static {@link Instance} wrapper holding a pre-resolved (or absent) value,
* for use in non-CDI contexts such as convenience constructors and tests.
* A {@code null} value represents an unsatisfied instance.
*
* <p>Usage example (wrapping a concrete value):
* <pre>{@code
* Instance<AgentCard> instance = new FixedInstance<>(myAgentCard);
* }</pre>
*
* <p>Usage example (empty / unsatisfied):
* <pre>{@code
* Instance<AgentCard> empty = FixedInstance.empty();
* }</pre>
*
* @param <T> the bean type
*/
public class FixedInstance<T> implements Instance<T> {
@SuppressWarnings("rawtypes")
private static final FixedInstance EMPTY = new FixedInstance<>(null);
private final @Nullable T value;
public FixedInstance(@Nullable T value) {
this.value = value;
}
/**
* Returns a {@code FixedInstance} with no value (unsatisfied).
*
* @param <T> the bean type
* @return an empty instance
*/
@SuppressWarnings("unchecked")
public static <T> FixedInstance<T> empty() {
return (FixedInstance<T>) EMPTY;
}
@Override
public T get() {
if (value == null) {
throw new UnsatisfiedResolutionException("No value available");
}
return value;
}
@Override
public boolean isUnsatisfied() {
return value == null;
}
@Override
public boolean isAmbiguous() {
return false;
}
@Override
public Iterator<T> iterator() {
return value != null ? Collections.singleton(value).iterator() : Collections.emptyIterator();
}
@Override
public void destroy(T instance) {
}
@Override
public Instance<T> select(Annotation... qualifiers) {
throw new UnsupportedOperationException();
}
@Override
public <U extends T> Instance<U> select(Class<U> subtype, Annotation... qualifiers) {
throw new UnsupportedOperationException();
}
@Override
public <U extends T> Instance<U> select(TypeLiteral<U> subtype, Annotation... qualifiers) {
throw new UnsupportedOperationException();
}
@Override
public Handle<T> getHandle() {
throw new UnsupportedOperationException();
}
@Override
public Iterable<? extends Handle<T>> handles() {
throw new UnsupportedOperationException();
}
}