I've just uploaded this to svn, package org.apache.river.api.io

This will blow your mind, how's this for simple mistake free alternative to Serialization?

3 examples of different construction methods are demonstrated:

  1. Constructor
  2. Static factory method
  3. Builder

All creation methods are public, fields are private and final, internal class state is not exposed and is free to evolve separately all invarients are checked during construction objects safely published on deserialisation.

You can even replace your objects with alternatives that use completely different classes, provided referencing fields are declared to use a common superclass or interface.

Serial form is fixed and it uses Externalizable, it's faster than serialization, there's no serialVersionUID required, just one little method to implement.

We now have the ability to create immutable distributed value objects that are thread safe and can be evolved though public api.

It took two nights to code and it worked first time!

This is a game changer, plug this into JERI and we've got a perfect compliment to reflective proxy's.

Penny for your thoughts?

Peter.

package tests.support;

import java.io.Serializable;
import org.apache.river.api.io.Distributed;
import org.apache.river.api.io.SerialFactory;

/**
 *
 * @author peter
 */
public class DistributedObject implements Distributed {

    public static DistributedObject create(String str){
        return new DistributedObject(str);
    }

    private final String testString;
    /* 0 - constructor
     * 1 - static factory method
     * 2 - builder
     */
    private final int method;

    public DistributedObject(String str){
        testString = str;
        method = 0;
    }

    public DistributedObject(String str, int method){
        testString = str;
        this.method = method;
    }

    @Override
    public SerialFactory substitute() {
        Class[] signature = new Class[1];
        Object[] parameters = new Object[1];
        parameters[0] = testString;
        if (method == 0){
            signature[0] = String.class;
return new SerialFactory(this.getClass(), null, signature, parameters );
        }
        if (method == 1){
            signature[0] = String.class;
return new SerialFactory(this.getClass(), "create", signature, parameters);
        }
        if (method == 2){
            Builder builder = new Builder().setString(testString);
            return new SerialFactory(builder, "build", null, null);
        }
        return null;
    }

    public String toString(){
        return testString;
    }

    public static class Builder implements Serializable {
        private static final long serialVersionUID = 1L;

        private String str;

        public Builder(){

        }

        public Builder setString(String str){
            this.str = str;
            return this;
        }

        public DistributedObject build(){
            return new DistributedObject(str);
        }
    }

}

Reply via email to