Skip to content
Snippets Groups Projects
Commit 85e6b62c authored by Emmanuel Bruno's avatar Emmanuel Bruno
Browse files

adds thread safe id inc.

parent 80cccb4d
Branches
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ import java.io.Serializable; ...@@ -18,6 +18,7 @@ import java.io.Serializable;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -28,14 +29,14 @@ import static fr.univtln.bruno.samples.jaxrs.model.BiblioModel.Field.valueOf; ...@@ -28,14 +29,14 @@ import static fr.univtln.bruno.samples.jaxrs.model.BiblioModel.Field.valueOf;
@FieldDefaults(level = AccessLevel.PRIVATE) @FieldDefaults(level = AccessLevel.PRIVATE)
@NoArgsConstructor(staticName = "of") @NoArgsConstructor(staticName = "of")
public class BiblioModel { public class BiblioModel {
private static long lastId = 0; private static AtomicLong lastId = new AtomicLong(0);
@Delegate @Delegate
final MutableLongObjectMap<Auteur> auteurs = LongObjectMaps.mutable.empty(); final MutableLongObjectMap<Auteur> auteurs = LongObjectMaps.mutable.empty();
public Auteur addAuteur(Auteur auteur) throws IllegalArgumentException { public Auteur addAuteur(Auteur auteur) throws IllegalArgumentException {
if (auteur.id != 0) throw new IllegalArgumentException(); if (auteur.id != 0) throw new IllegalArgumentException();
auteur.id = ++lastId; auteur.id = lastId.incrementAndGet();
auteurs.put(auteur.id, auteur); auteurs.put(auteur.id, auteur);
return auteur; return auteur;
} }
......
...@@ -6,7 +6,6 @@ import jakarta.ws.rs.container.ContainerResponseFilter; ...@@ -6,7 +6,6 @@ import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider; import jakarta.ws.rs.ext.Provider;
import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@Provider @Provider
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment