Identity.java [src/config/nodes] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package config.nodes;

import java.util.Objects;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import static utils.Constants.*;

/**
 *
 * @author Brad
 */
public class Identity {
  private String code;
  private int id;

  public Identity() {

  }

  public Identity(String code, int id) {
    this.code = code;
    this.id = id;
  }

  public String getCode() {
    return code;
  }

  public int getId() {
    return id;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public void setId(int id) {
    this.id = id;
  }

  public void parse(XMLEventReader reader) throws XMLStreamException {
    XMLEvent event;
    boolean done = false;
    StartElement startElement;
    EndElement endElement;

    while (reader.hasNext() && !done) {
      event = reader.nextEvent();

      switch (event.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
          startElement = event.asStartElement();
          if (startElement.getName().getLocalPart().equals(CODE))
            code = reader.getElementText();
          if (startElement.getName().getLocalPart().equals(ID))
            id = Integer.parseInt(reader.getElementText());
          break;
        case XMLStreamConstants.END_ELEMENT:
          endElement = event.asEndElement();
          if (endElement.getName().getLocalPart().equals(IDENTITY))
            done = true;
          break;

      }
    }
  }

  @Override
  public String toString() {
    return code + " " + String.format("%02d", id);
  }

  @Override
  public boolean equals(Object o) {
    if (o == null)
      return false;
    if (o == this)
      return true;
    if (!(o instanceof Identity))
      return false;
    Identity id = ( Identity ) o;

    return this.code.equals(id.code) && this.id == id.id;
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 53 * hash + Objects.hashCode(this.code);
    hash = 53 * hash + this.id;
    return hash;
  }
}