-
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.
- Loading branch information
TatuJLund
committed
Jun 4, 2024
1 parent
8480a25
commit 103236d
Showing
14 changed files
with
193 additions
and
126 deletions.
There are no files selected for viewing
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
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
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
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
30 changes: 0 additions & 30 deletions
30
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/LockedBooks.java
This file was deleted.
Oops, something went wrong.
68 changes: 0 additions & 68 deletions
68
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/LockedBooksImpl.java
This file was deleted.
Oops, something went wrong.
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
43 changes: 43 additions & 0 deletions
43
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/locking/LockedObjects.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,43 @@ | ||
package org.vaadin.tatu.vaadincreate.locking; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.vaadin.tatu.vaadincreate.backend.data.User; | ||
|
||
@SuppressWarnings("serial") | ||
public interface LockedObjects { | ||
|
||
public User isLocked(Class<?> type, Integer id); | ||
|
||
public void lock(Class<?> type, Integer id, User user); | ||
|
||
public void unlock(Class<?> type, Integer id); | ||
|
||
public static LockedObjects get() { | ||
return LockedObjectsImpl.getInstance(); | ||
} | ||
|
||
public static class LockingEvent implements Serializable { | ||
private Integer id; | ||
private User user; | ||
private Class<?> type; | ||
|
||
public LockingEvent(Class<?> type, Integer id, User user) { | ||
this.id = id; | ||
this.type = type; | ||
this.user = user; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public Class<?> getType() { | ||
return type; | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/locking/LockedObjectsImpl.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,91 @@ | ||
package org.vaadin.tatu.vaadincreate.locking; | ||
|
||
import java.io.Serializable; | ||
import java.util.Optional; | ||
import java.util.WeakHashMap; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.vaadin.tatu.vaadincreate.backend.data.User; | ||
import org.vaadin.tatu.vaadincreate.eventbus.EventBus; | ||
|
||
@SuppressWarnings("serial") | ||
public class LockedObjectsImpl implements LockedObjects { | ||
|
||
private static LockedObjectsImpl INSTANCE; | ||
private EventBus eventBus = EventBus.get(); | ||
|
||
private final WeakHashMap<LockedObject, Object> lockedObjects = new WeakHashMap<>(); | ||
|
||
public synchronized static LockedObjects getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new LockedObjectsImpl(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
private LockedObjectsImpl() { | ||
} | ||
|
||
@Override | ||
public User isLocked(Class<?> type, Integer id) { | ||
synchronized (lockedObjects) { | ||
var match = findObject(type, id); | ||
if (match.isPresent()) { | ||
return match.get().user; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void lock(Class<?> type, Integer id, User user) { | ||
if (id != null && id < 0) { | ||
throw new IllegalArgumentException( | ||
"Id can't be null and must be positive"); | ||
} | ||
synchronized (lockedObjects) { | ||
var match = findObject(type, id); | ||
if (match.isPresent()) { | ||
throw new IllegalStateException( | ||
"Can't locked book already locked: " + id); | ||
} | ||
lockedObjects.put(new LockedObject(type, id, user), null); | ||
eventBus.post(new LockingEvent(type, id, user)); | ||
logger.debug("Locked book {}", id); | ||
} | ||
} | ||
|
||
@Override | ||
public void unlock(Class<?> type, Integer id) { | ||
synchronized (lockedObjects) { | ||
var match = findObject(type, id); | ||
if (match.isPresent()) { | ||
var object = match.get(); | ||
lockedObjects.remove(object); | ||
eventBus.post(new LockingEvent(type, id, object.user)); | ||
} | ||
logger.debug("Unlocked book {}", id); | ||
} | ||
} | ||
|
||
private Optional<LockedObject> findObject(Class<?> type, Integer id) { | ||
return lockedObjects.keySet().stream() | ||
.filter(obj -> obj.type.equals(type) && obj.id.equals(id)) | ||
.findFirst(); | ||
} | ||
|
||
static class LockedObject implements Serializable { | ||
Integer id; | ||
Class<?> type; | ||
User user; | ||
|
||
LockedObject(Class<?> type, Integer id, User user) { | ||
this.type = type; | ||
this.id = id; | ||
this.user = user; | ||
} | ||
} | ||
|
||
private Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
} |
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
Oops, something went wrong.