четверг, 14 мая 2009 г.

Сериализация immutable объектов

Понадобилось создать сериализацию/десереализацию immutable объектов. Вот что получилось. Есть класс в который всё сериализуется - Stream, и небольшая иерархия классов, которая включает в себя абстрактные классы.

import java.io.IOException;

/**
 * @author Alexander
 */
public class Stream {

  public Stream() {

  }

  public interface Streamable {
  public Stream writeObject();
  }

  public void writeString(final String key, final String value) {

  }

  public void writeInteger(final String key, final int value) {

  }

  public void writeLong(final String key, final long value) {

  }

  public String readString(final String key) {
  return "";
  }

  public int readInteger(final String key) {
  return 0;
  }

  public long readLong(final String key) {
  return 0;
  }

  public void saveToXML(final String filename) throws IOException {

  }

  public void saveToProperties(final String filename) throws IOException {

  }

  public void saveToBinary(final String filename) throws IOException {

  }

  public void loadFromXML(final String filename) throws IOException {

  }

  public void loadFromProperties(final String filename) throws IOException {

  }

  public void loadFromBinary(final String filename) throws IOException {

  }
}

/**
 * @author Alexander
 */
public abstract class BaseAbstract implements Stream.Streamable {

  private final String name;

  protected BaseAbstract(final String name) {
  validate(name);

  this.name = name;
  }

  protected BaseAbstract(final Stream stream) {
  if (null == stream) {
  throw new IllegalArgumentException("stream cannot be null");
  }

  final String name = stream.readString("name");

  validate(name);

  this.name = name;
  }

  private void validate(final String name) throws IllegalArgumentException {
  if (null == name) {
  throw new IllegalArgumentException("name cannot be null");
  }
  }

  public Stream writeObject() {
  Stream stream = new Stream();
  stream.writeString("name", name);
  return stream;
  }

  public String getName() {
  return name;
  }
}

/**
 * @author Alexander
 */
public class MiddleAbstract extends BaseAbstract {

  private final long x;

  protected MiddleAbstract(final String name, final long x) {
  super(name);

  validate(x);

  this.x = x;
  }

  protected MiddleAbstract(final Stream stream) {
  super(stream);

  final long x = stream.readLong("x");

  validate(x);

  this.x = x;
  }

  private void validate(final long x) throws IllegalArgumentException {
  if (x < 0) {
  throw new IllegalArgumentException("x cannot be less than zero");
  }
  }

  @Override
  public Stream writeObject() {
  Stream stream = super.writeObject();
  stream.writeLong("x", x);
  return stream;
  }

  public long getX() {
  return x;
  }
}

/**
 * @author Alexander D. Shalugin
 */
public final class Child extends MiddleAbstract {

  private final int v;

  public Child(final String name, final long x, final int v) {
  super(name, x);

  validate(v);

  this.v = v;
  }

  public Child(Stream stream) {
  super(stream);

  final int v = stream.readInteger("v");

  validate(v);

  this.v = v;
  }

  private void validate(final int v) throws IllegalArgumentException {
  if (v == 0) {
  throw new IllegalArgumentException("v cannot be zero");
  }
  }

  public int getV() {
  return v;
  }
}

Буду рад любым комментариям.

Комментариев нет: