-
Notifications
You must be signed in to change notification settings - Fork 4
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 #9 from rec-jvm/kotlin-remove-stage2
Kotlin remove stage2
- Loading branch information
Showing
28 changed files
with
478 additions
and
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.kimleo.rec; | ||
|
||
public class Pair<K, V> { | ||
public final K first; | ||
public final V second; | ||
|
||
|
||
public Pair(K first, V second) { | ||
this.first = first; | ||
this.second = second; | ||
} | ||
|
||
public K getFirst() { | ||
return first; | ||
} | ||
|
||
public V getSecond() { | ||
return second; | ||
} | ||
|
||
public K component1() { | ||
return first; | ||
} | ||
|
||
public V component2() { | ||
return second; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
common/src/main/java/net/kimleo/rec/util/CollectUtils.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,30 @@ | ||
package net.kimleo.rec.util; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
import java.util.stream.Stream; | ||
|
||
public class CollectUtils { | ||
public static <T> Iterable<T> reversed(List<T> list) { | ||
|
||
|
||
return () -> { | ||
ListIterator<T> li = list.listIterator(list.size()); | ||
|
||
return new Iterator<T>() { | ||
@Override | ||
public boolean hasNext() { | ||
return li.hasPrevious(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
return li.previous(); | ||
} | ||
}; | ||
}; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
rec-accessor/src/main/java/net/kimleo/rec/accessor/Accessor.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,37 @@ | ||
package net.kimleo.rec.accessor; | ||
|
||
import net.kimleo.rec.Pair; | ||
import net.kimleo.rec.accessor.lexer.Lexer; | ||
import net.kimleo.rec.concept.Indexible; | ||
|
||
import java.util.Map; | ||
|
||
public class Accessor<T> { | ||
|
||
private final Map<String, Integer> fieldMap; | ||
private final Integer leastCapacity; | ||
|
||
public Accessor(String[] fields) { | ||
Pair<Map<String, Integer>, Integer> fieldMapPair = Lexer.buildFieldMapPair(fields); | ||
fieldMap = fieldMapPair.getFirst(); | ||
leastCapacity = fieldMapPair.getSecond(); | ||
} | ||
|
||
public Map<String, Integer> getFieldMap() { | ||
return fieldMap; | ||
} | ||
|
||
public Integer getLeastCapacity() { | ||
return leastCapacity; | ||
} | ||
|
||
public RecordWrapper<T> create(Indexible<T> record) { | ||
assert (record.getSize() >= leastCapacity); | ||
|
||
return new RecordWrapper<>(fieldMap, record); | ||
} | ||
|
||
public RecordWrapper<T> of(Indexible<T> record) { | ||
return create(record); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
rec-accessor/src/main/java/net/kimleo/rec/accessor/RecordWrapper.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,51 @@ | ||
package net.kimleo.rec.accessor; | ||
|
||
import net.kimleo.rec.concept.Indexible; | ||
import net.kimleo.rec.concept.Mapped; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class RecordWrapper<T> implements Indexible<T>, Mapped<T> { | ||
|
||
private final Map<String, Integer> fieldNames; | ||
|
||
private final Indexible<T> record; | ||
|
||
public RecordWrapper(Map<String, Integer> fieldNames, Indexible<T> record) { | ||
this.fieldNames = fieldNames; | ||
this.record = record; | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return getByIndex(index, record); | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return record.getSize(); | ||
} | ||
|
||
@Override | ||
public T get(String field) { | ||
if (fieldNames.containsKey(field)) { | ||
return getByIndex(fieldNames.get(field), record); | ||
} | ||
return null; | ||
} | ||
|
||
private T getByIndex(Integer index, Indexible<T> record) { | ||
if (index >= 0) { | ||
return record.get(index); | ||
} else { | ||
return record.get(record.getSize() + index); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> keys() { | ||
return fieldNames.keySet().stream().collect(Collectors.toList()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rec-accessor/src/main/java/net/kimleo/rec/accessor/lexer/FieldName.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,33 @@ | ||
package net.kimleo.rec.accessor.lexer; | ||
|
||
public class FieldName implements FieldType { | ||
private final String name; | ||
|
||
FieldName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
FieldName fieldName = (FieldName) o; | ||
|
||
return name != null ? name.equals(fieldName.name) : fieldName.name == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return name != null ? name.hashCode() : 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
rec-accessor/src/main/java/net/kimleo/rec/accessor/lexer/FieldType.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,4 @@ | ||
package net.kimleo.rec.accessor.lexer; | ||
|
||
public interface FieldType { | ||
} |
83 changes: 83 additions & 0 deletions
83
rec-accessor/src/main/java/net/kimleo/rec/accessor/lexer/Lexer.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,83 @@ | ||
package net.kimleo.rec.accessor.lexer; | ||
|
||
import net.kimleo.rec.Pair; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static net.kimleo.rec.util.CollectUtils.reversed; | ||
|
||
public class Lexer { | ||
public static final Pattern PADDING_PATTERN = Pattern.compile("\\{(\\d+)}"); | ||
|
||
|
||
public static Pair<Map<String, Integer>, Integer> buildFieldMapPair(String... fields) { | ||
final HashMap<String, Integer> accessorMap = new HashMap<>(); | ||
final Pair<List<FieldType>, Integer> lex = lex(fields); | ||
final List<FieldType> accessors = lex.getFirst(); | ||
final Integer leastCapacity = lex.getSecond(); | ||
final Iterable<FieldType> reversedAccessors = reversed(accessors); | ||
boolean reversed = false; | ||
int index = 0; | ||
for (FieldType accessor : accessors) { | ||
if (accessor instanceof FieldName) { | ||
accessorMap.put(((FieldName) accessor).getName(), index); | ||
index++; | ||
} else if (accessor instanceof Padding) { | ||
index += ((Padding) accessor).getSize(); | ||
} else if (accessor instanceof Placeholder) { | ||
reversed = true; | ||
break; | ||
} | ||
} | ||
|
||
index = -1; | ||
|
||
if (reversed) { | ||
for (FieldType accessor : reversedAccessors) { | ||
if (accessor instanceof FieldName) { | ||
accessorMap.put(((FieldName) accessor).getName(), index); | ||
index--; | ||
} else if (accessor instanceof Padding) { | ||
index -= ((Padding) accessor).getSize(); | ||
} else if (accessor instanceof Placeholder) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return new Pair<>(accessorMap, leastCapacity); | ||
} | ||
|
||
public static Pair<List<FieldType>, Integer> lex(String[] fields) { | ||
|
||
int segmentSize = 1; | ||
int currentSegmentSize = 0; | ||
ArrayList<FieldType> accessors = new ArrayList<>(); | ||
for (String field : fields) { | ||
if (field.trim().startsWith("{")) { | ||
Matcher matcher = PADDING_PATTERN.matcher(field); | ||
if (matcher.matches()) { | ||
int amount = Integer.parseInt(matcher.group(1)); | ||
currentSegmentSize += amount; | ||
accessors.add(new Padding(amount)); | ||
} else { | ||
throw new UnsupportedOperationException("Unknown format for padding: " + field); | ||
} | ||
} else if ("...".equals(field.trim())) { | ||
accessors.add(new Placeholder(field.trim())); | ||
segmentSize = Math.max(currentSegmentSize, segmentSize); | ||
currentSegmentSize = 0; | ||
} else { | ||
accessors.add(new FieldName(field.trim())); | ||
currentSegmentSize++; | ||
} | ||
} | ||
|
||
return new Pair<>(accessors, Math.max(currentSegmentSize, segmentSize)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rec-accessor/src/main/java/net/kimleo/rec/accessor/lexer/Padding.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,33 @@ | ||
package net.kimleo.rec.accessor.lexer; | ||
|
||
public class Padding implements FieldType { | ||
private final int size; | ||
|
||
public Padding(int size) { | ||
this.size = size; | ||
} | ||
|
||
public int getSize() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Padding padding = (Padding) o; | ||
|
||
return size == padding.size; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("{%d}", size); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rec-accessor/src/main/java/net/kimleo/rec/accessor/lexer/Placeholder.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,33 @@ | ||
package net.kimleo.rec.accessor.lexer; | ||
|
||
public class Placeholder implements FieldType { | ||
private final String name; | ||
|
||
public Placeholder(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Placeholder that = (Placeholder) o; | ||
|
||
return name != null ? name.equals(that.name) : that.name == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return name != null ? name.hashCode() : 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
rec-accessor/src/main/kotlin/net/kimleo/rec/accessor/Accessor.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.