docs: improve lockable object

This commit is contained in:
Ilkka Seppälä
2024-05-06 19:36:56 +03:00
parent 30cb9af12b
commit 92ae818fe2
10 changed files with 116 additions and 241 deletions
@@ -42,8 +42,8 @@ import lombok.extern.slf4j.Slf4j;
* the request.
*
* <p>In this example, we create a new Lockable object with the SwordOfAragorn implementation of it.
* Afterwards we create 6 Creatures with the Elf, Orc and Human implementations and assign them each
* to a Fiend object and the Sword is the target object. Because there is only one Sword and it uses
* Afterward we create 6 Creatures with the Elf, Orc and Human implementations and assign them each
* to a Fiend object and the Sword is the target object. Because there is only one Sword, and it uses
* the Lockable Object pattern, only one creature can hold the sword at a given time. When the sword
* is locked, any other alive Fiends will try to lock, which will result in a race to lock the
* sword.
@@ -24,11 +24,14 @@
*/
package com.iluwatar.lockableobject;
import java.io.Serial;
/**
* An exception regarding the locking process of a Lockable object.
*/
public class LockingException extends RuntimeException {
@Serial
private static final long serialVersionUID = 8556381044865867037L;
public LockingException(String message) {
@@ -66,7 +66,7 @@ public abstract class Creature {
return false;
}
/** Terminates the Creature and unlocks all of the Lockable that it posses. */
/** Terminates the Creature and unlocks all the Lockable that it possesses. */
public synchronized void kill() {
LOGGER.info("{} {} has been slayed!", type, name);
for (Lockable lockable : instruments) {
@@ -24,6 +24,8 @@
*/
package com.iluwatar.lockableobject.domain;
import lombok.Getter;
/** Attribute constants of each Creature implementation. */
public enum CreatureStats {
ELF_HEALTH(90),
@@ -33,13 +35,10 @@ public enum CreatureStats {
HUMAN_HEALTH(60),
HUMAN_DAMAGE(60);
int value;
@Getter
final int value;
private CreatureStats(int value) {
CreatureStats(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
@@ -30,7 +30,7 @@ import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** A Feind is a creature that all it wants is to posses a Lockable object. */
/** A Feind is a creature that wants to possess a Lockable object. */
public class Feind implements Runnable {
private final Creature creature;
@@ -53,12 +53,7 @@ public class Feind implements Runnable {
@Override
public void run() {
if (!creature.acquire(target)) {
try {
fightForTheSword(creature, target.getLocker(), target);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage());
Thread.currentThread().interrupt();
}
fightForTheSword(creature, target.getLocker(), target);
} else {
LOGGER.info("{} has acquired the sword!", target.getLocker().getName());
}
@@ -69,11 +64,9 @@ public class Feind implements Runnable {
*
* @param reacher as the source creature.
* @param holder as the foe.
* @param sword as the Lockable to posses.
* @throws InterruptedException in case of interruption.
* @param sword as the Lockable to possess.
*/
private void fightForTheSword(Creature reacher, @NonNull Creature holder, Lockable sword)
throws InterruptedException {
private void fightForTheSword(Creature reacher, @NonNull Creature holder, Lockable sword) {
LOGGER.info("A duel between {} and {} has been started!", reacher.getName(), holder.getName());
boolean randBool;
while (this.target.isLocked() && reacher.isAlive() && holder.isAlive()) {
@@ -28,7 +28,7 @@ package com.iluwatar.lockableobject.domain;
public class Human extends Creature {
/**
* A constructor that initializes the attributes of an human.
* A constructor that initializes the attributes of a human.
*
* @param name as the name of the creature.
*/
@@ -24,7 +24,7 @@
*/
package com.iluwatar.lockableobject.domain;
/** A Orc implementation of a Creature. */
/** An Orc implementation of a Creature. */
public class Orc extends Creature {
/**
* A constructor that initializes the attributes of an orc.