ijuma commented on a change in pull request #11311:
URL: https://github.com/apache/kafka/pull/11311#discussion_r712521701



##########
File path: metadata/src/main/java/org/apache/kafka/image/TopicsImage.java
##########
@@ -92,6 +98,148 @@ public int hashCode() {
         return Objects.hash(topicsById, topicsByName);
     }
 
+    /**
+     * Expose a view of this TopicsImage as a map from topic names to IDs.
+     *
+     * Like TopicsImage itself, this map is immutable.
+     */
+    public Map<String, Uuid> topicNameToIdView() {
+        return new TopicNameToIdMap();
+    }
+
+    class TopicNameToIdMap extends AbstractMap<String, Uuid> {
+        private final TopicNameToIdMapEntrySet set = new 
TopicNameToIdMapEntrySet();
+
+        @Override
+        public boolean containsKey(Object key) {
+            return topicsByName.containsKey(key);
+        }
+
+        @Override
+        public Uuid get(Object key) {
+            TopicImage image = topicsByName.get(key);
+            if (image == null) return null;
+            return image.id();
+        }
+
+        @Override
+        public Set<Entry<String, Uuid>> entrySet() {
+            return set;
+        }
+    }
+
+    class TopicNameToIdMapEntrySet extends AbstractSet<Entry<String, Uuid>> {
+        @Override
+        public Iterator<Entry<String, Uuid>> iterator() {
+            return new 
TopicNameToIdMapEntrySetIterator(topicsByName.entrySet().iterator());
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        public boolean contains(Object o) {
+            if (!(o instanceof Entry)) return false;
+            Entry other = (Entry) o;
+            TopicImage image = topicsByName.get(other.getKey());
+            if (image == null) return false;
+            return image.id().equals(other.getValue());
+        }
+
+        @Override
+        public int size() {
+            return topicsByName.size();
+        }
+    }
+
+    static class TopicNameToIdMapEntrySetIterator implements 
Iterator<Entry<String, Uuid>> {
+        private final Iterator<Entry<String, TopicImage>> iterator;
+
+        TopicNameToIdMapEntrySetIterator(Iterator<Entry<String, TopicImage>> 
iterator) {
+            this.iterator = iterator;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return this.iterator.hasNext();
+        }
+
+        @Override
+        public Entry<String, Uuid> next() {
+            Entry<String, TopicImage> entry = iterator.next();
+            return new SimpleImmutableEntry<>(entry.getKey(), 
entry.getValue().id());
+        }
+    }
+
+    /**
+     * Expose a view of this TopicsImage as a map from IDs to names.
+     *
+     * Like TopicsImage itself, this map is immutable.
+     */
+    public Map<Uuid, String> topicIdToNameView() {
+        return new TopicIdToNameMap();
+    }
+
+    class TopicIdToNameMap extends AbstractMap<Uuid, String> {
+        private final TopicIdToNameMapEntrySet set = new 
TopicIdToNameMapEntrySet();
+
+        @Override
+        public boolean containsKey(Object key) {
+            return topicsById.containsKey(key);
+        }
+
+        @Override
+        public String get(Object key) {
+            TopicImage image = topicsById.get(key);
+            if (image == null) return null;
+            return image.name();
+        }
+
+        @Override
+        public Set<Entry<Uuid, String>> entrySet() {
+            return set;
+        }
+    }
+
+    class TopicIdToNameMapEntrySet extends AbstractSet<Entry<Uuid, String>> {

Review comment:
       That's basically `map` right?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to