Skip to content

Commit

Permalink
Version 1.0.8.
Browse files Browse the repository at this point in the history
New `com.atatctech.packages.data`.
ATATC committed Aug 13, 2023
1 parent 4cb6d7d commit e349f8b
Showing 3 changed files with 150 additions and 2 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -7,17 +7,19 @@ plugins {
apply plugin: 'maven-publish'

group 'com.atatctech'
version '1.0.7'
version '1.0.8'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.jetbrains:annotations:23.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'com.belerweb:pinyin4j:2.5.1'
implementation 'com.fasterxml.jackson.core:jackson-core:2.15.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
implementation 'org.jetbrains:annotations:23.0.0'
}

java {
84 changes: 84 additions & 0 deletions src/main/java/com/atatctech/packages/data/DataCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.atatctech.packages.data;

import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Set;

public final class DataCheck {
public static <E> boolean lengthCheck(@Nullable List<E> list, int minLength, int maxLength) {
return list != null && list.size() >= minLength && list.size() <= maxLength;
}

public static <E> boolean lengthCheck(@Nullable Set<E> set, int minLength, int maxLength) {
return set != null && set.size() >= minLength && set.size() <= maxLength;
}

public static <E> boolean lengthCheck(@Nullable List<E> list, int maxLength) {
return lengthCheck(list, 0, maxLength);
}

public static <E> boolean lengthCheck(@Nullable Set<E> set, int maxLength) {
return lengthCheck(set, 0, maxLength);
}

public static boolean lengthCheck(byte @Nullable [] bytes, int minLength, int maxLength) {
return bytes != null && bytes.length >= minLength && bytes.length <= maxLength;
}

public static boolean lengthCheck(byte[] bytes, int maxLength) {
return lengthCheck(bytes, 0, maxLength);
}

public static boolean lengthCheck(@Nullable String string, int minLength, int maxLength) {
return string != null && string.length() >= minLength && string.length() <= maxLength;
}

public static boolean lengthCheck(@Nullable String string, int maxLength) {
return lengthCheck(string, 0, maxLength);
}

public static boolean emailCheck(@Nullable String email) {
return lengthCheck(email, 5, 320) && email.matches("^[\\w!#$%&'*+\\-/=?^`{|}~]+(\\.[\\w!#$%&'*+\\-/=?^`{|}~]+)*@[a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)*\\.[a-zA-Z]+$");
}

public static boolean displayableCheck(@Nullable String string) {
return string != null && string.matches("^[\\S ]+$");
}

public static boolean displayNameCheck(@Nullable String displayName, int minLength, int maxLength) {
return lengthCheck(displayName, minLength, maxLength) && displayableCheck(displayName);
}

public static boolean displayNameCheck(@Nullable String displayName, int maxLength) {
return displayNameCheck(displayName, 0, maxLength);
}

public static boolean displayNameCheck(@Nullable String displayName) {
return displayNameCheck(displayName, 3, 36);
}

public static boolean nameCheck(@Nullable String name, int minLength, int maxLength) {
return lengthCheck(name, minLength, maxLength) && name.matches("^[A-Za-z]+(-?[A-Za-z0-9]+)*$");
}

public static boolean nameCheck(@Nullable String name, int maxLength) {
return nameCheck(name, 0, maxLength);
}

public static boolean nameCheck(@Nullable String name) {
return nameCheck(name, 3, 36);
}

public static boolean urlCheck(@Nullable String url, @Nullable String prefix) {
return prefix != null && lengthCheck(url, prefix.length() + 1, 8182) && url.matches("^" + prefix + "[\\w!#$%&'*+\\-/=?^`{|}~.]+$");
}

public static boolean urlCheck(@Nullable String url) {
return urlCheck(url, "https://");
}

public static boolean mobileCheck(@Nullable String mobile) {
return lengthCheck(mobile, 7, 16) && mobile.matches("^\\+?[0-9]+$");
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/atatctech/packages/data/JSON.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.atatctech.packages.data;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class JSON implements Cloneable {
public static final ObjectMapper objectMapper = new ObjectMapper();

@Override
public @NotNull JSON clone() {
try {
return (JSON) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}

public static @NotNull String stringify(@Nullable Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException ignored) {
return "";
}
}
public static <T> @Nullable T parseString(@Nullable String json, @NotNull Class<T> classOf) {
if (json == null) return null;
try {
return objectMapper.readValue(json, classOf);
} catch (JsonProcessingException e) {
return null;
}
}
public static <T> @Nullable T parseString(@Nullable String json, @NotNull TypeReference<T> valueTypeRef) {
if (json == null) return null;
try {
return objectMapper.readValue(json, valueTypeRef);
} catch (JsonProcessingException e) {
return null;
}
}

public static <T> @Nullable T parseString(@Nullable String json, @NotNull JavaType valueType) {
if (json == null) return null;
try {
return objectMapper.readValue(json, valueType);
} catch (JsonProcessingException e) {
return null;
}
}

public @NotNull String stringify() {
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException ignored) {
return "";
}
}
}

0 comments on commit e349f8b

Please sign in to comment.