Skip to content

Instantly share code, notes, and snippets.

@SettingDust
Last active August 23, 2024 01:02
Show Gist options
  • Save SettingDust/9bd8720adbc03a5ba099b1d86e02b2a6 to your computer and use it in GitHub Desktop.
Save SettingDust/9bd8720adbc03a5ba099b1d86e02b2a6 to your computer and use it in GitHub Desktop.
Manifold StackOverflow
package com.*.api.key;
import com.google.common.base.Preconditions;
import lombok.val;
import manifold.ext.rt.api.auto;
import *.extensions.com.*.api.key.Key.KeyExtensions;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.OptionalInt;
/**
* @see <a href="https://github.com/KyoriPowered/adventure/blob/main/4/key/src/main/java/net/kyori/adventure/key/Key.java">https://github.com/KyoriPowered/adventure/blob/main/4/key/src/main/java/net/kyori/adventure/key/Key.java</a>
*/
@Data
public final class Key implements Comparable<Key> {
public static final String DEFAULT_NAMESPACE = "mud";
public static final char DEFAULT_SEPARATOR = ':';
@KeyPattern.Namespace private final String namespace;
@KeyPattern.Value private final String value;
/**
* Checks if {@code string} can be parsed into a {@link Key}.
*
* @param string the input string
*
* @return {@code true} if {@code string} can be parsed into a {@link Key}, {@code false} otherwise
*
* @since 4.12.0
*/
public static boolean parseable(final @Nullable String string) {
if (string == null) {
return false;
}
final int index = string.indexOf(DEFAULT_SEPARATOR);
final String namespace = index >= 1 ? string.substring(0, index) : DEFAULT_NAMESPACE;
final String value = index >= 0 ? string.substring(index + 1) : string;
return parseableNamespace(namespace) && parseableValue(value);
}
/**
* Checks if {@code value} is a valid namespace.
*
* @param namespace the string to check
*
* @return {@code true} if {@code value} is a valid namespace, {@code false} otherwise
*/
public static boolean parseableNamespace(final @NotNull String namespace) {
return checkNamespace(namespace).isEmpty();
}
public static boolean allowedInNamespace(final char character) {
return character == '_' || character == '-' || (character >= 'a' && character <= 'z') ||
(character >= '0' && character <= '9') || character == '.';
}
/**
* Checks if {@code value} is a valid namespace.
*
* @param namespace the string to check
*
* @return {@link OptionalInt#empty()} if {@code value} is a valid namespace, otherwise an {@code OptionalInt} containing the index of an invalid character
*/
public static @NotNull OptionalInt checkNamespace(final @NotNull String namespace) {
for (int i = 0, length = namespace.length(); i < length; i++) {
if (!allowedInNamespace(namespace.charAt(i))) {
return OptionalInt.of(i);
}
}
return OptionalInt.empty();
}
public static boolean allowedInValue(final char character) {
return character == '_' || character == '-' || (character >= 'a' && character <= 'z') ||
(character >= '0' && character <= '9') || character == '.' || character == '/';
}
/**
* Checks if {@code value} is a valid value.
*
* @param value the string to check
*
* @return {@link OptionalInt#empty()} if {@code value} is a valid value, otherwise an {@code OptionalInt} containing the index of an invalid character
*/
public static @NotNull OptionalInt checkValue(final @NotNull String value) {
for (int i = 0, length = value.length(); i < length; i++) {
if (!allowedInValue(value.charAt(i))) {
return OptionalInt.of(i);
}
}
return OptionalInt.empty();
}
/**
* Checks if {@code value} is a valid value.
*
* @param value the string to check
*
* @return {@code true} if {@code value} is a valid value, {@code false} otherwise
*/
public static boolean parseableValue(final @NotNull String value) {
return checkValue(value).isEmpty();
}
public Key(@KeyPattern.Namespace final String namespace, @KeyPattern.Value final String value) {
Preconditions.checkArgument(parseableNamespace(namespace));
Preconditions.checkArgument(parseableValue(value));
this.namespace = namespace;
this.value = value;
}
public Key(@Subst("mud:test") @KeyPattern String value) {
this(DEFAULT_NAMESPACE, value);
auto index = value.indexOf(DEFAULT_SEPARATOR);
}
@Override
public int compareTo(@NotNull final Key o) {
return KeyExtensions.COMPARATOR.compare(this, o);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.*</groupId>
<artifactId>*</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>*</name>
<description>*</description>
<properties>
<manifold-version>2024.1.30</manifold-version>
<lombok-version>1.18.34</lombok-version>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${lombok-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
<dependency>
<groupId>org.teasoft</groupId>
<artifactId>bee-all</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.1.0</version>
<scope>provided</scope>
</dependency>
<!-- DFU -->
<dependency>
<groupId>com.mojang</groupId>
<artifactId>datafixerupper</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.github.Builderb0y</groupId>
<artifactId>AutoCodec</artifactId>
<version>4.12.1</version>
</dependency>
<!-- manifold -->
<dependency>
<groupId>systems.manifold</groupId>
<artifactId>manifold-ext-rt</artifactId>
<version>${manifold-version}</version>
</dependency>
<dependency>
<groupId>systems.manifold</groupId>
<artifactId>manifold-delegation-rt</artifactId>
<version>${manifold-version}</version>
</dependency>
<dependency>
<groupId>systems.manifold</groupId>
<artifactId>manifold-tuple-rt</artifactId>
<version>${manifold-version}</version>
</dependency>
<dependency>
<groupId>systems.manifold</groupId>
<artifactId>manifold-props-rt</artifactId>
<version>${manifold-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<!--class files as source must be available for extension method classes-->
<Contains-Sources>java,class</Contains-Sources>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
<arg>-Xplugin:Manifold</arg>
<arg>--enable-preview</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-ext</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-io</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-text</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-strings</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-exceptions</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-delegation</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-tuple</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-collections</artifactId>
<version>${manifold-version}</version>
</path>
<path>
<groupId>systems.manifold</groupId>
<artifactId>manifold-props</artifactId>
<version>${manifold-version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven-central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>minecraft-libraries</id>
<name>Minecraft Libraries</name>
<url>https://libraries.minecraft.net</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment