|
| 1 | +/* |
| 2 | + * Copyright 2018 dc-square and the HiveMQ MQTT Client Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.hivemq.client.internal.mqtt.datatypes; |
| 19 | + |
| 20 | +import com.hivemq.client.internal.util.ByteArrayUtil; |
| 21 | +import com.hivemq.client.mqtt.datatypes.MqttTopic; |
| 22 | +import com.hivemq.client.mqtt.datatypes.MqttTopicFilter; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.NoSuchElementException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Iterator for a topic or topic filter. |
| 30 | + * <p> |
| 31 | + * equals and hashCode match the current level. |
| 32 | + * |
| 33 | + * @author Silvio Giebl |
| 34 | + */ |
| 35 | +public class MqttTopicIterator extends MqttTopicLevel { |
| 36 | + |
| 37 | + public static @NotNull MqttTopicIterator of(final @NotNull MqttTopicImpl topic) { |
| 38 | + final byte[] binary = topic.toBinary(); |
| 39 | + return new MqttTopicIterator(binary, -1, -1, binary.length); |
| 40 | + } |
| 41 | + |
| 42 | + public static @NotNull MqttTopicIterator of(final @NotNull MqttTopicFilterImpl topicFilter) { |
| 43 | + final byte[] binary = topicFilter.toBinary(); |
| 44 | + final int start = topicFilter.getFilterByteStart() - 1; |
| 45 | + return new MqttTopicIterator( |
| 46 | + binary, start, start, topicFilter.containsMultiLevelWildcard() ? (binary.length - 2) : binary.length); |
| 47 | + } |
| 48 | + |
| 49 | + private int start; |
| 50 | + private int end; |
| 51 | + private final int allEnd; |
| 52 | + |
| 53 | + private MqttTopicIterator(final @NotNull byte[] array, final int start, final int end, final int allEnd) { |
| 54 | + super(array); |
| 55 | + this.start = start; |
| 56 | + this.end = end; |
| 57 | + this.allEnd = allEnd; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected int getStart() { |
| 62 | + return start; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected int getEnd() { |
| 67 | + return end; |
| 68 | + } |
| 69 | + |
| 70 | + public boolean hasNext() { |
| 71 | + return end != allEnd; |
| 72 | + } |
| 73 | + |
| 74 | + public boolean hasMultiLevelWildcard() { |
| 75 | + return allEnd != array.length; |
| 76 | + } |
| 77 | + |
| 78 | + public @NotNull MqttTopicIterator fork() { |
| 79 | + return new MqttTopicIterator(array, start, end, allEnd); |
| 80 | + } |
| 81 | + |
| 82 | + public @NotNull MqttTopicLevel next() { |
| 83 | + if (!hasNext()) { |
| 84 | + throw new NoSuchElementException(); |
| 85 | + } |
| 86 | + start = end + 1; |
| 87 | + end = ByteArrayUtil.indexOf(array, start, (byte) MqttTopic.TOPIC_LEVEL_SEPARATOR); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public @NotNull MqttTopicLevel trim() { |
| 93 | + if (!hasNext()) { |
| 94 | + return MqttTopicLevel.of(array, start, end); |
| 95 | + } |
| 96 | + final int start = this.start; |
| 97 | + final int end = this.end; |
| 98 | + this.start = this.end = allEnd; |
| 99 | + return new MqttTopicLevels(Arrays.copyOfRange(array, start, allEnd), end - start); |
| 100 | + } |
| 101 | + |
| 102 | + public boolean forwardIfEqual(final @NotNull MqttTopicLevels topicLevels) { |
| 103 | + final byte[] topicLevelsArray = topicLevels.getArray(); |
| 104 | + final int topicLevelsEnd = topicLevels.getEnd(); |
| 105 | + final int to = end + topicLevelsArray.length - topicLevelsEnd; |
| 106 | + if ((to <= allEnd) && ByteArrayUtil.equals(array, end + 1, to, topicLevelsArray, topicLevelsEnd + 1, |
| 107 | + topicLevelsArray.length)) { |
| 108 | + start = end = to; |
| 109 | + return true; |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + public int forwardWhileEqual(final @NotNull MqttTopicLevels levels) { |
| 115 | + if (!hasNext()) { |
| 116 | + return levels.getEnd(); |
| 117 | + } |
| 118 | + int branchIndex = end; |
| 119 | + int levelsBranchIndex = levels.getEnd(); |
| 120 | + int index = branchIndex + 1; |
| 121 | + int levelsIndex = levelsBranchIndex + 1; |
| 122 | + final byte[] levelsArray = levels.getArray(); |
| 123 | + while (true) { |
| 124 | + final boolean isEnd = index == allEnd; |
| 125 | + final boolean isLevelsEnd = levelsIndex == levelsArray.length; |
| 126 | + if (isLevelsEnd || isEnd) { |
| 127 | + if ((isLevelsEnd || (levelsArray[levelsIndex] == MqttTopicImpl.TOPIC_LEVEL_SEPARATOR)) && |
| 128 | + (isEnd || (array[index] == MqttTopicImpl.TOPIC_LEVEL_SEPARATOR))) { |
| 129 | + branchIndex = index; |
| 130 | + levelsBranchIndex = levelsIndex; |
| 131 | + } |
| 132 | + break; |
| 133 | + } |
| 134 | + final byte lb = levelsArray[levelsIndex]; |
| 135 | + if (array[index] == lb) { |
| 136 | + if (lb == MqttTopicImpl.TOPIC_LEVEL_SEPARATOR) { |
| 137 | + branchIndex = index; |
| 138 | + levelsBranchIndex = levelsIndex; |
| 139 | + } |
| 140 | + index++; |
| 141 | + levelsIndex++; |
| 142 | + } else { |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + start = end = branchIndex; |
| 147 | + return levelsBranchIndex; |
| 148 | + } |
| 149 | + |
| 150 | + public boolean forwardIfMatch(final @NotNull MqttTopicLevels levels) { |
| 151 | + if (!hasNext()) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + int index = end + 1; |
| 155 | + int levelsIndex = levels.getEnd() + 1; |
| 156 | + final byte[] levelsArray = levels.getArray(); |
| 157 | + while (true) { |
| 158 | + final boolean isEnd = index == allEnd; |
| 159 | + final boolean isLevelsEnd = levelsIndex == levelsArray.length; |
| 160 | + if (isLevelsEnd) { |
| 161 | + if (isEnd || (array[index] == MqttTopicImpl.TOPIC_LEVEL_SEPARATOR)) { |
| 162 | + start = end = index; |
| 163 | + return true; |
| 164 | + } |
| 165 | + return false; |
| 166 | + } |
| 167 | + if (isEnd) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + final byte lb = levelsArray[levelsIndex]; |
| 171 | + if (array[index] == lb) { |
| 172 | + index++; |
| 173 | + levelsIndex++; |
| 174 | + } else if (lb == MqttTopicFilter.SINGLE_LEVEL_WILDCARD) { |
| 175 | + while ((index < allEnd) && (array[index] != MqttTopicImpl.TOPIC_LEVEL_SEPARATOR)) { |
| 176 | + index++; |
| 177 | + } |
| 178 | + levelsIndex++; |
| 179 | + } else { |
| 180 | + return false; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments