com.iab.gpp.encoder.segment.UsCaGpcSegment Maven / Gradle / Ivy
The newest version!
package com.iab.gpp.encoder.segment;
import java.util.List;
import com.iab.gpp.encoder.base64.AbstractBase64UrlEncoder;
import com.iab.gpp.encoder.base64.CompressedBase64UrlEncoder;
import com.iab.gpp.encoder.bitstring.BitStringEncoder;
import com.iab.gpp.encoder.datatype.EncodableBoolean;
import com.iab.gpp.encoder.datatype.EncodableFixedInteger;
import com.iab.gpp.encoder.error.DecodingException;
import com.iab.gpp.encoder.field.EncodableBitStringFields;
import com.iab.gpp.encoder.field.UsCaField;
public class UsCaGpcSegment extends AbstractLazilyEncodableSegment {
private AbstractBase64UrlEncoder base64UrlEncoder = CompressedBase64UrlEncoder.getInstance();
private BitStringEncoder bitStringEncoder = BitStringEncoder.getInstance();
public UsCaGpcSegment() {
super();
}
public UsCaGpcSegment(String encodedString) {
super();
this.decode(encodedString);
}
@Override
public List getFieldNames() {
return UsCaField.USCA_GPC_SEGMENT_FIELD_NAMES;
}
@Override
protected EncodableBitStringFields initializeFields() {
EncodableBitStringFields fields = new EncodableBitStringFields();
fields.put(UsCaField.GPC_SEGMENT_TYPE, new EncodableFixedInteger(2, 1));
fields.put(UsCaField.GPC_SEGMENT_INCLUDED, new EncodableBoolean(true));
fields.put(UsCaField.GPC, new EncodableBoolean(false));
return fields;
}
@Override
protected String encodeSegment(EncodableBitStringFields fields) {
String bitString = bitStringEncoder.encode(fields, getFieldNames());
String encodedString = base64UrlEncoder.encode(bitString);
return encodedString;
}
@Override
protected void decodeSegment(String encodedString, EncodableBitStringFields fields) {
if(encodedString == null || encodedString.isEmpty()) {
this.fields.reset(fields);
}
try {
String bitString = base64UrlEncoder.decode(encodedString);
bitStringEncoder.decode(bitString, getFieldNames(), fields);
} catch (Exception e) {
throw new DecodingException("Unable to decode UsCaGpcSegment '" + encodedString + "'", e);
}
}
}