Files
FT546-WCS/ruoyi-wcs-shadow/src/main/java/com/ruoyi/shadow/facts/ShadowFactBatch.java

126 lines
4.9 KiB
Java

package com.ruoyi.shadow.facts;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public final class ShadowFactBatch {
private final Instant databaseCaptureStartedAt;
private final Instant databaseCapturedAt;
private final List<ShadowQueueFact> sourceQueues;
private final List<ShadowQueueFact> queues;
private final List<ShadowVehicleFact> vehicles;
private final List<ShadowMapFact> maps;
private ShadowFactBatch(Instant databaseCaptureStartedAt,
Instant databaseCapturedAt,
List<ShadowQueueFact> sourceQueues,
List<ShadowVehicleFact> vehicles,
List<ShadowMapFact> maps) {
this.databaseCaptureStartedAt = databaseCaptureStartedAt;
this.databaseCapturedAt = databaseCapturedAt;
this.sourceQueues = immutable(sourceQueues);
this.queues = immutable(deduplicateAndSort(sourceQueues));
this.vehicles = immutable(sortVehicles(vehicles));
this.maps = immutable(sortMaps(maps));
}
public static ShadowFactBatch create(Instant startedAt, Instant capturedAt,
List<ShadowQueueFact> queues,
List<ShadowVehicleFact> vehicles,
List<ShadowMapFact> maps) {
return new ShadowFactBatch(startedAt, capturedAt,
requiredList(queues, "queues"),
requiredList(vehicles, "vehicles"),
requiredList(maps, "maps"));
}
public Instant getDatabaseCaptureStartedAt() { return databaseCaptureStartedAt; }
public Instant getDatabaseCapturedAt() { return databaseCapturedAt; }
public List<ShadowQueueFact> getSourceQueues() { return sourceQueues; }
public List<ShadowQueueFact> getQueues() { return queues; }
public List<ShadowVehicleFact> getVehicles() { return vehicles; }
public List<ShadowMapFact> getMaps() { return maps; }
public ShadowQueueFact queue(String guid) {
for (ShadowQueueFact fact : queues) {
if (fact.isActive() && fact.getGuid().equals(guid)) {
return fact;
}
}
for (ShadowQueueFact fact : queues) {
if (fact.getGuid().equals(guid)) {
return fact;
}
}
return null;
}
public ShadowVehicleFact vehicle(String deviceId) {
for (ShadowVehicleFact fact : vehicles) {
if (fact.getDeviceId().equals(deviceId)) {
return fact;
}
}
return null;
}
private static List<ShadowQueueFact> deduplicateAndSort(
List<ShadowQueueFact> source) {
Map<String, ShadowQueueFact> unique =
new LinkedHashMap<String, ShadowQueueFact>();
for (ShadowQueueFact fact : source) {
String key = fact.getTableName() + '\u0000' + fact.getGuid()
+ '\u0000' + fact.getContentHash();
unique.put(key, fact);
}
List<ShadowQueueFact> result =
new ArrayList<ShadowQueueFact>(unique.values());
Collections.sort(result, Comparator
.comparing(ShadowQueueFact::getCreatedAt,
Comparator.nullsFirst(Comparator.naturalOrder()))
.thenComparing(ShadowQueueFact::getGuid)
.thenComparing(ShadowQueueFact::getContentHash));
return result;
}
private static List<ShadowVehicleFact> sortVehicles(
List<ShadowVehicleFact> source) {
List<ShadowVehicleFact> result =
new ArrayList<ShadowVehicleFact>(source);
Collections.sort(result, Comparator
.comparing(ShadowVehicleFact::getDeviceId)
.thenComparing(ShadowVehicleFact::getGuid)
.thenComparing(ShadowVehicleFact::getContentHash));
return result;
}
private static List<ShadowMapFact> sortMaps(List<ShadowMapFact> source) {
List<ShadowMapFact> result = new ArrayList<ShadowMapFact>(source);
Collections.sort(result, Comparator
.comparing(ShadowMapFact::getSiteId)
.thenComparing(ShadowMapFact::getGuid));
return result;
}
private static <T> List<T> immutable(List<T> source) {
return Collections.unmodifiableList(new ArrayList<T>(source));
}
private static <T> List<T> requiredList(List<T> source, String name) {
if (source == null) {
throw new IllegalArgumentException(name + " must not be null");
}
for (T value : source) {
if (value == null) {
throw new IllegalArgumentException(name + " must not contain null");
}
}
return source;
}
}