ibessonov commented on a change in pull request #208: URL: https://github.com/apache/ignite-3/pull/208#discussion_r672067449
########## 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) Review comment: Done -- 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]
