Skip to content

Commit

Permalink
Add org.apache.commons.collections4.properties.OrderedProperties and
Browse files Browse the repository at this point in the history
OrderedPropertiesFactory
  • Loading branch information
garydgregory committed Jul 13, 2023
1 parent d3dbdc8 commit 452533a
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@
<suppressionsLocation>${basedir}/src/conf/checkstyle-suppressions.xml</suppressionsLocation>
<enableRulesSummary>false</enableRulesSummary>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<resourceExcludes>NOTICE.txt,LICENSE.txt,**/pom.properties,**/test.properties,**/resolver-status.properties</resourceExcludes>
<resourceExcludes>NOTICE.txt,LICENSE.txt,**/pom.properties,**/test*.properties,**/resolver-status.properties</resourceExcludes>
</configuration>
</plugin>
<plugin>
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@
<action issue="COLLECTIONS-746" dev="ggregory" type="add" due-to="Gary Gregory">
Add PropertiesFactory.
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add org.apache.commons.collections4.properties.OrderedProperties and OrderedPropertiesFactory.
</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory, Dependabot">
Bump org.easymock:easymock from 4.0.2 to 5.1.0 #352, #355, #375.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.commons.collections4.properties;

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* A drop-in replacement for {@link Properties} for ordered keys.
* <p>
* Overrides methods to keep keys in insertion order. Allows other methods in the superclass to work with ordered keys.
* </p>
*
* @see OrderedPropertiesFactory#INSTANCE
* @since 4.5
*/
public class OrderedProperties extends Properties {

private static final long serialVersionUID = 1L;

/**
* Preserves the insertion order.
*/
private final LinkedHashSet<Object> orderedKeys = new LinkedHashSet<>();

@Override
public synchronized void clear() {
orderedKeys.clear();
super.clear();
}

@Override
public synchronized Object compute(Object key, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
Object compute = super.compute(key, remappingFunction);
if (compute != null) {
orderedKeys.add(key);
}
return compute;
}

@Override
public synchronized Object computeIfAbsent(Object key, Function<? super Object, ? extends Object> mappingFunction) {
Object computeIfAbsent = super.computeIfAbsent(key, mappingFunction);
if (computeIfAbsent != null) {
orderedKeys.add(key);
}
return computeIfAbsent;
}

@Override
public synchronized Enumeration<Object> keys() {
return Collections.enumeration(orderedKeys);
}

@Override
public Set<Object> keySet() {
return orderedKeys;
}

@Override
public synchronized Object merge(final Object key, final Object value,
final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
orderedKeys.add(key);
return super.merge(key, value, remappingFunction);
}

@Override
public Enumeration<?> propertyNames() {
return Collections.enumeration(orderedKeys);
}

@Override
public synchronized Object put(final Object key, final Object value) {
Object put = super.put(key, value);
if (put == null) {
orderedKeys.add(key);
}
return put;
}

@Override
public synchronized void putAll(final Map<? extends Object, ? extends Object> t) {
orderedKeys.addAll(t.keySet());
super.putAll(t);
}

@Override
public synchronized Object putIfAbsent(final Object key, final Object value) {
Object putIfAbsent = super.putIfAbsent(key, value);
if (putIfAbsent == null) {
orderedKeys.add(key);
}
return putIfAbsent;
}

@Override
public synchronized Object remove(final Object key) {
Object remove = super.remove(key);
if (remove != null) {
orderedKeys.remove(key);
}
return remove;
}

@Override
public synchronized boolean remove(final Object key, final Object value) {
boolean remove = super.remove(key, value);
if (remove) {
orderedKeys.remove(key);
}
return remove;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.commons.collections4.properties;

/**
* Creates and loads {@link OrderedProperties}.
*
* @see OrderedProperties
* @since 4.5
*/
public class OrderedPropertiesFactory extends AbstractPropertiesFactory<OrderedProperties> {

/**
* The singleton instance.
*/
public static final OrderedPropertiesFactory INSTANCE = new OrderedPropertiesFactory();

/**
* Constructs an instance.
*/
private OrderedPropertiesFactory() {
// There is only one instance.
}

/**
* Subclasses override to provide customized properties instances.
*
* @return a new Properties instance.
*/
@Override
protected OrderedProperties createProperties() {
return new OrderedProperties();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ private void assertContents(final T properties) {
assertEquals("value11", properties.getProperty("key11"));
}

private boolean isXmlTest(final String fileExtension) {
return ".xml".equals(fileExtension);
}

private String getPathString(final String fileExtension) {
return BulkTest.TEST_PROPERTIES_PATH + "test" + fileExtension;
}

private boolean isXmlTest(final String fileExtension) {
return ".xml".equals(fileExtension);
}

@Test
public void testInstance() {
assertNotNull(PropertiesFactory.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.commons.collections4.properties;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

/**
* Tests {@link OrderedPropertiesFactory}.
*/
public class OrderedPropertiesFactoryTest extends AbstractPropertiesFactoryTest<OrderedProperties> {

public OrderedPropertiesFactoryTest() {
super(OrderedPropertiesFactory.INSTANCE);
}

@Test
@Override
public void testInstance() {
assertNotNull(OrderedPropertiesFactory.INSTANCE);
}

}
Loading

0 comments on commit 452533a

Please sign in to comment.