Skip to content

Commit

Permalink
Merge pull request #8 from thegatesdev/dev/dev
Browse files Browse the repository at this point in the history
Version 4.1.0
  • Loading branch information
thegatesdev authored Dec 6, 2023
2 parents fbb5f3c + 29bf839 commit fd53a5e
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![maple-banner-plain](https://github.com/thegatesdev/maple/assets/69715898/16368197-a6e0-4edf-9df4-2576db370412)

A simple, type safe configuration structure
A clean, type safe intermediary structure

*Updated for version 4.0.0*
*Updated for version 4.1.0*

## Contents

Expand Down Expand Up @@ -63,7 +63,7 @@ DataElement myElement = DataMap.EMPTY;
myElement.isList(); // False
myElement.isMap(); // True

myElement.type(); // ElementType.MAP
ElementType type = myElement.type(); // ElementType.MAP

myElement.ifMap(map -> print("myElement is a map!"), () -> print("myElement is not a map!"));
```
Expand All @@ -79,7 +79,7 @@ DataMap myMap;
DataElement otherElement = myMap.get("somekey");

DataMap otherMap = myMap.getMap("mapkey");
DataList otherList = myMap.getList("listkey");
DataList otherList = myMap.getList("listkey", DataList.EMTPY); // With default

DataElement optionalElement = myMap.find("invalid_key"); // Returns 'null' if not found

Expand Down
20 changes: 18 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ plugins {
}

group = "io.github.thegatesdev"
version = "4.0.0"
description = "A clean, type safe configuration structure."
version = "4.1.0"
description = "A clean, type safe intermediary structure"

repositories {
mavenCentral()
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
}


tasks {
test {
useJUnitPlatform()
}
}


java {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/thegatesdev/maple/ElementType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public enum ElementType {
/**
* The map element type, corresponding to the {@link io.github.thegatesdev.maple.element.DataMap} class.
*/
MAP("dataMap", "a map element"),
MAP("dataMap", "map"),
/**
* The list element type, corresponding to the {@link io.github.thegatesdev.maple.element.DataList} class.
*/
LIST("dataList", "a list element"),
LIST("dataList", "list"),
/**
* The value element type, corresponding to the {@link io.github.thegatesdev.maple.element.DataValue} interface.
*/
VALUE("dataValue", "a value element");
VALUE("dataValue", "value");


private final String elementName, inlineName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* This conversion is able to convert most existing Java types to their element equivalent.
* Arrays, Lists, and optionally Iterables will be converted to {@link DataList}.
* Maps will be converted to {@link DataMap}.
* The remaining types will be wrapped in a {@link DataValue}.
*/
public final class DefaultConversion implements Conversion {
public class DefaultConversion implements Conversion {

private boolean useToStringKeys = false, convertIterable = true;

Expand All @@ -54,19 +55,26 @@ public DefaultConversion convertIterable(boolean convertIterable) {
}


private DataElement apply(Object object) {
Objects.requireNonNull(object, "Cannot convert 'null'");
if (object instanceof DataElement) throw new IllegalArgumentException("Cannot convert 'DataElement'");
protected Optional<DataElement> tryApply(Object object){
if (object instanceof Object[] someArray)
return Optional.of(convertList(List.of(someArray)));
else if (object instanceof Map<?, ?> someMap)
return Optional.of(convertMap(someMap));
else if (object instanceof List<?> someList)
return Optional.of(convertList(someList));
else if (convertIterable && object instanceof Iterable<?> someIterable)
return Optional.of(convertList(List.of(someIterable)));
else return Optional.empty();
}

if (object instanceof Object[] someArray) return convertList(List.of(someArray));
if (object instanceof Map<?, ?> someMap) return convertMap(someMap);
if (object instanceof List<?> someList) return convertList(someList);
if (convertIterable && object instanceof Iterable<?> someIterable) return convertList(List.of(someIterable));

return DataValue.of(object);
protected final DataElement apply(Object object) {
Objects.requireNonNull(object, "Cannot convert 'null'");
if (object instanceof DataElement) throw new IllegalArgumentException("Cannot convert 'DataElement'");
return tryApply(object).orElse(DataValue.of(object));
}

public DataMap convertMap(Map<?, ?> someMap) {
public final DataMap convertMap(Map<?, ?> someMap) {
var output = DataMap.builder(someMap.size());
someMap.forEach((key, value) -> {
var result = apply(value);
Expand All @@ -76,7 +84,7 @@ public DataMap convertMap(Map<?, ?> someMap) {
return output.build();
}

public DataList convertList(List<?> someList) {
public final DataList convertList(List<?> someList) {
var output = DataList.builder(someList.size());
for (Object value : someList) output.add(apply(value));
return output.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

import io.github.thegatesdev.maple.exception.KeyNotPresentException;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public sealed interface DataDictionary<Key> permits DataMap, DataList {

Expand Down Expand Up @@ -202,6 +207,20 @@ default DataMap getMap(Key key) {
return get(key).asMap();
}


/**
* Get the map element at the given key. Returns the given default value when it is not present.
*
* @param key the key for the element
* @param def the default value
* @return the map element
*/
default DataMap getMap(Key key, DataMap def){
DataElement el = find(key);
if (el == null || !el.isMap()) return def;
return el.asMap();
}

/**
* Get the list element at the given key.
*
Expand All @@ -212,6 +231,19 @@ default DataList getList(Key key) {
return get(key).asList();
}

/**
* Get the list element at the given key. Returns the given default value when it is not present.
*
* @param key the key for the element
* @param def the default value
* @return the list element
*/
default DataList getList(Key key, DataList def){
DataElement el = find(key);
if (el == null || !el.isList()) return def;
return el.asList();
}

/**
* Get the value element at the given key.
*
Expand All @@ -222,6 +254,19 @@ default DataValue<?> getValue(Key key) {
return get(key).asValue();
}

/**
* Get the value element at the given key. Returns the given default value when it is not present.
*
* @param key the key for the element
* @param def the default value
* @return the value element
*/
default DataValue<?> getValue(Key key, DataValue<?> def){
DataElement el = find(key);
if (el == null || !el.isValue()) return def;
return el.asValue();
}

// Iteration


Expand Down Expand Up @@ -259,6 +304,40 @@ default void eachValue(Consumer<DataValue<?>> valueConsumer) {
each(element -> element.ifValue(valueConsumer));
}


/**
* Create a stream from the elements in this dictionary.
*
* @return the new stream
*/
Stream<DataElement> stream();

/**
* Collect the elements in this dictionary to a new, modifiable list.
*
* @return the new list
*/
List<DataElement> collect();

/**
* Obtain a list element holding the values of this dictionary.
* May return the same object.
*
* @return the list with values
*/
DataList valueList();

/**
* Collect the elements in this dictionary to a new, modifiable list after applying the given function.
*
* @param mapper the function to apply to each element
* @return the new list
*/
default <T> List<T> collect(Function<DataElement, T> mapper){
// Highly naive implementation... should be overridden.
return stream().map(mapper).collect(Collectors.toCollection(ArrayList::new));
}

// Information

/**
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/github/thegatesdev/maple/element/DataList.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* An element holding a list of elements.
Expand Down Expand Up @@ -95,6 +96,29 @@ public DataList transform(Function<DataElement, DataElement> transformFunction)
return new DataList(output);
}

@Override
public Stream<DataElement> stream() {
return Arrays.stream(elements);
}

@Override
public List<DataElement> collect() {
return new ArrayList<>(Arrays.asList(elements));
}

@Override
public <T> List<T> collect(Function<DataElement, T> mapper) {
var result = new ArrayList<T>(elements.length);
for (final DataElement element : elements)
result.add(mapper.apply(element));
return result;
}

@Override
public DataList valueList() {
return this;
}

// Information

@Override
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/io/github/thegatesdev/maple/element/DataMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import io.github.thegatesdev.maple.ElementType;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* An element mapping {@code String} keys to {@code DataValue} values.
Expand Down Expand Up @@ -86,6 +84,41 @@ public DataMap transform(Function<DataElement, DataElement> transformFunction) {
return builder.build();
}

@Override
public Stream<DataElement> stream() {
return elements.values().stream();
}

@Override
public List<DataElement> collect() {
return new ArrayList<>(elements.values());
}

@Override
public <T> List<T> collect(Function<DataElement, T> mapper) {
var result = new ArrayList<T>(elements.size());
elements.forEach((s, element) -> result.add(mapper.apply(element)));
return result;
}

/**
* Obtain a list element holding the keys of this map element.
*
* @return the list with keys
*/
public DataList keyList(){
var builder = DataList.builder(size());
elements.keySet().forEach(key -> builder.add(DataValue.of(key)));
return builder.build();
}

@Override
public DataList valueList(){
var builder = DataList.builder(size());
builder.addFrom(elements.values());
return builder.build();
}

// Information

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class ElementTypeException extends IllegalArgumentException {

private static final String MESSAGE = "Invalid element type, expected %s, got %s";
private static final String MESSAGE = "Invalid element type, expected a %s, got a %s";
private final ElementType expectedType, actualType;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;

/**
* DataType representing an enum.
* DataType representing a string as an enum value.
* Caches all types.
*/
public class EnumDataType<E extends Enum<E>> implements DataType<DataValue<E>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;

/**
* DataType representing a value type.
* DataType representing a simple value of the given type.
* Caches all types.
*/
public class ValueDataType<Val> implements DataType<DataValue<Val>> {
Expand Down
Loading

0 comments on commit fd53a5e

Please sign in to comment.