|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.client.impl.conf; |
| 20 | + |
| 21 | +import java.io.Serializable; |
| 22 | +import java.util.regex.Pattern; |
| 23 | +import lombok.AllArgsConstructor; |
| 24 | +import lombok.Data; |
| 25 | +import lombok.NoArgsConstructor; |
| 26 | +import lombok.NonNull; |
| 27 | +import lombok.RequiredArgsConstructor; |
| 28 | + |
| 29 | +@Data |
| 30 | +@NoArgsConstructor |
| 31 | +@AllArgsConstructor |
| 32 | +public class TopicConsumerConfigurationData implements Serializable { |
| 33 | + private static final long serialVersionUID = 1L; |
| 34 | + |
| 35 | + private TopicNameMatcher topicNameMatcher; |
| 36 | + private int priorityLevel; |
| 37 | + |
| 38 | + public static TopicConsumerConfigurationData ofTopicsPattern(@NonNull Pattern topicsPattern, int priorityLevel) { |
| 39 | + return of(new TopicNameMatcher.TopicsPattern(topicsPattern), priorityLevel); |
| 40 | + } |
| 41 | + |
| 42 | + public static TopicConsumerConfigurationData ofTopicsPattern(@NonNull Pattern topicsPattern, |
| 43 | + ConsumerConfigurationData<?> conf) { |
| 44 | + return ofTopicsPattern(topicsPattern, conf.getPriorityLevel()); |
| 45 | + } |
| 46 | + |
| 47 | + public static TopicConsumerConfigurationData ofTopicName(@NonNull String topicName, int priorityLevel) { |
| 48 | + return of(new TopicNameMatcher.TopicName(topicName), priorityLevel); |
| 49 | + } |
| 50 | + |
| 51 | + public static TopicConsumerConfigurationData ofTopicName(@NonNull String topicName, |
| 52 | + ConsumerConfigurationData<?> conf) { |
| 53 | + return ofTopicName(topicName, conf.getPriorityLevel()); |
| 54 | + } |
| 55 | + |
| 56 | + static TopicConsumerConfigurationData of(@NonNull TopicNameMatcher topicNameMatcher, int priorityLevel) { |
| 57 | + return new TopicConsumerConfigurationData(topicNameMatcher, priorityLevel); |
| 58 | + } |
| 59 | + |
| 60 | + public interface TopicNameMatcher extends Serializable { |
| 61 | + boolean matches(String topicName); |
| 62 | + |
| 63 | + @RequiredArgsConstructor |
| 64 | + class TopicsPattern implements TopicNameMatcher { |
| 65 | + @NonNull |
| 66 | + private final Pattern topicsPattern; |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean matches(String topicName) { |
| 70 | + return topicsPattern.matcher(topicName).matches(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @RequiredArgsConstructor |
| 75 | + class TopicName implements TopicNameMatcher { |
| 76 | + @NonNull |
| 77 | + private final String topicName; |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean matches(String topicName) { |
| 81 | + return this.topicName.equals(topicName); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments