|
19 | 19 | import java.util.Arrays;
|
20 | 20 | import java.util.HashMap;
|
21 | 21 | import java.util.Map;
|
| 22 | +import java.util.Objects; |
22 | 23 |
|
23 | 24 | import org.apache.qpid.protonj2.client.impl.ClientDeliveryState;
|
| 25 | +import org.apache.qpid.protonj2.types.DescribedType; |
24 | 26 |
|
25 | 27 | /**
|
26 | 28 | * Options type that carries configuration for link Source types.
|
@@ -99,6 +101,32 @@ public SourceOptions filters(Map<String, Object> filters) {
|
99 | 101 | return self();
|
100 | 102 | }
|
101 | 103 |
|
| 104 | + /** |
| 105 | + * Adds the given named filter into the map of filters (one will be created if not already set). |
| 106 | + * <p> |
| 107 | + * If a previous filters {@link Map} was assigned this new filter instance will be assigned |
| 108 | + * into that existing map, it is not cleared or reallocated. The descriptor should either be |
| 109 | + * an Symbol or UnsignedLong that aligns with the filters definition being used. |
| 110 | + * |
| 111 | + * @param name |
| 112 | + * The name to use when adding the described filter to the filters {@link Map}. |
| 113 | + * @param descriptor |
| 114 | + * The descriptor used for the {@link DescribedType} that will carry the filter. |
| 115 | + * @param filter |
| 116 | + * The filter value to assign to the filter {@link DescribedType}. |
| 117 | + * |
| 118 | + * @return this {@link SourceOptions} instance. |
| 119 | + */ |
| 120 | + public SourceOptions addFilter(String name, Object descriptor, Object filter) { |
| 121 | + if (filters == null) { |
| 122 | + filters = new HashMap<>(); |
| 123 | + } |
| 124 | + |
| 125 | + filters.put(name, new FilterDescribedType(descriptor, filter)); |
| 126 | + |
| 127 | + return self(); |
| 128 | + } |
| 129 | + |
102 | 130 | /**
|
103 | 131 | * @return the configured default outcome as a {@link DeliveryState} instance.
|
104 | 132 | */
|
@@ -139,4 +167,54 @@ public SourceOptions outcomes(DeliveryState.Type... outcomes) {
|
139 | 167 | SourceOptions self() {
|
140 | 168 | return this;
|
141 | 169 | }
|
| 170 | + |
| 171 | + private static class FilterDescribedType implements DescribedType { |
| 172 | + |
| 173 | + private final Object descriptor; |
| 174 | + private final Object described; |
| 175 | + |
| 176 | + public FilterDescribedType(Object descriptor, Object described) { |
| 177 | + this.descriptor = descriptor; |
| 178 | + this.described = described; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public Object getDescriptor() { |
| 183 | + return descriptor; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public Object getDescribed() { |
| 188 | + return this.described; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public String toString() { |
| 193 | + return "FilterDescribedType{ descriptor:" + descriptor + ", described:" + described + " }"; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public int hashCode() { |
| 198 | + return Objects.hash(described, descriptor); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public boolean equals(Object target) { |
| 203 | + if (this == target) { |
| 204 | + return true; |
| 205 | + } |
| 206 | + |
| 207 | + if (target == null) { |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + if (!(target instanceof DescribedType)) { |
| 212 | + return false; |
| 213 | + } |
| 214 | + |
| 215 | + final DescribedType other = (DescribedType) target; |
| 216 | + |
| 217 | + return Objects.equals(descriptor, other.getDescriptor()) && Objects.equals(described, other.getDescribed()); |
| 218 | + } |
| 219 | + } |
142 | 220 | }
|
0 commit comments