Skip to content

Commit

Permalink
Merge pull request #1 from alexissdev/develop
Browse files Browse the repository at this point in the history
Added all features
  • Loading branch information
alexissdev committed Jul 21, 2022
2 parents 289385d + 30e604d commit b5bb517
Show file tree
Hide file tree
Showing 36 changed files with 1,072 additions and 2 deletions.
Empty file added api/build.gradle.kts
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.alexisdev.balder.api.buildable;

public interface Buildable<T> {

/**
* Builds an instance of T.
*
* @return A new instance of the {@link T}.
*/

T build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.alexisdev.balder.api.checker;

import dev.alexisdev.balder.api.finder.LicenceFinder;
import dev.alexisdev.balder.api.util.Validate;

public class DefaultLicenceChecker
implements LicenceChecker {

protected final LicenceFinder licenceFinder;

protected DefaultLicenceChecker(
LicenceFinder licenceFinder
) {
this.licenceFinder = Validate.notNull(
licenceFinder, "licenceFinder"
);
}

@Override
public boolean isLicenced(
String licence
) {
return licenceFinder.find(licence)
!= null;
}

public static LicenceChecker create(
LicenceFinder licenceFinder
) {
return new DefaultLicenceChecker(licenceFinder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.alexisdev.balder.api.checker;

import dev.alexisdev.balder.api.finder.LicenceFinder;

public interface LicenceChecker {

/**
* This function returns true if the licence is valid, false otherwise.
*
* @param licence The licence key to check.
* @return A boolean value.
*/
boolean isLicenced(
String licence
);

/**
* Return the default checker of licence's
*
* @return A new instance of the DefaultLicenceChecker class.
*/
static LicenceChecker getDefault(
LicenceFinder licenceFinder
) {
return DefaultLicenceChecker.create(
licenceFinder
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.alexisdev.balder.api.exceptions;

public class LicenceNotFoundException
extends RuntimeException {

public LicenceNotFoundException() {
super();
}

public LicenceNotFoundException(
String message
) {
super(message);
}

public LicenceNotFoundException(
String message,
Throwable throwable
) {
super(message, throwable);
}

public LicenceNotFoundException(
Throwable throwable
) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.alexisdev.balder.api.exceptions;

public class LicenceUsedException
extends RuntimeException {

public LicenceUsedException() {
super();
}

public LicenceUsedException(
String message
) {
super(message);
}

public LicenceUsedException(
String message,
Throwable throwable
) {
super(message, throwable);
}

public LicenceUsedException(
Throwable throwable
) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.alexisdev.balder.api.finder;

import dev.alexisdev.balder.api.licence.Licence;

public interface LicenceFinder {

/**
* Find a licence by its key.
*
* @param licence The licence key to find.
* @return A licence model.
*/
Licence find(
String licence
);
}
70 changes: 70 additions & 0 deletions api/src/main/java/dev/alexisdev/balder/api/licence/Licence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.alexisdev.balder.api.licence;

import dev.alexisdev.balder.api.model.Model;
import dev.alexisdev.balder.api.util.Validate;
import org.jetbrains.annotations.Nullable;

/**
* This class represents the Model for creating licenses.
*/
public class Licence
implements Model {

private final String id;
protected String address;

protected Licence(
String id,

String address
) {
this.id = Validate.notNull(id, "id");
this.address = address;
}

/**
* @return The licence id.
*/

@Override
public String getId() {
return id;
}

/**
* Returns the address of the person, or null if the person has no address.
*
* @return A String
*/
public @Nullable String getAddress() {
return address;
}

/**
* This function sets the address of the user to the given address.
*
* @param address The address of the server.
*/
public void setAddress(String address) {
this.address = Validate.notNull(
address, "address"
);
}

/**
* Create a new Licence object with the given id, key and address.
*
* @param id The id of the licence.
* @param address The address of the licence holder.
* @return A new instance of the Licence class.
*/
public static Licence create(
String id,
String address
) {
return new Licence(
id,
address
);
}
}
11 changes: 11 additions & 0 deletions api/src/main/java/dev/alexisdev/balder/api/model/Model.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.alexisdev.balder.api.model;

public interface Model {

/**
* Returns the id of the model.
*
* @return The id of the model.
*/
String getId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.alexisdev.balder.api.registry;

import dev.alexisdev.balder.api.exceptions.LicenceNotFoundException;
import dev.alexisdev.balder.api.exceptions.LicenceUsedException;
import dev.alexisdev.balder.api.finder.LicenceFinder;
import dev.alexisdev.balder.api.licence.Licence;
import dev.alexisdev.balder.api.updater.LicenceUpdater;
import dev.alexisdev.balder.api.util.Validate;

public class DefaultLicenceRegistry
implements LicenceRegistry {

protected final LicenceFinder licenceFinder;
protected final LicenceUpdater licenceUpdater;

protected DefaultLicenceRegistry(
LicenceFinder licenceFinder,
LicenceUpdater licenceUpdater
) {
this.licenceFinder = Validate.notNull(
licenceFinder, "licenceFinder"
);
this.licenceUpdater = licenceUpdater;
}

@Override
public void registry(
String licence,
String address
) {
Licence licenceModel = licenceFinder.find(
licence
);

if (licenceModel == null) {
throw new LicenceNotFoundException(
"The licence of key " + licence + " was not found."
);
}

if (licenceModel.getAddress() != null) {
licenceUsedAction(
licenceModel
);
return;
}

licenceModel.setAddress(
address
);

licenceUpdater.update(
licenceModel
);
}


/**
* This will be the method called in case the license is already used,
* in case you want a customized action when this event happens,
* it is recommended to extend the class of {@link DefaultLicenceRegistry}
* and modify the method of {@link DefaultLicenceRegistry#licenceUsedAction(Licence)}
*/
protected void licenceUsedAction(
Licence licence
) {
throw new LicenceUsedException(
"The licence of key " + licence.getId() + " is already used."
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.alexisdev.balder.api.registry;

import dev.alexisdev.balder.api.finder.LicenceFinder;
import dev.alexisdev.balder.api.updater.LicenceUpdater;

import java.net.InetAddress;
import java.net.UnknownHostException;

public interface LicenceRegistry {

default void registry(String licence) throws UnknownHostException {
registry(
licence,
InetAddress.getLocalHost().getHostAddress()
);
}

/**
* This function takes a licence and an address.
*
* @param licence The licence.
* @param address The address of the node.
*/
void registry(
String licence,
String address
);

/**
* "This function returns a new instance of the DefaultLicenceRegistry class, which is a subclass of the
* LicenceRegistry interface."
* <p>
* The first line of the function is the function signature. It tells us the name of the function, the type of the
* return value, and the types of the parameters
*
* @param licenceFinder A LicenceFinder object that can be used to find a licence by its ID.
* @param licenceUpdater This is the object that will be used to update the licence.
* @return A new instance of the DefaultLicenceRegistry class.
*/
static LicenceRegistry getDefault(
LicenceFinder licenceFinder,
LicenceUpdater licenceUpdater
) {
return new DefaultLicenceRegistry(
licenceFinder,
licenceUpdater
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.alexisdev.balder.api.updater;

import dev.alexisdev.balder.api.licence.Licence;

public interface LicenceUpdater {

/**
* It updates the licence.
*
* @param licence The licence object to be updated.
*/

void update(Licence licence);
}
26 changes: 26 additions & 0 deletions api/src/main/java/dev/alexisdev/balder/api/util/Validate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.alexisdev.balder.api.util;

public class Validate {

public Validate() {
throw new UnsupportedOperationException();
}

/**
* If the object is null, throw a NullPointerException with the given message.
*
* @param object The object to check.
* @param message The message to be displayed if the object is null.
* @return The object that was passed in.
*/
public static <T> T notNull(
T object,
String message
) {
if (object == null) {
throw new NullPointerException(message);
}

return object;
}
}
Loading

0 comments on commit b5bb517

Please sign in to comment.