1 package au.gov.amsa.navigation.ais;
2
3 import static com.google.common.base.Optional.fromNullable;
4 import static com.google.common.base.Optional.of;
5
6 import java.util.concurrent.ConcurrentHashMap;
7
8 import au.gov.amsa.ais.message.AisPositionBExtended;
9 import au.gov.amsa.ais.message.AisShipStaticA;
10 import au.gov.amsa.navigation.Identifier;
11 import au.gov.amsa.navigation.Mmsi;
12 import au.gov.amsa.navigation.ais.Vessel.Builder;
13
14 import com.google.common.base.Optional;
15
16 public class VesselData {
17
18 private ConcurrentHashMap<Identifier, Vessel> map = new ConcurrentHashMap<>();
19
20 public Optional<Vessel> get(Identifier id) {
21 return Optional.fromNullable(map.get(id));
22 }
23
24 public VesselData add(AisShipStaticA m, Optional<String> nmea) {
25 Identifier id = new Mmsi(m.getMmsi());
26
27 while (true) {
28 Optional<Vessel> value = fromNullable(map.get(id));
29 final Optional<Vessel> v;
30 if (!value.isPresent()) {
31 v = of(Vessel.builder().mmsi(m.getMmsi()).build());
32 } else
33 v = value;
34 Builder builder = Vessel.from(v.get());
35 if (m.getImo().isPresent())
36 builder = builder.imo(m.getImo());
37 if (m.getDimensionA().isPresent())
38 builder = builder.dimensionA(m.getDimensionA());
39 if (m.getDimensionB().isPresent())
40 builder = builder.dimensionB(m.getDimensionB());
41 if (m.getDimensionC().isPresent())
42 builder = builder.dimensionC(m.getDimensionC());
43 if (m.getDimensionD().isPresent())
44 builder = builder.dimensionD(m.getDimensionD());
45 if (m.getLengthMetres().isPresent())
46 builder = builder.lengthMetres(m.getLengthMetres());
47 if (nmea.isPresent())
48 builder = builder.nmea(nmea);
49 builder = builder.shipType(of(m.getShipType()));
50 Vessel vessel = builder.build();
51 final boolean inserted;
52 if (value.isPresent())
53 inserted = map.replace(id, v.get(), vessel);
54 else
55 inserted = map.putIfAbsent(id, vessel) == null;
56 if (inserted) {
57 return this;
58 }
59 }
60
61 }
62
63 public VesselData add(AisPositionBExtended message, Optional<String> line) {
64
65 return this;
66 }
67 }