1 package au.gov.amsa.ais.message; 2 3 import com.google.common.annotations.VisibleForTesting; 4 import com.google.common.base.Optional; 5 6 import au.gov.amsa.ais.AisExtractor; 7 import au.gov.amsa.ais.AisExtractorFactory; 8 import au.gov.amsa.ais.Util; 9 10 public class AisBStaticDataReportPartA extends AbstractAisBStaticDataReport { 11 12 private final static int MESSAGE_LENGTH = 160; 13 14 private final static String NAME_NOT_AVAILABLE = "@@@@@@@@@@@@@@@@@@@@"; 15 16 private final Optional<String> name; 17 18 public AisBStaticDataReportPartA(String message, int padBits) { 19 this(message, null, padBits); 20 } 21 22 public AisBStaticDataReportPartA(String message, String source, int padBits) { 23 this(Util.getAisExtractorFactory(), message, source, padBits); 24 } 25 26 public AisBStaticDataReportPartA(AisExtractorFactory factory, String message, String source, 27 int padBits) { 28 super(PART_NUMBER_A, 29 factory, 30 source, 31 factory.create(message, MESSAGE_LENGTH, padBits)); 32 33 name = extractName(getExtractor()); 34 } 35 36 @VisibleForTesting 37 static Optional<String> extractName(AisExtractor extractor) { 38 String value = extractor.getString(40, 160); 39 if(NAME_NOT_AVAILABLE.contentEquals(value)) { 40 return Optional.absent(); 41 } else { 42 return Optional.of(value); 43 } 44 } 45 46 public Optional<String> getName() { 47 return name; 48 } 49 50 @Override 51 public String toString() { 52 StringBuilder builder = new StringBuilder(); 53 builder.append("AisPositionBStaticPartA [source="); 54 builder.append(getSource()); 55 builder.append(", messageId="); 56 builder.append(getMessageId()); 57 builder.append(", mmsi="); 58 builder.append(getMmsi()); 59 builder.append(", repeatIndicator="); 60 builder.append(getRepeatIndicator()); 61 builder.append(", partNumber="); 62 builder.append(getPartNumber()); 63 builder.append(", name="); 64 builder.append(getName()); 65 builder.append("]"); 66 return builder.toString(); 67 } 68 }