DVL1 dishevelled.org

Skip to: [content] [navigation]

dishevelled.org codegen 1.1-SNAPSHOT :: Downloads | Repository | Javadocs | Checkstyle | Test coverage | Changes

Summary

dishevelled.org codegen is a java source code generation suite based on Velocity [1] templates that generates source code in several different styles. The codegen project requires java version 1.5 or later.

Example

The following example generates the subsequent Java source files. Particular attention is given to proper utilization of typed collections and to reasonable javadoc comments:

ClassDescription foo = new ClassDescription("example", "Foo");
ClassDescription bar = new ClassDescription("example", "Bar");
foo.attribute("String", "Name", Cardinality.ZeroToOne);
bar.attribute("Integer", "Value", Cardinality.StrictlyOne);
foo.associate(bar, Cardinality.ZeroToOne);
bar.associate(foo, Cardinality.ZeroToMany, false, true, false, false);
Codegen.generateSource(foo, Style.Immutable);
Codegen.generateSource(bar, Style.RichlyMutable);

Foo.java:

package example;

/**
 * Foo.
 */
public final class Foo
{
    /** The name for this foo. */
    private final String name;

    /** The bar for this foo. */
    private final Bar bar;


    /**
     * Create a new foo from the specified parameters.
     *
     * @param name name for this foo
     * @param bar bar for this foo
     */
    public Foo(final String name,
               final Bar bar)
    {
        this.name = name;
        this.bar = bar;
    }


    /**
     * Return the name for this foo.
     * The name may be null.
     *
     * @return the name for this foo
     */
    public String getName()
    {
        return name;
    }

    /**
     * Return the bar for this foo.
     * The bar may be null.
     *
     * @return the bar for this foo
     */
    public Bar getBar()
    {
        return bar;
    }
}

Bar.java:

package example;

import java.util.Set;
import java.util.HashSet;
import java.util.Collections;

/**
 * Bar.
 */
public class Bar
{
    /** The value for this bar. */
    private Integer value;

    /** The set of foos for this bar. */
    private final Set<Foo> foos;


    /**
     * Create a new bar from the specified required parameters.
     *
     * @param value value for this bar, must not be null
     */
    public Bar(final Integer value)
    {
        if (value == null)
        {
            throw new IllegalArgumentException("value must not be null");
        }
        this.value = value;
        this.foos = new HashSet<Foo>();
    }

    /**
     * Create a new bar from the specified parameters.
     *
     * <p>The foos in <code>foos</code> are copied defensively
     * into this class.</p>
     *
     * @param value value for this bar, must not be null
     * @param foos set of foos, must not be null
     */
    public Bar(final Integer value,
               final Set<Foo> foos)
    {
        if (value == null)
        {
            throw new IllegalArgumentException("value must not be null");
        }
        if (foos == null)
        {
            throw new IllegalArgumentException("foos must not be null");
        }
        this.value = value;
        this.foos = new HashSet<Foo>(foos);
    }


    /**
     * Return the value for this bar.
     * The value will not be null.
     *
     * @return the value for this bar
     */
    public final Integer getValue()
    {
        return value;
    }

    /**
     * Set the value for this bar to <code>value</code>.
     *
     * @param value value for this bar, must not be null
     */
    public final void setValue(final Integer value)
    {
        if (value == null)
        {
            throw new IllegalArgumentException("value must not be null");
        }
        this.value = value;
    }

    /**
     * Return an unmodifiable set of foos
     * for this bar.  The returned set may be
     * empty but will not be null.
     *
     * @return an unmodifiable set of foos
     *    for this bar
     */
    public final Set<Foo> getFoos()
    {
        return Collections.unmodifiableSet(foos);
    }

    /**
     * Add the specified foo to the set of
     * foos for this bar.  An exception
     * may be thrown if the underlying set prevents
     * <code>foo</code> from being added.
     *
     * @param foo foo to add
     */
    public final void addFoo(final Foo foo)
    {
        foos.add(foo);
    }

    /**
     * Add all of the foos in the specified collection of foos
     * to the set of foos for this bar.
     * An exception may be thrown if the underlying
     * set prevents any of the foos in
     * <code>foos</code> from being added.
     *
     * @param foos collection of foos to add
     */
    public final void addAllFoos(final Collection<? extends Foo> foos)
    {
        this.foos.addAll(foos);
    }

    /**
     * Remove the specified foo from the set of
     * foos for this bar.  An exception
     * may be thrown if the underlying set prevents
     * <code>foo</code> from being removed.
     *
     * @param foo foo to remove
     */
    public final void removeFoo(final Foo foo)
    {
        foos.remove(foo);
    }

    /**
     * Remove all of the foos in the set of 
     * foos for this bar that are also contained in the
     * specified collection of foos.  An exception
     * may be thrown if the underlying set prevents any
     * of the foos in <code>foos</code> from being removed.
     *
     * @param foos collection of foos to remove
     */
    public final void removeAllFoos(final Collection<? extends Foo> foos)
    {
        this.foos.removeAll(foos);
    }

    /**
     * Retain only the foos in the set of
     * foos for this bar that are contained in the specified
     * collection of foos.  An exception may be thrown
     * if the underlying set prevents any of the foos
     * not in <code>foos</code> from being removed.
     *
     * @param foos collection of foos to retain
     */
    public final void retainAllFoos(final Collection<? extends Foo> foos)
    {
        this.foos.retainAll(foos);
    }

    /**
     * Remove all of the foos in the set of
     * foos for this bar.  An exception may
     * be thrown if the underlying set prevents any of the
     * foos from being removed.
     */
    public final void clearFoos()
    {
        foos.clear();
    }

    /** {@inheritDoc} */
    public final boolean equals(final Object o)
    {
        if (o == this)
        {
            return true;
        }
        if (!(o instanceof Bar))
        {
            return false;
        }

        Bar bar = (Bar) o;

        return (true
            && (value.equals(bar.getValue()))
            && (foos.equals(bar.getFoos()))
        );
    }

    /** {@inheritDoc} */
    public final int hashCode()
    {
        int result = 17;
        result = 37 * result + ((value == null) ? 0 : value.hashCode());
        result = 37 * result + foos.hashCode();
        return result;
    }
}

For further details on source code styles, please see the javadoc for
> Style

or the Velocity templates themselves
> src/main/resources/org/dishevelled/codegen

References

[1] Apache Jakarta Velocity
> http://jakarta.apache.org/velocity

Acknowledgements

SourceForge.net Logo