La Topic API fornisce la struttura dati CollaborationList per memorizzare valori ordinati e sottoscrivere le loro modifiche. È possibile ottenere una CollaborationList in base al suo nome nel callback di attivazione della connessione 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;
});
}
}
Quando ha ottenuto l’istanza della lista, può inserire valori alla fine della lista con il metodo 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;
});
}
}
Poiché l’inserimento è asincrono, il metodo restituisce un oggetto risultato dal quale può ottenere un CompletableFuture che può usare per determinare quando l’inserimento è stato completato.
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’oggetto risultato fornisce anche il ListKey dell’elemento inserito, che può usare per sostituire l’elemento stesso, ottenere il suo valore corrente, oppure rimuoverlo dalla lista.
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;
});
}
}
Operazioni avanzate sulla lista
La classe ListOperation consente di preparare un’operazione sulla lista che può poi essere applicata alla lista usando il metodo CollaborationList::apply.
Il motivo per cui potrebbe voler usare questa classe è definire determinate condizioni che devono essere soddisfatte quando si tenta l’operazione. Se una condizione non è soddisfatta, l’operazione non viene completata. Questo è utile per proteggersi da operazioni duplicate.
ListOperation dispone di diversi metodi statici e condizionali per inserire valori:
Method
Type
Description
insertFirst()
static
Inserito all’inizio della lista.
insertLast()
static
Inserito alla fine della lista.
insertBefore()
static
Inserito immediatamente prima di una chiave specificata.
insertAfter()
static
Inserito immediatamente dopo una chiave specificata.
insertBetween()
static
Abbreviazione di insertAfter(value, keyBefore).ifNext(keyBefore, keyAfter).
ifFirst(key)
conditional
La key specificata deve essere la prima nella lista.
ifLast(key)
conditional
Deve essere l’ultima nella lista.
ifPrev(key, keyPrev)
conditional
La keyPrev specificata deve essere immediatamente prima di key.
ifNext(key, keyNext)
conditional
La keyNext specificata deve essere immediatamente dopo key.
ifEmpty()
conditional
La lista non deve avere voci.
ifNotEmpty()
conditional
La lista deve avere almeno una voce.
L’esempio qui mostra come alcuni di questi metodi potrebbero essere usati:
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;
});
}
}