Skip to content

Commit

Permalink
implemented change req. #46 propertyNames()
Browse files Browse the repository at this point in the history
in interface Accessible

    Set<String> propertyNames();

This differs from the correspondent method in java.util.Properties
since Properties.propertyNames returns an Enumeration<?> but property
names are required to be Strings. In JDK 1.6 the method
`Set<String> stringPropertyNames()` has been introduced but since
we require JDK 1.5+ support I couldn't just delegate and I had to
reimplement it. So I chosed to implement it with the proper method
signature, even though it doesn't match java.util.Properties
interface.
  • Loading branch information
Luigi R. Viggiano committed Jul 29, 2013
1 parent 28a5611 commit b93b2f0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/aeonbits/owner/Accessible.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Set;

/**
* <p>Allows a <tt>Config</tt> object to access the contents of the properties, providing utility methods to perform
Expand Down Expand Up @@ -98,4 +99,7 @@ public interface Accessible {

//TODO: javadocs
void storeToXML(OutputStream os, String comment) throws IOException;

//TODO: javadocs
Set<String> propertyNames();
}
20 changes: 18 additions & 2 deletions src/main/java/org/aeonbits/owner/PropertiesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.util.Collections.synchronizedList;
import static org.aeonbits.owner.Config.LoadType;
import static org.aeonbits.owner.Config.LoadType.FIRST;
import static org.aeonbits.owner.PropertiesMapper.defaults;
Expand Down Expand Up @@ -65,7 +68,7 @@ class PropertiesManager implements Reloadable, Accessible, Mutable {

private volatile boolean loading = false;

private final List<ReloadListener> reloadListeners = Collections.synchronizedList(new LinkedList<ReloadListener>());
private final List<ReloadListener> reloadListeners = synchronizedList(new LinkedList<ReloadListener>());
private Object proxy;
private final LoadersManager loaders;

Expand Down Expand Up @@ -211,6 +214,19 @@ public void storeToXML(OutputStream os, String comment) throws IOException {
}
}

@Delegate
public Set<String> propertyNames() {
readLock.lock();
try {
LinkedHashSet<String> result = new LinkedHashSet<String>();
for (Enumeration<?> propertyNames = properties.propertyNames(); propertyNames.hasMoreElements();)
result.add((String)propertyNames.nextElement());
return result;
} finally {
readLock.unlock();
}
}

@Delegate
public void list(PrintStream out) {
readLock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
Expand All @@ -57,7 +61,7 @@ public static interface AccessibleConfig extends Config, Accessible {
@Test
public void testListPrintStream() throws IOException {
ByteArrayOutputStream expected = new ByteArrayOutputStream();
PropertiesManagerForTest manager =
PropertiesManagerForTest manager =
new PropertiesManagerForTest(AccessibleConfig.class, new Properties(), scheduler, expander, loaders);
manager.load().list(new PrintStream(expected, true));

Expand All @@ -71,7 +75,7 @@ public void testListPrintStream() throws IOException {
@Test
public void testListPrintWriter() throws IOException {
StringWriter expected = new StringWriter();
PropertiesManagerForTest manager =
PropertiesManagerForTest manager =
new PropertiesManagerForTest(AccessibleConfig.class, new Properties(), scheduler, expander, loaders);
manager.load().list(new PrintWriter(expected, true));

Expand Down Expand Up @@ -115,4 +119,12 @@ public void testGetPropertyWithDefaultThatDoesNotExists() throws IOException {
assertEquals("Hello", cfg.getProperty("salutation.text.nonexistent", "Hello"));
}

@Test
public void testStringPropertyNames() throws Throwable {
AccessibleConfig cfg = ConfigFactory.create(AccessibleConfig.class);
Set<String> propNames = cfg.propertyNames();
assertThat(propNames.size(), is(2));
assertThat(propNames, containsInAnyOrder("favoriteSong", "salutation.text"));
}

}

0 comments on commit b93b2f0

Please sign in to comment.