Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: Create models for representing anonymous implementations (#434)
- Loading branch information
Showing
12 changed files
with
234 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...n/src/main/java/com/speedment/common/codegen/internal/model/value/AnonymousValueImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,83 @@ | |||
package com.speedment.common.codegen.internal.model.value; | |||
|
|||
import com.speedment.common.codegen.internal.util.Copier; | |||
import com.speedment.common.codegen.model.ClassOrInterface; | |||
import com.speedment.common.codegen.model.Generic; | |||
import com.speedment.common.codegen.model.Value; | |||
import com.speedment.common.codegen.model.trait.HasCopy; | |||
import com.speedment.common.codegen.model.value.AnonymousValue; | |||
|
|||
import java.util.ArrayList; | |||
import java.util.List; | |||
|
|||
/** | |||
* Default implementation of the {@link AnonymousValue} interface. | |||
* | |||
* @author Emil Forslund | |||
* @since 2.4.6 | |||
*/ | |||
public final class AnonymousValueImpl<T extends ClassOrInterface<T>> | |||
implements AnonymousValue<T> { | |||
|
|||
private final List<Value<?>> args; | |||
private final List<Generic> generics; | |||
private T classOrInterface; | |||
|
|||
public AnonymousValueImpl() { | |||
this.args = new ArrayList<>(); | |||
this.generics = new ArrayList<>(); | |||
} | |||
|
|||
private AnonymousValueImpl(AnonymousValue<T> prototype) { | |||
this.args = Copier.copy(prototype.getValues(), HasCopy::copy); | |||
this.generics = Copier.copy(prototype.getGenerics()); | |||
this.classOrInterface = Copier.copy(prototype.getValue()); | |||
} | |||
|
|||
@Override | |||
public List<Value<?>> getValues() { | |||
return args; | |||
} | |||
|
|||
@Override | |||
public List<Generic> getGenerics() { | |||
return generics; | |||
} | |||
|
|||
@Override | |||
public T getValue() { | |||
return classOrInterface; | |||
} | |||
|
|||
@Override | |||
public AnonymousValue<T> setValue(T value) { | |||
classOrInterface = value; | |||
return this; | |||
} | |||
|
|||
@Override | |||
public Value<T> copy() { | |||
return new AnonymousValueImpl<>(this); | |||
} | |||
|
|||
@Override | |||
public boolean equals(Object o) { | |||
if (this == o) return true; | |||
if (!(o instanceof AnonymousValue)) return false; | |||
|
|||
final AnonymousValue<?> that = (AnonymousValue<?>) o; | |||
if (!args.equals(that.getValues())) return false; | |||
else if (!getGenerics().equals(that.getGenerics())) return false; | |||
return classOrInterface != null | |||
? classOrInterface.equals(that.getValue()) | |||
: that.getValue() == null; | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
int result = args.hashCode(); | |||
result = 31 * result + getGenerics().hashCode(); | |||
result = 31 * result + (classOrInterface != null ? classOrInterface.hashCode() : 0); | |||
return result; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
common-parent/codegen/src/main/java/com/speedment/common/codegen/model/trait/HasValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
package com.speedment.common.codegen.model.trait; | |||
|
|||
import com.speedment.common.codegen.model.Value; | |||
|
|||
import java.util.List; | |||
|
|||
/** | |||
* Trait representing a type that has multiple {@link Value values}, like a | |||
* method or constructor invocation. | |||
* | |||
* @author Emil Forslund | |||
* @since 2.4.6 | |||
*/ | |||
public interface HasValues<T extends HasValues<T>> { | |||
|
|||
/** | |||
* Adds the specified {@link Value} to this model. | |||
* | |||
* @param generic the new child | |||
* @return a reference to this | |||
*/ | |||
@SuppressWarnings("unchecked") | |||
default <E> T add(final Value<E> generic) { | |||
getValues().add(generic); | |||
return (T) this; | |||
} | |||
|
|||
/** | |||
* Returns a list of all the values in this model. | |||
* <p> | |||
* The list returned must be mutable for changes! | |||
* | |||
* @return the values | |||
*/ | |||
List<Value<?>> getValues(); | |||
|
|||
} |
22 changes: 22 additions & 0 deletions
22
...parent/codegen/src/main/java/com/speedment/common/codegen/model/value/AnonymousValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,22 @@ | |||
package com.speedment.common.codegen.model.value; | |||
|
|||
import com.speedment.common.codegen.model.ClassOrInterface; | |||
import com.speedment.common.codegen.model.Value; | |||
import com.speedment.common.codegen.model.trait.HasGenerics; | |||
import com.speedment.common.codegen.model.trait.HasValues; | |||
|
|||
/** | |||
* A value representing a reference to an abstract implementation used | |||
* anonymously to set a field. | |||
* | |||
* @author Emil Forslund | |||
* @since 2.4.6 | |||
*/ | |||
public interface AnonymousValue<T extends ClassOrInterface<T>> | |||
extends Value<T>, | |||
HasGenerics<AnonymousValue<T>>, | |||
HasValues<AnonymousValue<T>> { | |||
|
|||
@Override | |||
AnonymousValue<T> setValue(T value); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters