L’API Topic fournit la structure de données CollaborationList pour stocker des valeurs ordonnées et s’abonner à leurs modifications. Vous pouvez obtenir une CollaborationList par son nom dans le callback d’activation de la connexion Topic.
Source code
CollaborationListExample.java
package com.vaadin.demo.collaboration;
import com.vaadin.collaborationengine.CollaborationEngine;
import com.vaadin.collaborationengine.CollaborationList;
import com.vaadin.collaborationengine.ListOperation;
import com.vaadin.collaborationengine.ListOperationResult;
import com.vaadin.collaborationengine.ListKey;
import com.vaadin.collaborationengine.UserInfo;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
public class CollaborationListExample extends VerticalLayout {
public CollaborationListExample() {
// NOTE: Use the user id of the logged-in user
// instead
String userId = System.identityHashCode(UI.getCurrentOrThrow()) + "";
UserInfo localUser = new UserInfo(userId, "User " + userId);
CollaborationEngine.getInstance().openTopicConnection(this, "my-topic",
localUser, connection -> {
CollaborationList list = connection.getNamedList("my-list");
ListOperationResult<Void> result = list.insertLast("foo");
result.getCompletableFuture().thenAccept(v -> {
// do something when the insertion completes
});
ListKey key = result.getKey();
// Replace an item using its key
list.set(key, "bar");
// Get an item value using its key
String bar = list.getItem(key, String.class);
// Insert an item at the end of the list
// Only succeeds if key is currently last
ListOperation operation1 = ListOperation.insertLast("baz")
.ifLast(key);
ListOperationResult<Boolean> result1 = list
.apply(operation1);
ListKey key1 = result1.getKey();
// Insert an item before key1
// Only succeeds if key1 is currently last
ListOperation operation2 = ListOperation
.insertBefore(key1, "qux").ifLast(key1);
ListOperationResult<Boolean> result2 = list
.apply(operation2);
ListKey key2 = result2.getKey();
// Insert an item between two keys
// Only succeeds if the keys are consecutive
ListOperation operation3 = ListOperation.insertBetween(key2,
key1, "xyz");
ListOperationResult<Boolean> result3 = list
.apply(operation3);
// Remove an item using its key
list.remove(key);
return null;
});
}
}
Lorsque vous avez obtenu l’instance de la liste, vous pouvez insérer des valeurs à la fin de la liste avec la méthode insertLast().
Source code
CollaborationListExample.java
package com.vaadin.demo.collaboration;
import com.vaadin.collaborationengine.CollaborationEngine;
import com.vaadin.collaborationengine.CollaborationList;
import com.vaadin.collaborationengine.ListOperation;
import com.vaadin.collaborationengine.ListOperationResult;
import com.vaadin.collaborationengine.ListKey;
import com.vaadin.collaborationengine.UserInfo;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
public class CollaborationListExample extends VerticalLayout {
public CollaborationListExample() {
// NOTE: Use the user id of the logged-in user
// instead
String userId = System.identityHashCode(UI.getCurrentOrThrow()) + "";
UserInfo localUser = new UserInfo(userId, "User " + userId);
CollaborationEngine.getInstance().openTopicConnection(this, "my-topic",
localUser, connection -> {
CollaborationList list = connection.getNamedList("my-list");
ListOperationResult<Void> result = list.insertLast("foo");
result.getCompletableFuture().thenAccept(v -> {
// do something when the insertion completes
});
ListKey key = result.getKey();
// Replace an item using its key
list.set(key, "bar");
// Get an item value using its key
String bar = list.getItem(key, String.class);
// Insert an item at the end of the list
// Only succeeds if key is currently last
ListOperation operation1 = ListOperation.insertLast("baz")
.ifLast(key);
ListOperationResult<Boolean> result1 = list
.apply(operation1);
ListKey key1 = result1.getKey();
// Insert an item before key1
// Only succeeds if key1 is currently last
ListOperation operation2 = ListOperation
.insertBefore(key1, "qux").ifLast(key1);
ListOperationResult<Boolean> result2 = list
.apply(operation2);
ListKey key2 = result2.getKey();
// Insert an item between two keys
// Only succeeds if the keys are consecutive
ListOperation operation3 = ListOperation.insertBetween(key2,
key1, "xyz");
ListOperationResult<Boolean> result3 = list
.apply(operation3);
// Remove an item using its key
list.remove(key);
return null;
});
}
}
Comme l’insertion est asynchrone, la méthode renvoie un objet de résultat à partir duquel vous pouvez obtenir un CompletableFuture que vous pouvez utiliser pour déterminer quand l’insertion est terminée.
Source code
CollaborationListExample.java
package com.vaadin.demo.collaboration;
import com.vaadin.collaborationengine.CollaborationEngine;
import com.vaadin.collaborationengine.CollaborationList;
import com.vaadin.collaborationengine.ListOperation;
import com.vaadin.collaborationengine.ListOperationResult;
import com.vaadin.collaborationengine.ListKey;
import com.vaadin.collaborationengine.UserInfo;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
public class CollaborationListExample extends VerticalLayout {
public CollaborationListExample() {
// NOTE: Use the user id of the logged-in user
// instead
String userId = System.identityHashCode(UI.getCurrentOrThrow()) + "";
UserInfo localUser = new UserInfo(userId, "User " + userId);
CollaborationEngine.getInstance().openTopicConnection(this, "my-topic",
localUser, connection -> {
CollaborationList list = connection.getNamedList("my-list");
ListOperationResult<Void> result = list.insertLast("foo");
result.getCompletableFuture().thenAccept(v -> {
// do something when the insertion completes
});
ListKey key = result.getKey();
// Replace an item using its key
list.set(key, "bar");
// Get an item value using its key
String bar = list.getItem(key, String.class);
// Insert an item at the end of the list
// Only succeeds if key is currently last
ListOperation operation1 = ListOperation.insertLast("baz")
.ifLast(key);
ListOperationResult<Boolean> result1 = list
.apply(operation1);
ListKey key1 = result1.getKey();
// Insert an item before key1
// Only succeeds if key1 is currently last
ListOperation operation2 = ListOperation
.insertBefore(key1, "qux").ifLast(key1);
ListOperationResult<Boolean> result2 = list
.apply(operation2);
ListKey key2 = result2.getKey();
// Insert an item between two keys
// Only succeeds if the keys are consecutive
ListOperation operation3 = ListOperation.insertBetween(key2,
key1, "xyz");
ListOperationResult<Boolean> result3 = list
.apply(operation3);
// Remove an item using its key
list.remove(key);
return null;
});
}
}
L’objet de résultat fournit également le ListKey de l’élément inséré, que vous pouvez utiliser pour remplacer l’élément lui-même, obtenir sa valeur actuelle, ou le supprimer de la liste.
Source code
CollaborationListExample.java
package com.vaadin.demo.collaboration;
import com.vaadin.collaborationengine.CollaborationEngine;
import com.vaadin.collaborationengine.CollaborationList;
import com.vaadin.collaborationengine.ListOperation;
import com.vaadin.collaborationengine.ListOperationResult;
import com.vaadin.collaborationengine.ListKey;
import com.vaadin.collaborationengine.UserInfo;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
public class CollaborationListExample extends VerticalLayout {
public CollaborationListExample() {
// NOTE: Use the user id of the logged-in user
// instead
String userId = System.identityHashCode(UI.getCurrentOrThrow()) + "";
UserInfo localUser = new UserInfo(userId, "User " + userId);
CollaborationEngine.getInstance().openTopicConnection(this, "my-topic",
localUser, connection -> {
CollaborationList list = connection.getNamedList("my-list");
ListOperationResult<Void> result = list.insertLast("foo");
result.getCompletableFuture().thenAccept(v -> {
// do something when the insertion completes
});
ListKey key = result.getKey();
// Replace an item using its key
list.set(key, "bar");
// Get an item value using its key
String bar = list.getItem(key, String.class);
// Insert an item at the end of the list
// Only succeeds if key is currently last
ListOperation operation1 = ListOperation.insertLast("baz")
.ifLast(key);
ListOperationResult<Boolean> result1 = list
.apply(operation1);
ListKey key1 = result1.getKey();
// Insert an item before key1
// Only succeeds if key1 is currently last
ListOperation operation2 = ListOperation
.insertBefore(key1, "qux").ifLast(key1);
ListOperationResult<Boolean> result2 = list
.apply(operation2);
ListKey key2 = result2.getKey();
// Insert an item between two keys
// Only succeeds if the keys are consecutive
ListOperation operation3 = ListOperation.insertBetween(key2,
key1, "xyz");
ListOperationResult<Boolean> result3 = list
.apply(operation3);
// Remove an item using its key
list.remove(key);
return null;
});
}
}
Opérations avancées sur les listes
La classe ListOperation vous permet de préparer une opération de liste qui peut ensuite être appliquée à la liste en utilisant la méthode CollaborationList::apply.
La raison pour laquelle vous pourriez vouloir utiliser cette classe est de définir certaines conditions qui doivent être remplies lorsque l’opération est tentée. Si une condition n’est pas remplie, l’opération n’est pas effectuée. Ceci est utile pour se protéger contre des opérations en double.
ListOperation dispose de plusieurs méthodes statiques et conditionnelles pour insérer des valeurs :
Method
Type
Description
insertFirst()
static
Inséré au début de la liste.
insertLast()
static
Inséré à la fin de la liste.
insertBefore()
static
Inséré immédiatement avant une clé spécifiée.
insertAfter()
static
Inséré immédiatement après une clé spécifiée.
insertBetween()
static
Raccourci pour insertAfter(value, keyBefore).ifNext(keyBefore, keyAfter).
ifFirst(key)
conditional
La key spécifiée doit être la première dans la liste.
ifLast(key)
conditional
Doit être la dernière dans la liste.
ifPrev(key, keyPrev)
conditional
La keyPrev spécifiée doit se trouver immédiatement avant key.
ifNext(key, keyNext)
conditional
La keyNext spécifiée doit se trouver immédiatement après key.
ifEmpty()
conditional
La liste ne doit contenir aucune entrée.
ifNotEmpty()
conditional
La liste doit contenir au moins une entrée.
L’exemple ici montre comment certaines de ces méthodes peuvent être utilisées :
Source code
CollaborationListExample.java
package com.vaadin.demo.collaboration;
import com.vaadin.collaborationengine.CollaborationEngine;
import com.vaadin.collaborationengine.CollaborationList;
import com.vaadin.collaborationengine.ListOperation;
import com.vaadin.collaborationengine.ListOperationResult;
import com.vaadin.collaborationengine.ListKey;
import com.vaadin.collaborationengine.UserInfo;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
public class CollaborationListExample extends VerticalLayout {
public CollaborationListExample() {
// NOTE: Use the user id of the logged-in user
// instead
String userId = System.identityHashCode(UI.getCurrentOrThrow()) + "";
UserInfo localUser = new UserInfo(userId, "User " + userId);
CollaborationEngine.getInstance().openTopicConnection(this, "my-topic",
localUser, connection -> {
CollaborationList list = connection.getNamedList("my-list");
ListOperationResult<Void> result = list.insertLast("foo");
result.getCompletableFuture().thenAccept(v -> {
// do something when the insertion completes
});
ListKey key = result.getKey();
// Replace an item using its key
list.set(key, "bar");
// Get an item value using its key
String bar = list.getItem(key, String.class);
// Insert an item at the end of the list
// Only succeeds if key is currently last
ListOperation operation1 = ListOperation.insertLast("baz")
.ifLast(key);
ListOperationResult<Boolean> result1 = list
.apply(operation1);
ListKey key1 = result1.getKey();
// Insert an item before key1
// Only succeeds if key1 is currently last
ListOperation operation2 = ListOperation
.insertBefore(key1, "qux").ifLast(key1);
ListOperationResult<Boolean> result2 = list
.apply(operation2);
ListKey key2 = result2.getKey();
// Insert an item between two keys
// Only succeeds if the keys are consecutive
ListOperation operation3 = ListOperation.insertBetween(key2,
key1, "xyz");
ListOperationResult<Boolean> result3 = list
.apply(operation3);
// Remove an item using its key
list.remove(key);
return null;
});
}
}