View Javadoc
1   package au.gov.amsa.ais;
2   
3   import java.math.BigInteger;
4   
5   import com.google.common.annotations.VisibleForTesting;
6   
7   /**
8    * Decodes the ship type code as per 1371-4.pdf.
9    * 
10   * @author dxm
11   * 
12   */
13  public final class ShipTypeDecoder {
14  
15      /**
16       * Private constructor.
17       */
18      private ShipTypeDecoder() {
19          // private constructor.
20      }
21  
22      /**
23       * Visible for test coverage only (calls the private constructor).
24       */
25      @VisibleForTesting
26      static void forTestCoverageOnly() {
27          new ShipTypeDecoder();
28      }
29  
30      private static String[] vesselTypes = { "Fishing", "Towing", "Towing Long/Large",
31              "Engaged in dredging or underwater operations", "Engaged in diving operations",
32              "Engaged in military operations", "Sailing", "Pleasure craft", "Reserved", "Reserved" };
33      private static String[] specialTypes = { "Pilot vessel", "SAR", "Tug", "Port tender",
34              "Vessel with anti-pollution facilities or equipment", "Law enforcement", "Local 56",
35              "Local 57", "Medical transport", "Ship according to RR Resolution No. 18 (Mob-83)" };
36      private static String[] categoryTypes = { "Unknown", "Reserved", "WIG", "Vessel", "HSC",
37              "Special", "Passenger ship", "Cargo ship", "Tanker", "Other" };
38      private static String[] cargoTypes = { "All",
39              "Carrying DG, HS, or MP, IMO Hazard or pollutant category A",
40              "Carrying DG, HS, or MP, IMO Hazard or pollutant category B",
41              "Carrying DG, HS, or MP, IMO Hazard or pollutant category C",
42              "Carrying DG, HS, or MP, IMO Hazard or pollutant category D", "Reserved 5",
43              "Reserved 6", "Reserved 7", "Reserved 8", "No additional info" };
44  
45      /**
46       * Returns the ship type for given integer. If integer is null then returns
47       * null.
48       * 
49       * @param ts
50       * @return
51       */
52      public static String getShipType(BigInteger ts) {
53          if (ts == null)
54              return null;
55          else
56              return getShipType(ts.intValue());
57      }
58  
59      /**
60       * Returns the ship type for given integer. If integer is null then returns
61       * null.
62       * 
63       * @param ts
64       * @return
65       */
66      public static String getShipType(Integer ts) {
67          if (ts == null)
68              return null;
69          if (ts < 10 || ts > 99) {
70              return "unknown code " + ts.intValue();
71          } else {
72              int a = ts / 10;
73              int b = ts % 10;
74              if (a == 3) {
75                  return vesselTypes[b];
76              } else if (a == 5) {
77                  return specialTypes[b];
78              } else {
79                  return categoryTypes[a] + " - " + cargoTypes[b];
80              }
81          }
82      }
83  
84      public static void main(String[] args) {
85          for (int i = 10; i <= 99; i++) {
86              System.out.println(i + "," + getShipType(i));
87          }
88      }
89  
90  }