|
| 1 | +/* |
| 2 | + * * Copyright (C) 2013-2021 Matt Baxter https://kitteh.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, copy, |
| 8 | + * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | + * of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package org.kitteh.irc.client.library.feature.filter; |
| 25 | + |
| 26 | +import net.engio.mbassy.listener.Filter; |
| 27 | +import net.engio.mbassy.listener.IMessageFilter; |
| 28 | +import net.engio.mbassy.subscription.SubscriptionContext; |
| 29 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 30 | +import org.kitteh.irc.client.library.event.helper.ServerMessageEvent; |
| 31 | + |
| 32 | +import java.lang.annotation.ElementType; |
| 33 | +import java.lang.annotation.Repeatable; |
| 34 | +import java.lang.annotation.Retention; |
| 35 | +import java.lang.annotation.RetentionPolicy; |
| 36 | +import java.lang.annotation.Target; |
| 37 | + |
| 38 | +/** |
| 39 | + * Indicates a specific tag to listen to for any {@link ServerMessageEvent}. |
| 40 | + * This annotation can be repeated. |
| 41 | + * |
| 42 | + * The below code only listens to messages with a label: |
| 43 | + * <pre> |
| 44 | + * {@code @TagFilter("label")} |
| 45 | + * {@code @Handler)} |
| 46 | + * public void privmsg(ClientReceiveCommandEvent event) { |
| 47 | + * System.out.println("It's a labeled response!"); |
| 48 | + * } |
| 49 | + * </pre> |
| 50 | + */ |
| 51 | +@Filter(TagFilter.Processor.class) |
| 52 | +@Repeatable(TagFilter.Tags.class) |
| 53 | +@Retention(RetentionPolicy.RUNTIME) |
| 54 | +@Target(ElementType.METHOD) |
| 55 | +public @interface TagFilter { |
| 56 | + /** |
| 57 | + * Processes this annotation-based filter. |
| 58 | + */ |
| 59 | + class Processor implements FilterProcessor<ServerMessageEvent, TagFilter>, IMessageFilter<ServerMessageEvent> { |
| 60 | + /** |
| 61 | + * Constructs the processor. |
| 62 | + */ |
| 63 | + public Processor() { |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean accepts(@NonNull ServerMessageEvent event, @NonNull TagFilter[] tagFilters) { |
| 68 | + for (TagFilter tagFilter : tagFilters) { |
| 69 | + if (event.getTag(tagFilter.value()).isPresent()) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean accepts(ServerMessageEvent event, SubscriptionContext context) { |
| 78 | + return this.accepts(event, context.getHandler().getMethod().getAnnotationsByType(TagFilter.class)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * This is an annotation for storing repeated TagFilter annotations. |
| 84 | + * Just use the TagFilter annotation instead, multiple times! |
| 85 | + */ |
| 86 | + @Retention(RetentionPolicy.RUNTIME) |
| 87 | + @Target(ElementType.METHOD) |
| 88 | + @interface Tags { |
| 89 | + /** |
| 90 | + * Gets the stored annotations. |
| 91 | + * |
| 92 | + * @return stored annotations |
| 93 | + */ |
| 94 | + @NonNull TagFilter[] value(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * The tag to listen to. |
| 99 | + * |
| 100 | + * @return the tag |
| 101 | + */ |
| 102 | + @NonNull String value(); |
| 103 | +} |
0 commit comments