All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iab.gpp.encoder.segment.AbstractLazilyEncodableSegment Maven / Gradle / Ivy

package com.iab.gpp.encoder.segment;

import com.iab.gpp.encoder.error.InvalidFieldException;
import com.iab.gpp.encoder.field.Fields;

public abstract class AbstractLazilyEncodableSegment> implements EncodableSegment {

  protected T fields;

  private String encodedString = null;

  private boolean dirty = false;
  private boolean decoded = true;

  public AbstractLazilyEncodableSegment() {
    this.fields = initializeFields();
  }

  protected abstract T initializeFields();

  protected abstract String encodeSegment(T fields);

  protected abstract void decodeSegment(String encodedString, T Fields);

  public boolean hasField(String fieldName) {
    return this.fields.containsKey(fieldName);
  }

  public Object getFieldValue(String fieldName) {
    if (!this.decoded) {
      this.decodeSegment(this.encodedString, this.fields);
      this.dirty = false;
      this.decoded = true;
    }

    if (this.fields.containsKey(fieldName)) {
      return this.fields.get(fieldName).getValue();
    } else {
      throw new InvalidFieldException("Invalid field: '" + fieldName + "'");
    }
  }

  public void setFieldValue(String fieldName, Object value) {
    if (!this.decoded) {
      this.decodeSegment(this.encodedString, this.fields);
      this.dirty = false;
      this.decoded = true;
    }

    if (this.fields.containsKey(fieldName)) {
      this.fields.get(fieldName).setValue(value);
      this.dirty = true;
    } else {
      throw new InvalidFieldException(fieldName + " not found");
    }
  }

  public String encode() {
    if (this.encodedString == null || this.encodedString.isEmpty() || this.dirty) {
      this.validate();
      this.encodedString = encodeSegment(this.fields);
      this.dirty = false;
      this.decoded = true;
    }

    return this.encodedString;
  }

  public void decode(String encodedString) {
    this.encodedString = encodedString;
    this.dirty = false;
    this.decoded = false;
  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy