Skip to content

Commit

Permalink
Move to Types library
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenicos committed Feb 22, 2024
1 parent 994acbf commit 075308c
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 1,449 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ allprojects {
apply plugin: 'maven-publish'
apply plugin: 'idea'

idea {
module {
downloadJavadoc = true
downloadSources = true
}
}

repositories {
maven { url 'https://jitpack.io' }
mavenCentral()
}

Expand Down Expand Up @@ -50,6 +58,8 @@ subprojects {
}

dependencies {
api 'com.saicone:types:1.0'

compileOnly 'com.ezylang:EvalEx:3.1.1'
compileOnly 'com.github.ben-manes.caffeine:caffeine:2.9.3'
compileOnly 'com.google.guava:guava:33.0.0-jre'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.saicone.settings.node.ListNode;
import com.saicone.settings.node.MapNode;
import com.saicone.settings.node.NodeKey;
import com.saicone.settings.type.ValueType;
import com.saicone.types.ValueType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/saicone/settings/SettingsNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.saicone.settings.node.MapNode;
import com.saicone.settings.node.NodeKey;
import com.saicone.settings.node.ObjectNode;
import com.saicone.settings.type.ValueType;
import com.saicone.types.ValueType;
import com.saicone.settings.util.Strings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/saicone/settings/node/MapNode.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.saicone.settings.node;

import com.saicone.settings.SettingsNode;
import com.saicone.settings.type.IterableType;
import com.saicone.settings.type.ValueType;
import com.saicone.types.IterableType;
import com.saicone.types.ValueType;
import com.saicone.settings.util.Strings;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/com/saicone/settings/node/NodeValue.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.saicone.settings.node;

import com.saicone.settings.SettingsNode;
import com.saicone.settings.type.TypeParser;
import com.saicone.settings.type.Types;
import com.saicone.types.TypeParser;
import com.saicone.types.Types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

/**
* Abstract class that represents a node multi-layer value along with comments.<br>
Expand Down Expand Up @@ -160,9 +157,14 @@ public SettingsNode setSideComment(@Nullable List<String> sideComment) {
return parsedValue;
}

@Override
public @NotNull <E> Optional<E> asOptional(@NotNull TypeParser<E> parser) {
return Optional.ofNullable(as(parser));
}

@Override
@SuppressWarnings("unchecked")
public <E, C extends Collection<E>> @NotNull C asCollection(@NotNull C collection, @NotNull TypeParser<E> parser) {
public <E, C extends Collection<E>> @NotNull C asCollection(@NotNull TypeParser<E> parser, @NotNull C collection) {
if (parser.equals(this.typeParser) && collection.getClass().isInstance(this.parsedValue)) {
return (C) this.parsedValue;
}
Expand All @@ -172,6 +174,28 @@ public SettingsNode setSideComment(@Nullable List<String> sideComment) {
return parsedValue;
}

@Override
@SuppressWarnings("unchecked")
public <E extends Enum<?>> @Nullable E asEnum(@NotNull Class<E> type) {
if (type.isInstance(this.parsedValue)) {
return (E) this.parsedValue;
}
final E parsedValue = SettingsNode.super.asEnum(type);
this.parsedValue = parsedValue;
return parsedValue;
}

@Override
@SuppressWarnings("unchecked")
public <E extends Enum<?>> @Nullable E asEnum(@NotNull Class<E> type, @NotNull E[] values) {
if (type.isInstance(this.parsedValue)) {
return (E) this.parsedValue;
}
final E parsedValue = SettingsNode.super.asEnum(type, values);
this.parsedValue = parsedValue;
return parsedValue;
}

@Override
public String toString() {
return Types.STRING.parse(getValue(), "null");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/saicone/settings/node/ObjectNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.saicone.settings.node;

import com.saicone.settings.SettingsNode;
import com.saicone.settings.type.IterableType;
import com.saicone.types.IterableType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
95 changes: 0 additions & 95 deletions src/main/java/com/saicone/settings/type/IterableType.java

This file was deleted.

171 changes: 171 additions & 0 deletions src/main/java/com/saicone/settings/type/ListValueType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.saicone.settings.type;

import com.saicone.types.TypeParser;
import com.saicone.types.Types;
import com.saicone.types.ValueType;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
* Represents a value that can be converted into different types of objects.
*
* @author Rubenicos
*
* @param <T> the value type itself.
*/
@FunctionalInterface
public interface ListValueType<T> extends ValueType<T> {

/**
* Create a wrapped value type from given object type.
*
* @param value the object type to wrap.
* @return a value type instance.
* @param <T> the value type itself.
*/
@NotNull
static <T> ListValueType<T> of(T value) {
return () -> value;
}

/**
* Convert this object into a list of objects.
*
* @return a list containing objects.
*/
@NotNull
default List<Object> asList() {
return asList(Types.OBJECT);
}

/**
* Convert this object into a list of strings.
*
* @see Types#STRING
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into strings.
*/
@NotNull
default List<String> asStringList() {
return asList(Types.STRING);
}

/**
* Convert this object into a list of characters.
*
* @see Types#CHAR
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into characters.
*/
@NotNull
default List<Character> asCharList() {
return asList(Types.CHAR);
}

/**
* Convert this object into a list of booleans.
*
* @see Types#BOOLEAN
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into booleans.
*/
@NotNull
default List<Boolean> asBooleanList() {
return asList(Types.BOOLEAN);
}

/**
* Convert this object into a list of bytes.
*
* @see Types#BYTE
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into bytes.
*/
@NotNull
default List<Byte> asByteList() {
return asList(Types.BYTE);
}

/**
* Convert this object into a list of shorts.
*
* @see Types#SHORT
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into shorts.
*/
@NotNull
default List<Short> asShortList() {
return asList(Types.SHORT);
}

/**
* Convert this object into a list of integers.
*
* @see Types#INTEGER
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into integers.
*/
@NotNull
default List<Integer> asIntList() {
return asList(Types.INTEGER);
}

/**
* Convert this object into a list of floats.
*
* @see Types#FLOAT
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into floats.
*/
@NotNull
default List<Float> asFloatList() {
return asList(Types.FLOAT);
}

/**
* Convert this object into a list of longs.
*
* @see Types#LONG
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into longs.
*/
@NotNull
default List<Long> asLongList() {
return asList(Types.LONG);
}

/**
* Convert this object into a list of doubles.
*
* @see Types#DOUBLE
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into doubles.
*/
@NotNull
default List<Double> asDoubleList() {
return asList(Types.DOUBLE);
}

/**
* Convert this object into a list of unique IDs.
*
* @see Types#UUID
* @see TypeParser#collection(Collection, Object)
*
* @return a list containing only values that was converted into unique IDs.
*/
@NotNull
default List<UUID> asUniqueIdList() {
return asList(Types.UUID);
}
}
Loading

0 comments on commit 075308c

Please sign in to comment.