-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New `com.atatctech.packages.data`.
Showing
3 changed files
with
150 additions
and
2 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
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,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]+$"); | ||
} | ||
} |
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,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 ""; | ||
} | ||
} | ||
} |