-
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.
Merge pull request #1 from alexissdev/develop
Added all features
- Loading branch information
Showing
36 changed files
with
1,072 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
12 changes: 12 additions & 0 deletions
12
api/src/main/java/dev/alexisdev/balder/api/buildable/Buildable.java
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,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(); | ||
} |
32 changes: 32 additions & 0 deletions
32
api/src/main/java/dev/alexisdev/balder/api/checker/DefaultLicenceChecker.java
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,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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
api/src/main/java/dev/alexisdev/balder/api/checker/LicenceChecker.java
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,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 | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/dev/alexisdev/balder/api/exceptions/LicenceNotFoundException.java
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,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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/dev/alexisdev/balder/api/exceptions/LicenceUsedException.java
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,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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
api/src/main/java/dev/alexisdev/balder/api/finder/LicenceFinder.java
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,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
70
api/src/main/java/dev/alexisdev/balder/api/licence/Licence.java
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,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
11
api/src/main/java/dev/alexisdev/balder/api/model/Model.java
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,11 @@ | ||
package dev.alexisdev.balder.api.model; | ||
|
||
public interface Model { | ||
|
||
/** | ||
* Returns the id of the model. | ||
* | ||
* @return The id of the model. | ||
*/ | ||
String getId(); | ||
} |
71 changes: 71 additions & 0 deletions
71
api/src/main/java/dev/alexisdev/balder/api/registry/DefaultLicenceRegistry.java
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,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." | ||
); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
api/src/main/java/dev/alexisdev/balder/api/registry/LicenceRegistry.java
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,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 | ||
); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
api/src/main/java/dev/alexisdev/balder/api/updater/LicenceUpdater.java
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,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
26
api/src/main/java/dev/alexisdev/balder/api/util/Validate.java
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,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; | ||
} | ||
} |
Oops, something went wrong.