Skip to content
51 changes: 0 additions & 51 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/main/java/pro/cloudnode/smp/bankaccounts/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.jetbrains.annotations.NotNull;
import pro.cloudnode.smp.bankaccounts.api.account.AccountsService;
import pro.cloudnode.smp.bankaccounts.api.acl.AclService;
import pro.cloudnode.smp.bankaccounts.api.ledger.LedgerService;

import javax.sql.DataSource;
Expand All @@ -36,6 +37,11 @@ public final class API {
*/
public final @NotNull LedgerService ledger;

/**
* Service for managing access control lists (ACLs) and relations.
*/
public final @NotNull AclService acl;

API(
final @NotNull Logger parentLogger,
final @NotNull DataSource dataSource,
Expand All @@ -44,5 +50,6 @@ public final class API {
) {
this.accounts = new AccountsService(parentLogger, dataSource, idLengthAccount);
this.ledger = new LedgerService(parentLogger, dataSource, accounts, idLengthTransaction);
this.acl = new AclService(parentLogger, dataSource);
}
}
30 changes: 30 additions & 0 deletions src/main/java/pro/cloudnode/smp/bankaccounts/Serializable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* BankAccounts is a Minecraft economy plugin that enables players to hold multiple bank accounts.
* Copyright © 2023–2026 Cloudnode OÜ.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/

package pro.cloudnode.smp.bankaccounts;

import org.jetbrains.annotations.NotNull;

/**
* Represents an object that can be serialised to a string.
*/
public interface Serializable {
/**
* Serialises this object to a string.
*
* @return the serialised string representation
*/
@NotNull String serialize();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* BankAccounts is a Minecraft economy plugin that enables players to hold multiple bank accounts.
* Copyright © 2023–2026 Cloudnode OÜ.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/

package pro.cloudnode.smp.bankaccounts.api;

import org.jetbrains.annotations.NotNull;
import pro.cloudnode.smp.bankaccounts.Serializable;

import java.util.Objects;

/**
* Represents a typed identifier.
*/
public class TypedIdentifier implements Serializable {
private final @NotNull Type type;
private final @NotNull String id;

/**
* Constructs a typed identifier.
*
* @param type the type
* @param id the identifier
*/
public TypedIdentifier(final @NotNull Type type, final @NotNull String id) {
this.type = type;
this.id = id;
}

/**
* Parses a typed identifier from a string in the format {@code <type>:<id>}.
*
* @param identifier the identifier string to parse
* @return the corresponding typed identifier
* @throws IllegalArgumentException if the identifier format is invalid or the type is unknown
*/
@NotNull
public static TypedIdentifier deserialize(final @NotNull String identifier) {
final int colonIndex = identifier.indexOf(':');
if (colonIndex == -1) {
throw new IllegalArgumentException(String.format("Invalid identifier: %s", identifier));
}
return new TypedIdentifier(
Type.deserialize(identifier.substring(0, colonIndex)),
identifier.substring(colonIndex + 1)
);
}

/**
* Returns a string representation of this typed identifier in the format {@code <type>:<id>}.
*
* @return the identifier string representation
*/
@Override
@NotNull
public String serialize() {
return type.toString() + ':' + id;
}

/**
* Returns the identifier type.
*
* @return the type
*/
public @NotNull Type type() {
return type;
}

/**
* Returns the identifier value.
*
* @return the identifier
*/
public @NotNull String id() {
return id;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof final TypedIdentifier ti)) {
return false;
}
return this.type == ti.type && this.id.equals(ti.id);
}

@Override
public int hashCode() {
return Objects.hash(type.serialize(), id);
}

/**
* Represents an identifier type.
*/
public enum Type implements Serializable {
/**
* Represents an account holder.
*/
HOLDER,

/**
* Represents an account.
*/
ACCOUNT;

/**
* Returns a type from a string.
*
* @param value the string representation of the type
* @return the type
*/
@NotNull
public static Type deserialize(final @NotNull String value) {
return valueOf(value.toUpperCase());
}

@Override
public @NotNull String serialize() {
return name().toLowerCase();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
* Represents a bank account.
*/
public final class Account {
private final @NotNull String id;
private final @NotNull AccountId id;
private final @NotNull Type type;
private final @NotNull Instant created;
private boolean allowNegative;
private @NotNull Status status;
private @Nullable String name;

Account(
final @NotNull String id,
final @NotNull AccountId id,
final @NotNull Type type,
final boolean allowNegative,
final @NotNull Status status,
Expand All @@ -54,7 +54,7 @@ public final class Account {
* @return the account ID
*/
@NotNull
public String id() {
public AccountId id() {
return id;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* BankAccounts is a Minecraft economy plugin that enables players to hold multiple bank accounts.
* Copyright © 2023–2026 Cloudnode OÜ.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/

package pro.cloudnode.smp.bankaccounts.api.account;

import org.jetbrains.annotations.NotNull;
import pro.cloudnode.smp.bankaccounts.api.TypedIdentifier;

/**
* Represents an account identifier.
*/
public final class AccountId extends TypedIdentifier {
/**
* Constructs an account identifier.
*
* @param id the account ID
*/
public AccountId(@NotNull String id) {
super(Type.ACCOUNT, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ INSERT INTO bank_accounts (
created
) VALUES (?, ?, ?, ?, ?, ?)
""", stmt -> {
stmt.setString(1, account.id());
stmt.setString(1, account.id().id());
stmt.setInt(2, account.type().ordinal());
stmt.setBoolean(3, account.allowNegative());
stmt.setInt(4, account.status().ordinal());
Expand All @@ -109,7 +109,7 @@ public void update(final @NotNull Account account) throws RepositoryException {
stmt.setBoolean(1, account.allowNegative());
stmt.setInt(2, account.status().ordinal());
stmt.setString(3, account.name().orElse(null));
stmt.setString(4, account.id());
stmt.setString(4, account.id().id());
}
) > 0;

Expand All @@ -123,7 +123,7 @@ public void update(final @NotNull Account account) throws RepositoryException {
@NotNull
protected Account map(final @NotNull ResultSet resultSet) throws SQLException {
return new Account(
resultSet.getString("id"),
new AccountId(resultSet.getString("id")),
Account.Type.values()[resultSet.getInt("type")],
resultSet.getBoolean("allow_negative"),
Account.Status.values()[resultSet.getInt("status")],
Expand Down
Loading
Loading