public class Cluster extends SubscribableCrudModel<ClusterListener>
A cluster represents a set of commodities, sub-clusters, and transports.
A cluster is used to group related models. That relation could be geographic
type of transportation, or anything else. To sync the cluster with a
cluster on the Pathfinder server use the SubscribableCrudModel.connect() method.
Note, that clusters, as are all models, are implemented as singletons.
If a cluster already exists with the same path it will be returned, not a
new object. Use getInstance(java.lang.String, xyz.thepathfinder.android.PathfinderServices) to retrieve an cluster object.
Commodity,
ClusterListener,
Transport| Modifier and Type | Field and Description |
|---|---|
private Map<String,Commodity> |
commodities
Maps a path in the form of a string to the commodities directly under this cluster
|
private static org.slf4j.Logger |
logger |
private List<Route> |
routes
List of routes for this cluster.
|
private Map<String,Cluster> |
subclusters
Maps a path in the form of a string to the sub-clusters directly under this cluster
|
private Map<String,Transport> |
transports
Maps a path in the form of a string to the transports directly under this cluster
|
| Modifier | Constructor and Description |
|---|---|
private |
Cluster(String path,
PathfinderServices services)
Constructor for a cluster object.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addCommodity(Commodity commodity)
Adds a commodity to this cluster's list of available commodities.
|
protected void |
addTransport(Transport transport)
Adds a transport to this cluster's list of available transports.
|
private static boolean |
checkClusterFields(JsonObject clusterJson)
Checks the json object for all the required fields of a cluster object.
|
void |
createCommodity(double startLatitude,
double startLongitude,
double endLatitude,
double endLongitude,
CommodityStatus status,
JsonObject metadata)
Creates a commodity under this cluster on the pathfinder server.
|
private Commodity |
createCommodity(JsonObject commodityJson)
Creates a commodity with a values specified in the JSON object.
|
void |
createSubcluster(String name)
Creates a subcluster under this cluster on the pathfinder server.
|
void |
createTransport(double latitude,
double longitude,
TransportStatus status,
JsonObject metadata)
Creates a transport under this cluster on the pathfinder server.
|
private Transport |
createTransport(JsonObject transportJson)
Creates a connected transport under this cluster with the values in the JSON object provided.
|
protected JsonObject |
createValueJson()
Returns the value used in create request to the Pathfinder server
|
Collection<Commodity> |
getCommodities()
Returns an immutable collection of this cluster's commodities.
|
Map<String,Commodity> |
getCommoditiesMap()
Returns an immutable map of this cluster's commodities.
|
Commodity |
getCommodity(String path)
Returns a commodity directly under this cluster by its path.
|
protected static Cluster |
getInstance(JsonObject clusterJson,
PathfinderServices services)
Returns an instance of cluster object based on the path in clusterJson.
|
protected static Cluster |
getInstance(String path,
PathfinderServices services)
Returns an instance of cluster object based on the path parameter.
|
private static String |
getPath(JsonObject clusterJson)
Returns the path as a string from a JSON object formatted as cluster.
|
Collection<Route> |
getRoutes()
Returns an immutable collection of this cluster's routes.
|
Cluster |
getSubcluster(String path)
Returns a direct descendant subcluster by its path.
|
Collection<Cluster> |
getSubclusters()
Returns an immutable collection of this cluster's direct subclusters.
|
Map<String,Cluster> |
getSubclustersMap()
Returns an immutable map of this cluster's direct subclusters.
|
private String |
getSubmodelPath(JsonObject json) |
Transport |
getTransport(String path)
Returns a transport directly under this cluster by its path.
|
Collection<Transport> |
getTransports()
Returns an immutable collection of this cluster's transports.
|
Map<String,Transport> |
getTransportsMap()
Returns an immutable map of this cluster's transports.
|
protected void |
route(JsonObject json,
PathfinderServices services)
Updates the models routes.
|
private void |
setCommodities(Iterable<Commodity> commodities)
Sets this cluster's commodities.
|
private void |
setCommodities(Map<String,Commodity> commodities)
Sets this cluster's commodities.
|
private void |
setRoutes(List<Route> routes)
Sets this cluster's routes.
|
private void |
setSubclusters(Iterable<Cluster> subclusters)
Sets this cluster's sub-clusters.
|
private void |
setSubclusters(Map<String,Cluster> subclusters)
Sets this cluster's sub-clusters.
|
private void |
setTransports(Iterable<Transport> transports)
Sets this cluster's transports.
|
private void |
setTransports(Map<String,Transport> transports)
Sets this cluster's transports.
|
String |
toString() |
protected boolean |
updateFields(JsonObject json)
Updates the fields of the model.
|
connect, create, delete, updategetMessageHeader, routeSubscribe, routeUnsubscribe, subscribe, unsubscribegetChildPath, getModelType, getName, getParentCluster, getParentPath, getPath, getPathName, getServices, isConnected, notifyUpdate, setConnectedaddListener, getListeners, removeListenerprivate static final org.slf4j.Logger logger
private Map<String,Commodity> commodities
private Map<String,Cluster> subclusters
private Map<String,Transport> transports
private Cluster(String path, PathfinderServices services)
getInstance(String, PathfinderServices).
Each path must only refer to one object.path - The path name of the cluster.services - A service object to send messages to the server and keep track of all
Model objects.IllegalArgumentException - occurs when a cluster has already been registered with the same path.protected static Cluster getInstance(String path, PathfinderServices services)
path - The full path to pathfinder modelservices - The pathfinder services object.protected static Cluster getInstance(JsonObject clusterJson, PathfinderServices services)
clusterJson - A json object that parses to a cluster.services - The pathfinder services object.IllegalArgumentException - occurs if the json object doesn't parse to
a cluster object.private static boolean checkClusterFields(JsonObject clusterJson)
clusterJson - JsonObject to check if it will parse to a cluster.private static String getPath(JsonObject clusterJson)
clusterJson - JSON object that represents a clusterpublic void createCommodity(double startLatitude,
double startLongitude,
double endLatitude,
double endLongitude,
CommodityStatus status,
JsonObject metadata)
startLatitude - The pick up latitude of the commodity.startLongitude - The pick up longitude of the commodity.endLatitude - The drop off latitude of the commodity.endLongitude - The drop off longitude of the commodity.status - The current status of the commodity. If null it will default to CommodityStatus.OFFLINE.metadata - A JsonObject representing the metadata field of the commodity. If null it will default to an empty JsonObject.protected void addCommodity(Commodity commodity)
commodity - to be added.private Commodity createCommodity(JsonObject commodityJson)
commodityJson - a JSON object that represents a commoditypublic Commodity getCommodity(String path)
path - of the commodity.public Collection<Commodity> getCommodities()
public Map<String,Commodity> getCommoditiesMap()
private void setCommodities(Iterable<Commodity> commodities)
commodities - an iterable collection of commoditiesprivate void setCommodities(Map<String,Commodity> commodities)
commodities - a map of commoditiespublic void createSubcluster(String name)
name - Name of the subcluster, it must form a unique identifier when concatenated with this cluster's path.public Cluster getSubcluster(String path)
path - of the subclusterpublic Collection<Cluster> getSubclusters()
public Map<String,Cluster> getSubclustersMap()
private void setSubclusters(Iterable<Cluster> subclusters)
subclusters - an iterable collection of clustersprivate void setSubclusters(Map<String,Cluster> subclusters)
subclusters - a map of clustersprotected void addTransport(Transport transport)
transport - to be added.public void createTransport(double latitude,
double longitude,
TransportStatus status,
JsonObject metadata)
latitude - The current latitude of the transport.longitude - The current longitude of the transport.status - The current status of the transport. If null it defaults to TransportStatus.OFFLINEmetadata - The transports's metadata field. If null it defaults to an empty JSON object.private Transport createTransport(JsonObject transportJson)
transportJson - a JSON object that represents a transport.public Transport getTransport(String path)
path - of the transport.public Collection<Transport> getTransports()
public Map<String,Transport> getTransportsMap()
private void setTransports(Iterable<Transport> transports)
transports - an iterable collection of transportsprivate void setTransports(Map<String,Transport> transports)
transports - a map of transportsprivate void setRoutes(List<Route> routes)
routes - a list of routes for this clusterpublic Collection<Route> getRoutes()
protected JsonObject createValueJson()
createValueJson in class Model<ClusterListener>private String getSubmodelPath(JsonObject json)
protected boolean updateFields(JsonObject json)
updateFields in class Model<ClusterListener>json - of the model.protected void route(JsonObject json, PathfinderServices services)
route in class Model<ClusterListener>json - of the model.services - pathfinder services object.