Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Magic with Merlin: Long-term persistence

Serialize JavaBean component state to XML

Return to article


Listing 1. Sample class definition

package net.zukowski.ibm;

import java.awt.Point;

public class Sample {
  private int[] scores;
  private String name;
  private Point seat;

  public void setScores(int[] value) {
    scores = value;
  }
  public void setScores(int value, int position) {
    scores[position] = value;
  }
  public int[] getScores() {
    return scores;
  }
  public int getScores(int position) {
    return scores[position];
  }

  public float getAverage() {
    float sum = 0;
    int count = scores.length;
    float avg;
    if (count == 0) {
      avg = -1;
    } else {
      for (int i = 0; i < count; i++) {
        sum += scores[i];
      }
      avg = sum / count;
    }
    return avg;
  }

  public void setName(String value) {
    name = value;
  }
  public String getName() {
    return name;
  }

  public void setSeat(Point value) {
    seat = value;
  }
  public Point getSeat() {
    return seat;
  }

  public String toString() {
    return getClass().getName() + 
      "[scores=" + asString(scores) + 
      ",avg=" + getAverage() +
      ",name=" + name + 
      ",seat=" + seat + "]";
  }
  private String asString(int[] array) {
    StringBuffer buffer = new StringBuffer("[");
    for (int i=0, n=array.length; i < n; i++) {
      if (i != 0) {
        buffer.append(",");
      }
      buffer.append(array[i]);
    }
    buffer.append("]");
    return buffer.toString();
  }
}

Return to article