-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from connorhartley/master
Parse and write configs inside collections correctly for yaml
- Loading branch information
Showing
9 changed files
with
204 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/main/java/com/electronwill/nightconfig/core/utils/TransformingList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.electronwill.nightconfig.core.utils; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
import java.util.function.Function; | ||
|
||
public final class TransformingList<InternalV, ExternalV> | ||
extends TransformingCollection<InternalV, ExternalV> implements List<ExternalV> { | ||
public TransformingList(List<InternalV> internalList, | ||
Function<? super InternalV, ? extends ExternalV> readTransformation, | ||
Function<? super ExternalV, ? extends InternalV> writeTransformation, | ||
Function<Object, Object> searchTransformation) { | ||
super(internalList, readTransformation, writeTransformation, searchTransformation); | ||
} | ||
|
||
@Override | ||
public boolean addAll(int index, Collection<? extends ExternalV> c) { | ||
return ((List<InternalV>) internalCollection).addAll(index, | ||
new TransformingCollection(c, writeTransformation, readTransformation, searchTransformation)); | ||
} | ||
|
||
@Override | ||
public ExternalV get(int index) { | ||
return readTransformation.apply(((List<InternalV>) internalCollection).get(index)); | ||
} | ||
|
||
@Override | ||
public ExternalV set(int index, ExternalV element) { | ||
return readTransformation.apply(((List<InternalV>) internalCollection).set(index, writeTransformation.apply(element))); | ||
} | ||
|
||
@Override | ||
public void add(int index, ExternalV element) { | ||
((List<InternalV>) internalCollection).add(index, writeTransformation.apply(element)); | ||
} | ||
|
||
@Override | ||
public ExternalV remove(int index) { | ||
return readTransformation.apply(((List<InternalV>) internalCollection).remove(index)); | ||
} | ||
|
||
@Override | ||
public int indexOf(Object o) { | ||
return ((List<InternalV>) internalCollection).indexOf(searchTransformation.apply(o)); | ||
} | ||
|
||
@Override | ||
public int lastIndexOf(Object o) { | ||
return ((List<InternalV>) internalCollection).lastIndexOf(searchTransformation.apply(o)); | ||
} | ||
|
||
@Override | ||
public ListIterator<ExternalV> listIterator() { | ||
return new TransformingListIterator<>(((List<InternalV>) internalCollection).listIterator(), readTransformation, writeTransformation); | ||
} | ||
|
||
@Override | ||
public ListIterator<ExternalV> listIterator(int index) { | ||
return new TransformingListIterator<>(((List<InternalV>) internalCollection).listIterator(index), readTransformation, writeTransformation); | ||
} | ||
|
||
@Override | ||
public List<ExternalV> subList(int fromIndex, int toIndex) { | ||
return new TransformingList<>(((List<InternalV>) internalCollection).subList(fromIndex, toIndex), | ||
readTransformation, writeTransformation, searchTransformation); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
core/src/main/java/com/electronwill/nightconfig/core/utils/TransformingListIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.electronwill.nightconfig.core.utils; | ||
|
||
import java.util.ListIterator; | ||
import java.util.function.Function; | ||
|
||
public final class TransformingListIterator<InternalV, ExternalV> | ||
extends TransformingIterator<InternalV, ExternalV> implements ListIterator<ExternalV> { | ||
private final Function<? super ExternalV, ? extends InternalV> writeTransformation; | ||
|
||
public TransformingListIterator(ListIterator<InternalV> internalIterator, | ||
Function<? super InternalV, ? extends ExternalV> readTransformation, | ||
Function<? super ExternalV, ? extends InternalV> writeTransformation) { | ||
super(internalIterator, readTransformation); | ||
this.writeTransformation = writeTransformation; | ||
} | ||
|
||
@Override | ||
public boolean hasPrevious() { | ||
return ((ListIterator<InternalV>) internalIterator).hasPrevious(); | ||
} | ||
|
||
@Override | ||
public ExternalV previous() { | ||
return readTransformation.apply(((ListIterator<InternalV>) internalIterator).previous()); | ||
} | ||
|
||
@Override | ||
public int nextIndex() { | ||
return ((ListIterator<InternalV>) internalIterator).nextIndex(); | ||
} | ||
|
||
@Override | ||
public int previousIndex() { | ||
return ((ListIterator<InternalV>) internalIterator).previousIndex(); | ||
} | ||
|
||
@Override | ||
public void set(ExternalV externalV) { | ||
((ListIterator<InternalV>) internalIterator).set(writeTransformation.apply(externalV)); | ||
} | ||
|
||
@Override | ||
public void add(ExternalV externalV) { | ||
((ListIterator<InternalV>) internalIterator).add(writeTransformation.apply(externalV)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters