diff --git a/src/main/java/de/licenseapi/entities/License.java b/src/main/java/de/licenseapi/entities/License.java new file mode 100644 index 0000000..9ceb115 --- /dev/null +++ b/src/main/java/de/licenseapi/entities/License.java @@ -0,0 +1,180 @@ +package de.licenseapi.entities; + +import java.util.ArrayList; +import java.util.Date; + +public class License { + + private final LicenseStatus status; + private final String licenseKey; + private final ArrayList groups; + private final ArrayList permissions; + private final ArrayList meta; + private final int maxUses; + private final int currentUses; + private final Date expirationDate; + + /** + * Creates a new license object with the given parameters + * @param status The status of the license + * @param licenseKey The license key + * @param groups The groups of the license + * @param permissions The permissions of the license + * @param meta The meta information of the license + * @param maxUses The maximum amount of uses of the license + * @param currentUses The current amount of uses of the license + * @param expirationDate The expiration date of the license + */ + public License(LicenseStatus status, String licenseKey, ArrayList groups, ArrayList permissions, + ArrayList meta, int maxUses, int currentUses, Date expirationDate) { + this.status = status; + this.licenseKey = licenseKey; + this.groups = groups; + this.permissions = permissions; + this.meta = meta; + this.maxUses = maxUses; + this.currentUses = currentUses; + this.expirationDate = expirationDate; + } + + /** + * Gets the status of the license + *

+ * The status can be one of the following: + *

    + *
  • {@link LicenseStatus#VALID}
  • + *
  • {@link LicenseStatus#EXPIRED}
  • + *
  • {@link LicenseStatus#INVALID}
  • + *
  • {@link LicenseStatus#MAX_USES_REACHED}
  • + *
+ *

+ * + * @return the status of the license + */ + public LicenseStatus getStatus() { + return status; + } + + /** + * Checks if the license is valid + * + * @return {@code true} if the license is valid and {@code false} if not + */ + public boolean isValid() { + return status == LicenseStatus.VALID; + } + + /** + * Checks if the license has expired + * + * @return {@code true} if the license has expired and {@code false} if not + */ + public boolean isExpired() { + return status == LicenseStatus.EXPIRED; + } + + /** + * Checks if the license has reached the maximum amount of uses (if the maximum amount of uses is set) + * + * @return {@code true} if the license has reached the maximum amount of uses and {@code false} if not + */ + public boolean isMaxUsesReached() { + return status == LicenseStatus.MAX_USES_REACHED; + } + + /** + * Gets the current license key. If the license key is invalid, this method will return {@code null} + * + * @return the current license key + */ + public String getLicenseKey() { + return licenseKey; + } + + /** + * Gets all groups of the license + * + * @return all groups of the license + */ + public ArrayList getGroups() { + return groups; + } + + /** + * Checks if the license has a specific group + * + * @param group The group you want to check + * @return {@code true} if the license has the group and {@code false} if not + */ + public boolean hasGroup(String group) { + return groups.contains(group); + } + + /** + * Gets all permissions of the license + * + * @return all permissions of the license + */ + public ArrayList getPermissions() { + return permissions; + } + + /** + * Checks if the license has a specific permission + * + * @param permission The permission you want to check + * @return {@code true} if the license has the permission and {@code false} if not + */ + public boolean hasPermission(String permission) { + return permissions.contains(permission); + } + + /** + * Gets all meta information of the license + * + * @return all meta information of the license + */ + public ArrayList getMeta() { + return meta; + } + + /** + * Gets a specific meta information of the license. If the meta information does not exist, this method will return {@code null} + * + * @param key The key of the meta information you want to get + * @return the value of the meta information + */ + public String getMeta(String key) { + for (LicenseMeta meta : getMeta()) { + if (meta.getKey().equals(key)) return meta.getValue(); + } + return null; + } + + /** + * Gets the maximum amount of uses of the license. If the maximum amount of uses is not set, this method will return {@code -1} + * + * @return the maximum amount of uses of the license + */ + public int getMaxUses() { + return maxUses; + } + + /** + * Gets the current amount of uses of the license + * + * @return the current amount of uses of the license + */ + public int getCurrentUses() { + return currentUses; + } + + /** + * Gets the expiration date of the license. If the license has no expiration date, this method will return {@code null} + * + * @return the expiration date of the license + */ + public Date getExpirationDate() { + return expirationDate; + } +}