FileDocCategorySizeDatePackage
Person.javaAPI DocExample2513Mon Feb 13 12:52:36 GMT 2006None

Person

public class Person extends Object implements Serializable, ObjectInputValidation

Fields Summary
static Map
thePeople
private String
name
private String
ss
Constructors Summary
public Person(String name, String ss)


       
    this.name = name;
    this.ss = ss;
    thePeople.put(ss, name);
  
Methods Summary
public static voidmain(java.lang.String[] args)

  
    Person p1 = new Person("Rusty", "123-45-5678");
    Person p2 = new Person("Beth",  "321-45-5678");
    Person p3 = new Person("David", "453-45-5678");
    Person p4 = new Person("David", "453-45-5678");
  
    Iterator iterator = thePeople.values().iterator();
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }
    
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(p1);
    oout.writeObject(p2);
    oout.writeObject(p3);
    oout.writeObject(p4);
    oout.flush();
    oout.close();
    
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream oin = new ObjectInputStream(bin);
    try {
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
    }
    catch (InvalidObjectException ex) {
      System.err.println(ex);
    }
    oin.close();

    // now empty the map and try again
    thePeople.clear();
    bin = new ByteArrayInputStream(bout.toByteArray());
    oin = new ObjectInputStream(bin);
    try {
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
      System.out.println(oin.readObject());
    }
    catch (InvalidObjectException ex) {
      System.err.println(ex);
    }
    oin.close();
    
    iterator = thePeople.values().iterator();
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }   
  
private voidreadObject(java.io.ObjectInputStream in)

    in.registerValidation(this, 5);
    in.defaultReadObject();
  
public java.lang.StringtoString()

    return this.name + "\t" + this.ss;
  
public voidvalidateObject()

    if (thePeople.containsKey(this.ss)) {
      throw new InvalidObjectException(this.name + " already exists");
    }
    else {
      thePeople.put(this.ss, this.name);
    }