Skip to content

Commit

Permalink
Fix code style for code generated by velocity templates
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 5, 2023
1 parent e08c95a commit 7ec72d5
Show file tree
Hide file tree
Showing 9 changed files with 910 additions and 1,403 deletions.
265 changes: 102 additions & 163 deletions src/mdo/merger.vm

Large diffs are not rendered by default.

181 changes: 77 additions & 104 deletions src/mdo/model-v3.vm
Original file line number Diff line number Diff line change
Expand Up @@ -106,58 +106,48 @@ public class ${class.name}
#end
{

public ${class.name}()
{
this( ${packageModelV4}.${class.name}.newInstance() );
public ${class.name}() {
this(${packageModelV4}.${class.name}.newInstance());
}

public ${class.name}( ${packageModelV4}.${class.name} delegate )
{
this( delegate, null );
public ${class.name}(${packageModelV4}.${class.name} delegate) {
this(delegate, null);
}

public ${class.name}( ${packageModelV4}.${class.name} delegate, BaseObject parent )
{
super( delegate, parent );
public ${class.name}(${packageModelV4}.${class.name} delegate, BaseObject parent) {
super(delegate, parent);
}

public ${class.name} clone()
{
return new ${class.name}( getDelegate() );
public ${class.name} clone(){
return new ${class.name}(getDelegate());
}

#if ( $class.superClass )
@Override
#end
public ${packageModelV4}.${class.name} getDelegate()
{
return ( ${packageModelV4}.${class.name} ) super.getDelegate();
public ${packageModelV4}.${class.name} getDelegate() {
return (${packageModelV4}.${class.name}) super.getDelegate();
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ( o == null || !( o instanceof ${class.name} ) )
{
if (o == null || !(o instanceof ${class.name})) {
return false;
}
${class.name} that = ( ${class.name} ) o;
return Objects.equals( this.delegate, that.delegate );
${class.name} that = (${class.name}) o;
return Objects.equals(this.delegate, that.delegate);
}

@Override
public int hashCode()
{
public int hashCode() {
return getDelegate().hashCode();
}

#if ( $class == $root )
public String getModelEncoding()
{
public String getModelEncoding() {
return getDelegate().getModelEncoding();
}

Expand All @@ -173,52 +163,47 @@ public class ${class.name}
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" )
@Nonnull
#end
public ${type} ${pfx}${cap}()
{
public ${type} ${pfx}${cap}() {
#if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
return new WrapperList<${field.to}, ${packageModelV4}.${field.to}>(
() -> getDelegate().get${cap}(), l -> update( getDelegate().with${cap}( l ) ),
d -> new ${field.to}( d, this ), ${field.to}::getDelegate );
() -> getDelegate().get${cap}(), l -> update(getDelegate().with${cap}(l)),
d -> new ${field.to}(d, this), ${field.to}::getDelegate);
#elseif ( $field.to == "String" && $field.type == "java.util.Properties" && $field.multiplicity == "*" )
return new WrapperProperties( () -> getDelegate().get${cap}(), this::set${cap} );
return new WrapperProperties(() -> getDelegate().get${cap}(), this::set${cap});
#elseif ( $field.to == "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
return new WrapperList<String, ${field.to}>( () -> getDelegate().get${cap}(), this::set${cap}, s -> s, s -> s );
return new WrapperList<String, ${field.to}>(() -> getDelegate().get${cap}(), this::set${cap}, s -> s, s -> s);
#elseif ( $field.to )
return getDelegate().${pfx}${cap}() != null ? new ${field.to}( getDelegate().${pfx}${cap}(), this ) : null;
return getDelegate().${pfx}${cap}() != null ? new ${field.to}(getDelegate().${pfx}${cap}(), this) : null;
#elseif ( $field.type == "DOM" )
return getDelegate().${pfx}${cap}() != null ? new Xpp3Dom( getDelegate().${pfx}${cap}(), this::replace ) : null;
return getDelegate().${pfx}${cap}() != null ? new Xpp3Dom(getDelegate().${pfx}${cap}(), this::replace) : null;
#else
return getDelegate().${pfx}${cap}();
#end
}

public void set${cap}( ${type} ${field.name} )
{
#if ( $field.type == "DOM" )
if ( ! Objects.equals( ( ( Xpp3Dom ) ${field.name} ).getDom(), getDelegate().${pfx}${cap}() ) )
{
update( getDelegate().with${cap}( ( ( Xpp3Dom ) ${field.name} ).getDom() ) );
( ( Xpp3Dom ) ${field.name} ).setChildrenTracking( this::replace );
public void set${cap}(${type} ${field.name}) {
#if ($field.type == "DOM")
if (!Objects.equals(((Xpp3Dom) ${field.name}).getDom(), getDelegate().${pfx}${cap}())) {
update(getDelegate().with${cap}(((Xpp3Dom) ${field.name}).getDom()));
((Xpp3Dom) ${field.name}).setChildrenTracking(this::replace);
}
#elseif( $field.type == "java.util.Properties" )
Map<String, String> map = ${field.name}.entrySet().stream()
.collect( Collectors.toMap( e -> e.getKey().toString(), e -> e.getValue().toString() ) );
if ( !Objects.equals( map, getDelegate().get${cap}() ) )
{
update( getDelegate().with${cap}( map ) );
.collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
if (!Objects.equals(map, getDelegate().get${cap}())) {
update(getDelegate().with${cap}(map));
}
#else
if ( !Objects.equals( ${field.name}, getDelegate().${pfx}${cap}() ) )
{
if (!Objects.equals(${field.name}, getDelegate().${pfx}${cap}())) {
#if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
update( getDelegate().with${cap}(
${field.name}.stream().map( c -> c.getDelegate() ).collect( Collectors.toList() ) ) );
${field.name}.forEach( e -> e.childrenTracking = this::replace );
update(getDelegate().with${cap}(
${field.name}.stream().map(c -> c.getDelegate()).collect(Collectors.toList())));
${field.name}.forEach(e -> e.childrenTracking = this::replace);
#elseif ( $field.to && $field.to != "String" )
update( getDelegate().with${cap}( ${field.name}.getDelegate() ) );
update(getDelegate().with${cap}(${field.name}.getDelegate()));
${field.name}.childrenTracking = this::replace;
#else
update( getDelegate().with${cap}( ${field.name} ) );
update(getDelegate().with${cap}(${field.name}));
#end
}
#end
Expand All @@ -227,101 +212,89 @@ public class ${class.name}
#if ( $field.type == "java.util.List" && $field.multiplicity == "*" )
#set ( $v = $Helper.singular( ${field.name} ) )
#set ( $scap = $Helper.capitalise( $v ) )
public void add${scap}( ${field.to} ${v} )
{
public void add${scap}(${field.to} ${v}) {
#if ( $field.to == "String" )
update( getDelegate().with${cap}(
Stream.concat( getDelegate().get${cap}().stream(), Stream.of( ${v} ) )
.collect( Collectors.toList() ) ) );
update(getDelegate().with${cap}(
Stream.concat(getDelegate().get${cap}().stream(), Stream.of(${v}))
.collect(Collectors.toList())));
#else
update( getDelegate().with${cap}(
Stream.concat( getDelegate().get${cap}().stream(), Stream.of( ${v}.getDelegate() ) )
.collect( Collectors.toList() ) ) );
update(getDelegate().with${cap}(
Stream.concat(getDelegate().get${cap}().stream(), Stream.of(${v}.getDelegate()))
.collect(Collectors.toList())));
${v}.childrenTracking = this::replace;
#end
}

public void remove${scap}( ${field.to} ${v} )
{
public void remove${scap}(${field.to} ${v}) {
#if ( $field.to == "String" )
update( getDelegate().with${cap}(
getDelegate().get${cap}().stream()
.filter( e -> !Objects.equals( e, ${v} ) )
.collect( Collectors.toList() ) ) );
update(getDelegate().with${cap}(
getDelegate().get${cap}().stream()
.filter(e -> !Objects.equals(e, ${v}))
.collect(Collectors.toList())));
#else
update( getDelegate().with${cap}(
getDelegate().get${cap}().stream()
.filter( e -> !Objects.equals( e, ${v} ) )
.collect( Collectors.toList() ) ) );
update(getDelegate().with${cap}(
getDelegate().get${cap}().stream()
.filter(e -> !Objects.equals(e, ${v}))
.collect(Collectors.toList())));
${v}.childrenTracking = null;
#end
}

#elseif ( $field.type == "java.util.Properties" && $field.multiplicity == "*" )
#set ( $v = $Helper.singular( ${field.name} ) )
#set ( $scap = $Helper.capitalise( $v ) )
public void add${scap}( String key, String value )
{
get${cap}().put( key, value );
public void add${scap}(String key, String value) {
get${cap}().put(key, value);
}

#end
#end
#if ( $locationTracking )
public InputLocation getLocation( Object key )
{
${packageModelV4}.InputLocation loc = getDelegate().getLocation( key );
return loc != null ? new InputLocation( loc ) : null;
public InputLocation getLocation(Object key) {
${packageModelV4}.InputLocation loc = getDelegate().getLocation(key);
return loc != null ? new InputLocation(loc) : null;
}

public void setLocation( Object key, InputLocation location )
{
update( ${packageModelV4}.${class.name}.newBuilder( getDelegate(), true )
.location( key, location.toApiLocation() ).build() );
public void setLocation(Object key, InputLocation location) {
update(${packageModelV4}.${class.name}.newBuilder(getDelegate(), true)
.location(key, location.toApiLocation()).build());
}

#end
protected boolean replace( Object oldDelegate, Object newDelegate )
{
if ( super.replace( oldDelegate, newDelegate ) )
{
protected boolean replace(Object oldDelegate, Object newDelegate) {
if (super.replace(oldDelegate, newDelegate)) {
return true;
}
#foreach ( $field in $class.getFields($version) )
#set ( $cap = $Helper.capitalise( $field.name ) )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $field.to && $field.multiplicity != "*" )
if ( oldDelegate == getDelegate().get${cap}() )
{
update( getDelegate().with${cap}( ( ${packageModelV4}.${field.to} ) newDelegate ) );
if (oldDelegate == getDelegate().get${cap}()) {
update(getDelegate().with${cap}((${packageModelV4}.${field.to}) newDelegate));
return true;
}
#elseif ( $field.type == "java.util.List" && $field.to != "String" )
if ( getDelegate().get${cap}().contains( oldDelegate ) )
{
List<${packageModelV4}.${field.to}> list = new ArrayList<>( getDelegate().get${cap}() );
list.replaceAll( d -> d == oldDelegate ? ( ${packageModelV4}.${field.to} ) newDelegate : d );
update( getDelegate().with${cap}( list ) );
#elseif ($field.type == "java.util.List" && $field.to != "String")
if (getDelegate().get${cap}().contains(oldDelegate)) {
List<${packageModelV4}.${field.to}> list = new ArrayList<>(getDelegate().get${cap}());
list.replaceAll(d -> d == oldDelegate ? (${packageModelV4}.${field.to}) newDelegate : d);
update(getDelegate().with${cap}(list));
return true;
}
#elseif ( $field.type == "DOM" )
if ( getDelegate().get${cap}() == oldDelegate )
{
update( getDelegate().with${cap}( ( org.apache.maven.api.xml.XmlNode ) newDelegate ) );
if (getDelegate().get${cap}() == oldDelegate) {
update(getDelegate().with${cap}((org.apache.maven.api.xml.XmlNode) newDelegate));
}
#end
#end
return false;
}

public static List<${packageModelV4}.${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV4( List<${class.name}> list )
{
return list != null ? new WrapperList<>( list, ${class.name}::getDelegate, ${class.name}::new ) : null;
public static List<${packageModelV4}.${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV4(List<${class.name}> list) {
return list != null ? new WrapperList<>(list, ${class.name}::getDelegate, ${class.name}::new) : null;
}

public static List<${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV3( List<${packageModelV4}.${class.name}> list )
{
return list != null ? new WrapperList<>( list, ${class.name}::new, ${class.name}::getDelegate ) : null;
public static List<${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV3(List<${packageModelV4}.${class.name}> list) {
return list != null ? new WrapperList<>(list, ${class.name}::new, ${class.name}::getDelegate) : null;
}

#foreach ( $cs in $class.getCodeSegments($version) )
Expand Down
Loading

0 comments on commit 7ec72d5

Please sign in to comment.