-
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
May 26, 2024
1 parent
3705f48
commit 689fc28
Showing
7 changed files
with
174 additions
and
7 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
30 changes: 30 additions & 0 deletions
30
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/LockedBooks.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,30 @@ | ||
package org.vaadin.tatu.vaadincreate.crud; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collection; | ||
|
||
@SuppressWarnings("serial") | ||
public interface LockedBooks { | ||
|
||
public Collection<Integer> lockedBooks(); | ||
|
||
public void lock(Integer id); | ||
|
||
public void unlock(Integer id); | ||
|
||
public static LockedBooks get() { | ||
return LockedBooksImpl.getInstance(); | ||
} | ||
|
||
public static class BookEvent implements Serializable { | ||
private Integer id; | ||
|
||
public BookEvent(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/LockedBooksImpl.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,64 @@ | ||
package org.vaadin.tatu.vaadincreate.crud; | ||
|
||
import java.util.Collection; | ||
import java.util.WeakHashMap; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.vaadin.tatu.vaadincreate.eventbus.EventBus; | ||
|
||
@SuppressWarnings("serial") | ||
public class LockedBooksImpl implements LockedBooks { | ||
|
||
private static LockedBooksImpl INSTANCE; | ||
private EventBus eventBus = EventBus.get(); | ||
|
||
private final WeakHashMap<Integer, Object> books = new WeakHashMap<>(); | ||
|
||
public synchronized static LockedBooks getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new LockedBooksImpl(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
private LockedBooksImpl() { | ||
} | ||
|
||
@Override | ||
public Collection<Integer> lockedBooks() { | ||
synchronized (books) { | ||
return books.keySet(); | ||
} | ||
} | ||
|
||
@Override | ||
public void lock(Integer id) { | ||
synchronized (books) { | ||
var match = books.keySet().stream().filter(i -> i.equals(id)) | ||
.findFirst(); | ||
if (match.isPresent()) { | ||
throw new IllegalStateException( | ||
"Can't open book already opened: " + id); | ||
} | ||
books.put(id, null); | ||
eventBus.post(new BookEvent(id)); | ||
logger.info("Locked book {}", id); | ||
} | ||
} | ||
|
||
@Override | ||
public void unlock(Integer id) { | ||
synchronized (books) { | ||
var match = books.keySet().stream().filter(i -> i.equals(id)) | ||
.findFirst(); | ||
if (match.isPresent()) { | ||
books.remove(match.get()); | ||
} | ||
eventBus.post(new BookEvent(id)); | ||
logger.info("Unlocked book {}", id); | ||
} | ||
} | ||
|
||
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
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