Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/org/codejive/properties/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,50 @@ public String put(String key, String value) {
return values.put(key, value);
}

/**
* Associates the specified value with the specified key at the position indicated by the
* cursor. The cursor must point to the start of a line, or to the end of the token list. If the
* key already exists, its value is replaced at its existing position.
*
* @param pos position at which to insert the property
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with key, or null if there was no mapping for key
* @throws IllegalArgumentException if the cursor is not at a valid insertion position
*/
public String put(Cursor pos, String key, String value) {
Objects.requireNonNull(pos, "pos");
if (key == null || value == null) {
throw new NullPointerException();
}
String rawValue = escapeValue(value);
if (values.containsKey(key)) {
replaceValue(key, rawValue, value);
} else {
String rawKey = escapeKey(key);
addNewKeyValue(pos, rawKey, key, rawValue, value);
}
return values.put(key, value);
}

/**
* Associates the specified value with the specified key at the position indicated by the
* cursor. The cursor must point to the start of a line, or to the end of the token list. If the
* key already exists, its value is replaced at its existing position. If any comment lines are
* supplied they will be prepended to the property.
*
* @param pos position at which to insert the property
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @param comment comment lines to be associated with the specified key
* @return the previous value associated with key, or null if there was no mapping for key
*/
public String putCommented(Cursor pos, String key, String value, String... comment) {
String old = put(pos, key, value);
setComment(key, comment);
return old;
}

/**
* Associates the specified value with the specified key in this properties table. If the
* properties previously contained a mapping for the key, the old value is replaced. If any
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/codejive/properties/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,31 @@ void testPutNew() throws IOException, URISyntaxException {
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-putnew.properties")));
}

@Test
void testPutAtPosition() throws IOException {
Properties p = Properties.loadProperties(new StringReader("alpha=1\nbeta=2\n"));

assertThat(p.put(p.beforeProperty("beta"), "middle", "3")).isNull();

StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString()).isEqualTo("alpha=1\nmiddle=3\nbeta=2\n");
assertThat(p).containsEntry("middle", "3");
}

@Test
void testPutAtInvalidPosition() throws IOException {
Properties p = Properties.loadProperties(new StringReader("alpha=1\nbeta=2\n"));

Cursor separator = p.indexOf("beta").next();
assertThat(p.put(separator, "middle", "3")).isNull();

StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString()).isEqualTo("alpha=1\nmiddle=3\nbeta=2\n");
assertThat(p).containsEntry("middle", "3");
}

@Test
void testPutNewTest2() throws IOException, URISyntaxException {
Path f = getResource("/test2.properties");
Expand Down
Loading