ibessonov commented on a change in pull request #208:
URL: https://github.com/apache/ignite-3/pull/208#discussion_r672051729



##########
File path: 
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/tree/OrderedMap.java
##########
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.tree;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Simplified map class that preserves keys order.
+ *
+ * @param <V> Type of the value.
+ */
+class OrderedMap<V> {
+    /** Underlying hash map. */
+    private final Map<String, V> map;
+
+    /** Ordered keys. */
+    private final List<String> orderedKeys;
+
+    /** Default constructor. */
+    OrderedMap() {
+        map = new HashMap<>();
+        orderedKeys = new ArrayList<>();
+    }
+
+    /**
+     * Copy constructor.
+     *
+     * @param other Source of keys/values to copy from.
+     */
+    OrderedMap(OrderedMap<V> other) {
+        map = new HashMap<>(other.map);
+        orderedKeys = new ArrayList<>(other.orderedKeys);
+    }
+
+    /**
+     * Same as {@link Map#containsKey(Object)}.
+     *
+     * @param key Key to check.
+     * @return {@code true} if map contains the key.
+     */
+    public boolean containsKey(String key) {
+        return map.containsKey(key);
+    }
+
+    /**
+     * Same as {@link Map#get(Object)}.
+     *
+     * @param key Key to search.
+     * @return Value associated with the key or {@code null} is it's not found.
+     */
+    public V get(String key) {
+        return map.get(key);
+    }
+
+    /**
+     * Same as {@link Map#remove(Object)}.
+     *
+     * @param key Key to remove.
+     * @return Previous value associated with the key or {@code null} if the 
map had no such key.
+     */
+    public V remove(String key) {
+        if (map.containsKey(key))
+            orderedKeys.remove(key);
+
+        return map.remove(key);
+    }
+
+    /**
+     * Inserts a value into the map under the specified key. If the key was 
not present in the map, it will be ordered last.
+     * ordering index will be used.
+     *
+     * @param key Key to put.
+     * @param value Value associated with the key.
+     */
+    public void put(String key, V value) {
+        if (map.put(key, value) == null)
+            orderedKeys.add(key);
+    }
+
+    /**
+     * Inserts a value into the map under the specified key. The key will be 
positioned at the given index, shifting any
+     * existing values at that position to the right. Key must not be present 
in the map when the method is called.
+     *
+     * @param idx Ordering index for the key. Must be in {@code [0 .. size()]} 
range.
+     * @param key Key to put.
+     * @param value Value associated with the key.
+     */
+    public void putByIndex(int idx, String key, V value) {
+        assert !map.containsKey(key) : key + " " + map;
+
+        if (idx >= orderedKeys.size())
+            orderedKeys.add(key);
+        else
+            orderedKeys.add(idx, key);
+
+        map.put(key, value);
+    }
+
+    /**
+     * Put value to the map at the position after the {@code precedingKey}. 
Key must not be present in the map when the
+     * method is called.
+     *
+     * @param precedingKey Previous key for the new key. Last key will be used 
if this one is missing from the map.
+     * @param key Key to put.
+     * @param value Value associated with the key.
+     */
+    public void putAfter(String precedingKey, String key, V value) {
+        assert !map.containsKey(key) : key + " " + map;
+
+        int idx = orderedKeys.indexOf(precedingKey);
+
+        putByIndex(idx < 0 ? orderedKeys.size() : idx + 1, key, value);
+    }
+
+    /**
+     * Re-associates the value under the {@code oldKey} to the {@code newKey}. 
Does nothing if the {@code oldKey}
+     * is not present in the map.
+     * was missing from the map.
+     *
+     * @param oldKey Old key.
+     * @param newKey New key.
+     *
+     * @throws IllegalArgumentException If both {@code oldKey} and {@code 
newKey} already exist in the map.
+     */
+    public void rename(String oldKey, String newKey) {

Review comment:
       OrderedMap class has very loose contract. "rename" is not yet used 
anywhere except tests and will probably be changed in the future.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to