安全发布底层状态的车辆追踪器 /** 安全发布底层状态的车辆追踪器 */public class PublishingVehicleTracker {private final Map locations;private final Map unmodifiableMap; public PublishingVehicleTracker(Map locations)
/** 安全发布底层状态的车辆追踪器 */ public class PublishingVehicleTracker { private final Maplocations; private final Map unmodifiableMap; public PublishingVehicleTracker(Map locations) { this.locations = new ConcurrentHashMap (locations); this.unmodifiableMap = Collections.unmodifiableMap(this.locations); } public Map getLocations() { return unmodifiableMap; } public SafePoint getLocation(String id) { return locations.get(id); } public void setLocation(String id, int x, int y) { if (!locations.containsKey(id)) System.out.println("invalid vehicle name : " + id); locations.get(id).set(x, y); } } public class SafePoint { private int x, y; private SafePoint(int[] a) { this(a[0], a[1]); } public SafePoint(SafePoint p) { this(p.get()); } public SafePoint(int x, int y) { this.x = x; this.y = y; } public synchronized int[] get() { return new int[] {x, y}; } public synchronized void set(int x, int y) { this.x = x; this.y = y; } }