/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed 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.jetbrains.osgi.jps.util;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.*;
/**
* Implementation of {@link Properties} which has a defined order of elements.
* Properties will be returned in the same order in which they are inserted.
*/
public class OrderedProperties extends Properties {
private final Set<Object> myKeys = ContainerUtil.newLinkedHashSet();
@NotNull
public static OrderedProperties fromMap(@NotNull Map<String, String> map) {
OrderedProperties result = new OrderedProperties();
for (Map.Entry<String, String> entry : map.entrySet()) {
result.setProperty(entry.getKey(), entry.getValue());
}
return result;
}
@NotNull
public Map<String, String> toMap() {
Map<String, String> result = new LinkedHashMap<String, String>(size());
for (String name : stringPropertyNames()) {
result.put(name, getProperty(name));
}
return result;
}
@Override
public synchronized Enumeration<Object> keys() {
return Collections.enumeration(myKeys);
}
@Override
public Enumeration<?> propertyNames() {
return Collections.enumeration(myKeys);
}
@SuppressWarnings("unchecked")
@Override
public Set<String> stringPropertyNames() {
return (Set)Collections.unmodifiableSet(myKeys);
}
@SuppressWarnings("UseOfPropertiesAsHashtable")
@Override
public synchronized Object put(Object key, Object value) {
myKeys.add(key);
return super.put(key, value);
}
@Override
public synchronized Object remove(Object key) {
myKeys.remove(key);
return super.remove(key);
}
@Override
public synchronized void clear() {
myKeys.clear();
super.clear();
}
}