Skip to content

Commit

Permalink
Some generifiable cleansing
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed May 16, 2017
1 parent ed9c092 commit c0bfb26
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
*/
package com.helger.jcodemodel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;
Expand All @@ -53,7 +50,7 @@
*
* @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public abstract class AbstractJGenerifiableImpl implements IJGenerifiable, IJDeclaration, IJOwned
public abstract class AbstractJGenerifiableImpl implements IJGenerifiable
{
/**
* Lazily created list of {@link JTypeVar}s.
Expand Down Expand Up @@ -89,31 +86,11 @@ public JTypeVar generify (@Nonnull final String name)
return v;
}

@Nonnull
public JTypeVar generify (@Nonnull final String name, @Nonnull final Class <?> _extends)
{
return generify (name, owner ().ref (_extends));
}

@Nonnull
public JTypeVar generify (@Nonnull final String name, @Nonnull final AbstractJClass _extends)
{
return generify (name).bound (_extends);
}

@Nonnull
public JTypeVar [] typeParams ()
{
if (m_aTypeVariables == null)
return AbstractJClass.EMPTY_ARRAY;
return m_aTypeVariables.values ().toArray (new JTypeVar [m_aTypeVariables.size ()]);
}

@Nonnull
public List <JTypeVar> typeParamList ()
{
if (m_aTypeVariables == null)
return Collections.<JTypeVar> emptyList ();
return new ArrayList <> (m_aTypeVariables.values ());
}
}
26 changes: 11 additions & 15 deletions src/main/java/com/helger/jcodemodel/IJGenerifiable.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
*/
package com.helger.jcodemodel;

import java.util.List;

import javax.annotation.Nonnull;

/**
Expand All @@ -50,7 +48,7 @@
*
* @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public interface IJGenerifiable
public interface IJGenerifiable extends IJDeclaration, IJOwned
{
/**
* Adds a new type variable to this declaration.
Expand All @@ -67,24 +65,30 @@ public interface IJGenerifiable
*
* @param name
* type variable name
* @param bound
* @param _extends
* Bound class
* @return The created {@link JTypeVar}
*/
@Nonnull
JTypeVar generify (@Nonnull String name, @Nonnull Class <?> bound);
default JTypeVar generify (@Nonnull final String name, @Nonnull final Class <?> _extends)
{
return generify (name, owner ().ref (_extends));
}

/**
* Adds a new type variable to this declaration with a bound.
*
* @param name
* type variable name
* @param bound
* @param _extends
* Bound class
* @return The created {@link JTypeVar}
*/
@Nonnull
JTypeVar generify (@Nonnull String name, @Nonnull AbstractJClass bound);
default JTypeVar generify (@Nonnull final String name, @Nonnull final AbstractJClass _extends)
{
return generify (name).bound (_extends);
}

/**
* Iterates all the type parameters of this declaration.
Expand All @@ -93,12 +97,4 @@ public interface IJGenerifiable
*/
@Nonnull
JTypeVar [] typeParams ();

/**
* Get a list of all type parameters of this declaration.
*
* @return All type parameters
*/
@Nonnull
List <JTypeVar> typeParamList ();
}
10 changes: 2 additions & 8 deletions src/main/java/com/helger/jcodemodel/JDefinedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* {@link #field(int, AbstractJType, String)}.
*/
public class JDefinedClass extends AbstractJClassContainer <JDefinedClass>
implements IJDeclaration, IJGenerifiable, IJAnnotatable, IJDocCommentable
implements IJGenerifiable, IJAnnotatable, IJDocCommentable
{
/**
* The optional header that is emitted prior to the package (Issue #47)
Expand Down Expand Up @@ -148,7 +148,7 @@ public class JDefinedClass extends AbstractJClassContainer <JDefinedClass>
/**
* Helper class to implement {@link IJGenerifiable}.
*/
private final AbstractJGenerifiableImpl m_aGenerifiable = new AbstractJGenerifiableImpl ()
private final IJGenerifiable m_aGenerifiable = new AbstractJGenerifiableImpl ()
{
@Nonnull
public JCodeModel owner ()
Expand Down Expand Up @@ -778,12 +778,6 @@ public JTypeVar generify (@Nonnull final String name, @Nonnull final AbstractJCl
return m_aGenerifiable.typeParams ();
}

@Nonnull
public List <JTypeVar> typeParamList ()
{
return m_aGenerifiable.typeParamList ();
}

@Override
protected AbstractJClass substituteParams (final JTypeVar [] variables,
final List <? extends AbstractJClass> bindings)
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/helger/jcodemodel/JNarrowedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package com.helger.jcodemodel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -71,6 +72,11 @@ public JNarrowedClass (@Nonnull final AbstractJClass basis, @Nonnull final Abstr
this (basis, Collections.singletonList (arg));
}

public JNarrowedClass (@Nonnull final AbstractJClass basis, @Nonnull final AbstractJClass [] args)
{
this (basis, Arrays.asList (args));
}

public JNarrowedClass (@Nonnull final AbstractJClass basis, @Nonnull final List <? extends AbstractJClass> args)
{
super (basis.owner ());
Expand Down Expand Up @@ -290,17 +296,17 @@ protected AbstractJClass substituteParams (final JTypeVar [] variables,
final List <? extends AbstractJClass> bindings)
{
final AbstractJClass b = m_aBasis.substituteParams (variables, bindings);
boolean different = b != m_aBasis;
boolean bDifferent = b != m_aBasis;

final List <AbstractJClass> clazz = new ArrayList <> (m_aArgs.size ());
for (final AbstractJClass aClass : m_aArgs)
{
final AbstractJClass c = aClass.substituteParams (variables, bindings);
clazz.add (c);
different |= c != aClass;
bDifferent |= c != aClass;
}

if (different)
if (bDifferent)
return new JNarrowedClass (b, clazz);
return this;
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/helger/jcodemodel/JTypeVarClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
*/
package com.helger.jcodemodel;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -68,8 +66,8 @@ public String name ()
// This method is used for the main printing
if (m_aClass instanceof JDefinedClass)
{
final List <JTypeVar> aTypeParams = ((JDefinedClass) m_aClass).typeParamList ();
if (!aTypeParams.isEmpty ())
final JTypeVar [] aTypeParams = ((JDefinedClass) m_aClass).typeParams ();
if (aTypeParams.length > 0)
{
// We need the type params here!
return new JNarrowedClass (m_aClass, aTypeParams).name ();
Expand Down

0 comments on commit c0bfb26

Please sign in to comment.