Skip to content

Commit

Permalink
Reduced the footprint of IniSection, used by SwordBookMetaData.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsmith committed Jan 15, 2016
1 parent 90c7bbc commit 24343c3
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions src/main/java/org/crosswire/common/util/IniSection.java
Expand Up @@ -34,10 +34,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* A utility class for a section of an INI style configuration file.
Expand Down Expand Up @@ -96,8 +96,7 @@ public IniSection() {
*/
public IniSection(String name) {
this.name = name;
section = new TreeMap<String, ListSet<String>>(String.CASE_INSENSITIVE_ORDER);
list = new ArrayList<String>();
section = new HashMap<String, List<String>>();
warnings = new StringBuilder();
}

Expand All @@ -108,8 +107,7 @@ public IniSection(String name) {
*/
public IniSection(IniSection config) {
this.name = config.getName();
section = new TreeMap<String, ListSet<String>>(String.CASE_INSENSITIVE_ORDER);
list = new ArrayList<String>();
section = new HashMap<String, List<String>>();
for (String key : config.getKeys()) {
for (String value : config.getValues(key)) {
add(key, value);
Expand All @@ -121,7 +119,9 @@ public IniSection(IniSection config) {
*/
public void clear() {
section.clear();
list.clear();
warnings.setLength(0);
warnings.trimToSize();
report = "";
}

/**
Expand All @@ -148,7 +148,7 @@ public String getName() {
* @return the count
*/
public int size() {
return list.size();
return section.size();
}

/**
Expand All @@ -157,19 +157,19 @@ public int size() {
* @return {@code true} if this section is empty
*/
public boolean isEmpty() {
return size() == 0;
return section.isEmpty();
}

public Iterator iterator() {
return list.iterator();
return section.keySet().iterator();
}
/**
* Get the unmodifiable ordered list of keys.
* Get the unmodifiable unordered list of keys.
*
* @return the set of keys
*/
public List<String> getKeys() {
return Collections.unmodifiableList(list);
public Collection<String> getKeys() {
return Collections.unmodifiableSet(section.keySet());
}

/**
Expand Down Expand Up @@ -252,7 +252,7 @@ public Collection<String> getValues(String key) {
* @return the number of values for a key or 0 if the key does not exist.
*/
public int size(String key) {
ListSet<String> values = section.get(key);
Collection<String> values = section.get(key);
return values == null ? 0 : values.size();
}

Expand All @@ -265,7 +265,7 @@ public int size(String key) {
* @throws ArrayIndexOutOfBoundsException when the index is out of bounds
*/
public String get(String key, int index) {
ListSet<String> values = section.get(key);
List<String> values = section.get(key);
return values == null ? null : values.get(index);
}

Expand All @@ -276,12 +276,12 @@ public String get(String key, int index) {
* @return the value at the specified index or null
*/
public String get(String key) {
ListSet<String> values = section.get(key);
List<String> values = section.get(key);
return values == null ? null : values.get(0);
}

public String get(String key, String defaultValue) {
ListSet<String> values = section.get(key);
List<String> values = section.get(key);
return values == null ? defaultValue : values.get(0);
}

Expand All @@ -303,27 +303,25 @@ public boolean remove(String key, String value) {
if (changed) {
if (values.isEmpty()) {
section.remove(key);
list.remove(key);
}
}

return changed;
}

/**
* Remove the value if present.
* If it were the last value for the key, the key is removed.
* Remove the key and all its values, if present.
*
* @param key the key for the section
* @return whether the value was present and removed
* @return whether the key was present and removed
*/
public boolean remove(String key) {
Collection<String> values = section.get(key);
if (values == null) {
return false;
}
section.remove(key);
return list.remove(key);
return true;
}

/**
Expand Down Expand Up @@ -385,6 +383,7 @@ public void load(InputStream is, String encoding, Filter<String> filter) throws
public void load(File file, String encoding) throws IOException {
load(file, encoding, null);
}

/**
* Load the INI from a file using the given encoding. Filter keys as specified.
*
Expand Down Expand Up @@ -496,7 +495,7 @@ public void save(Writer out) {
writer.println();

boolean first = true;
Iterator<String> keys = list.iterator();
Iterator<String> keys = section.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Collection<String> values = section.get(key);
Expand Down Expand Up @@ -542,11 +541,10 @@ private String format(final String value) {
}

private Collection<String> getOrCreateValues(final String key) {
ListSet<String> values = section.get(key);
List<String> values = section.get(key);
if (values == null) {
values = new ListSet<String>(String.CASE_INSENSITIVE_ORDER);
values = new ArrayList<String>();
section.put(key, values);
list.add(key);
}
return values;
}
Expand Down Expand Up @@ -725,14 +723,9 @@ private boolean allowed(String key, String value) {
private String name;

/**
* A map of sections by section names.
*/
private Map<String, ListSet<String>> section;

/**
* Indexed list of sections maintaining insertion order.
* A map of values by key names.
*/
private List<String> list;
private Map<String, List<String>> section;

private File configFile;

Expand Down

0 comments on commit 24343c3

Please sign in to comment.