Summary
dishevelled.org codegen-shell is a runnable shell for the codegen library. The shell is an instance of the BeanShell [1] Interpreter [2] with the codegen library included in the classpath and the packages from the codegen library imported. The codegen-shell project, the shell itself, and generated Java source files all require java version 1.5 or later.
Example
The following example, as run from a command line, generates the subsequent Java source files. Particular attention is given to proper utilization of typed collections and to reasonable javadoc comments:
$ java -jar dsh-codegen-shell.jar
Shell interface to org.dishevelled.codegen.Codegen
BeanShell 2.0b1.1 - by Pat Niemeyer (pat@pat.net)
bsh % ClassDescription foo = new ClassDescription("example", "Foo");
bsh % ClassDescription bar = new ClassDescription("example", "Bar");
bsh % foo.attribute("String", "Name", Cardinality.ZeroToOne);
bsh % bar.attribute("Integer", "Value", Cardinality.StrictlyOne);
bsh % foo.associate(bar, Cardinality.ZeroToOne);
bsh % bar.associate(foo, Cardinality.ZeroToMany, false, true, false, false);
bsh % generateSource(foo, Style.Immutable);
bsh % 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");
}
setValue(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");
}
setValue(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 the codegen API documentation and further details on source code styles, please see the dishevelled.org codegen project.
References
[1] BeanShell - Lightweight Scripting for Java
> http://www.beanshell.org
[2] class bsh.Interpreter
> http://www.beanshell.org/javadoc/bsh/Interpreter.html