com.iab.gpp.encoder.section.AbstractLazilyEncodableSection Maven / Gradle / Ivy
The newest version!
package com.iab.gpp.encoder.section;
import java.util.List;
import com.iab.gpp.encoder.error.InvalidFieldException;
import com.iab.gpp.encoder.segment.EncodableSegment;
public abstract class AbstractLazilyEncodableSection implements EncodableSection {
private List segments;
private String encodedString = null;
private boolean dirty = false;
private boolean decoded = true;
public AbstractLazilyEncodableSection() {
this.segments = initializeSegments();
}
protected abstract List initializeSegments();
protected abstract String encodeSection(List segments);
protected abstract List decodeSection(String encodedString);
public boolean hasField(String fieldName) {
if (!this.decoded) {
this.segments = this.decodeSection(this.encodedString);
this.dirty = false;
this.decoded = true;
}
for(EncodableSegment segment : segments) {
if(segment.getFieldNames().contains(fieldName)) {
return segment.hasField(fieldName);
}
}
return false;
}
public Object getFieldValue(String fieldName) {
if (!this.decoded) {
this.segments = this.decodeSection(this.encodedString);
this.dirty = false;
this.decoded = true;
}
for(EncodableSegment segment : segments) {
if(segment.hasField(fieldName)) {
return segment.getFieldValue(fieldName);
}
}
throw new InvalidFieldException("Invalid field: '" + fieldName + "'");
}
public void setFieldValue(String fieldName, Object value) {
if (!this.decoded) {
this.segments = this.decodeSection(this.encodedString);
this.dirty = false;
this.decoded = true;
}
for(EncodableSegment segment : segments) {
if(segment.hasField(fieldName)) {
segment.setFieldValue(fieldName, value);
return;
}
}
throw new InvalidFieldException("Invalid field: '" + fieldName + "'");
}
public String encode() {
if (this.encodedString == null || this.encodedString.isEmpty() || this.dirty) {
this.encodedString = this.encodeSection(this.segments);
this.dirty = false;
this.decoded = true;
}
return this.encodedString;
}
public void decode(String encodedString) {
this.encodedString = encodedString;
this.dirty = false;
this.decoded = false;
}
}