Skip to content

Commit

Permalink
rename generic type parameters with consistent rules
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Dec 28, 2022
1 parent ee8cad4 commit efb744b
Show file tree
Hide file tree
Showing 35 changed files with 352 additions and 335 deletions.
17 changes: 17 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ gradle test --tests GrammarDebugTest

# Development Notes

## Conventions

### Generic Type Parameters

Generic type parameters are named with the follwing rules:

- `N` for `extends ASTNode`
- `C` for `extends ParserRuleContext`
- `T` for `extends ParseTree`
- `Child` for `extends ASTNode` if it's the child parameter of an `ASTNode` sub class
- `J` for `extends JobParameters`
- `E` for extending some kind of Enum
- `R` for some other return type
- `V` for generic (unconstrained) values that aren't any of the above

`P` can't be used because it makes javadoc think it's a `<p>` paragraph tag.

## AST Development

### AST Node Registration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* is added or removed from the list. The notification methods should be
* idempotent and reversible with the inverse other method.
*/
public abstract class ProxyArrayList<T> extends ArrayList<T> {
private Set<T> elements = null;
public abstract class ProxyArrayList<V> extends ArrayList<V> {
private Set<V> elements = null;

public ProxyArrayList() {
}
Expand All @@ -18,22 +18,22 @@ public ProxyArrayList(int initialCapacity) {
super(initialCapacity);
}

public ProxyArrayList(Collection<? extends T> c) {
public ProxyArrayList(Collection<? extends V> c) {
this(c, true);
}

public ProxyArrayList(Collection<? extends T> c, boolean notifyInitial) {
public ProxyArrayList(Collection<? extends V> c, boolean notifyInitial) {
super(c);
if (notifyInitial) {
notifyAdditionSafe(c);
}
}

protected abstract void notifyAddition(T added);
protected abstract void notifyAddition(V added);

protected abstract void notifyRemoval(T removed);
protected abstract void notifyRemoval(V removed);

void notifyAdditionSafe(T added) {
void notifyAdditionSafe(V added) {
if (added != null) {
notifyAddition(added);
if (elements != null) {
Expand All @@ -42,13 +42,13 @@ void notifyAdditionSafe(T added) {
}
}

void notifyAdditionSafe(Collection<? extends T> collection) {
for (var element : collection) {
void notifyAdditionSafe(Collection<? extends V> collection) {
for (V element : collection) {
notifyAdditionSafe(element);
}
}

private void notifyRemovalSafe(T removed) {
private void notifyRemovalSafe(V removed) {
if (removed != null) {
notifyRemoval(removed);
if (elements != null) {
Expand All @@ -57,22 +57,22 @@ private void notifyRemovalSafe(T removed) {
}
}

private void notifyRemovalSafe(Collection<? extends T> collection) {
for (var element : collection) {
private void notifyRemovalSafe(Collection<? extends V> collection) {
for (V element : collection) {
notifyRemovalSafe(element);
}
}

private Set<T> getElements() {
private Set<V> getElements() {
if (elements == null) {
elements = new HashSet<>(this);
}
return elements;
}

@Override
public T set(int index, T element) {
var prev = super.set(index, element);
public V set(int index, V element) {
V prev = super.set(index, element);
if (prev != element) {
notifyRemovalSafe(prev);
notifyAdditionSafe(element);
Expand All @@ -81,20 +81,20 @@ public T set(int index, T element) {
}

@Override
public boolean add(T e) {
public boolean add(V e) {
var result = super.add(e);
notifyAdditionSafe(e);
return result;
}

@Override
public void add(int index, T element) {
public void add(int index, V element) {
super.add(index, element);
notifyAdditionSafe(element);
}

@Override
public boolean addAll(Collection<? extends T> c) {
public boolean addAll(Collection<? extends V> c) {
var result = super.addAll(c);
if (result) {
notifyAdditionSafe(c);
Expand All @@ -103,7 +103,7 @@ public boolean addAll(Collection<? extends T> c) {
}

@Override
public boolean addAll(int index, Collection<? extends T> c) {
public boolean addAll(int index, Collection<? extends V> c) {
var result = super.addAll(index, c);
if (result) {
notifyAdditionSafe(c);
Expand All @@ -112,8 +112,8 @@ public boolean addAll(int index, Collection<? extends T> c) {
}

@Override
public T remove(int index) {
var removed = super.remove(index);
public V remove(int index) {
V removed = super.remove(index);
notifyRemovalSafe(removed);
return removed;
}
Expand All @@ -124,7 +124,7 @@ public boolean remove(Object o) {
var result = super.remove(o);
if (result) {
// the cast is ok because only T-type elements could have been added
notifyRemovalSafe((T) o);
notifyRemovalSafe((V) o);
}
return result;
}
Expand All @@ -136,15 +136,15 @@ public boolean removeAll(Collection<?> c) {
if (result) {
for (Object element : this) {
if (getElements().contains(element)) {
notifyRemovalSafe((T) element);
notifyRemovalSafe((V) element);
}
}
}
return result;
}

@Override
public void replaceAll(UnaryOperator<T> operator) {
public void replaceAll(UnaryOperator<V> operator) {
for (int i = 0; i < size(); i++) {
set(i, operator.apply(get(i)));
}
Expand All @@ -157,18 +157,18 @@ public boolean retainAll(Collection<?> c) {
if (result) {
for (Object element : this) {
if (!getElements().contains(element)) {
notifyRemovalSafe((T) element);
notifyRemovalSafe((V) element);
}
}
}
return result;
}

@Override
public boolean removeIf(Predicate<? super T> filter) {
public boolean removeIf(Predicate<? super V> filter) {
var result = super.removeIf(filter);
if (result) {
for (var element : this) {
for (V element : this) {
if (filter.test(element)) {
notifyRemovalSafe(element);
}
Expand Down Expand Up @@ -196,8 +196,8 @@ public void clear() {

@Override
@SuppressWarnings("unchecked")
public ProxyArrayList<T> clone() {
var result = (ProxyArrayList<T>) super.clone();
public ProxyArrayList<V> clone() {
var result = (ProxyArrayList<V>) super.clone();
result.elements = null;
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import org.antlr.v4.runtime.Token;

public class TypeUtil {
public static <T extends TokenTyped> T enumFromToken(T[] enumValues, Token token) {
for (T value : enumValues) {
public static <E extends TokenTyped> E enumFromToken(E[] enumValues, Token token) {
for (E value : enumValues) {
if (value.getTokenType() == token.getType()) {
return value;
}
}
throw new IllegalArgumentException("Unknown token: " + token.getText());
}

public static <T> T enumFromToken(T[] values, int[] tokenTypes, Token token) {
public static <E> E enumFromToken(E[] values, int[] tokenTypes, Token token) {
if (values.length != tokenTypes.length) {
throw new IllegalArgumentException("values.length != tokenTypes.length");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public ASTNode getAncestor(Predicate<ASTNode> predicate) {
* @return the first ancestor that is an instance of the given class, or null
* otherwise
*/
public <T extends ASTNode> T getAncestor(Class<T> ancestorType) {
public <N extends ASTNode> N getAncestor(Class<N> ancestorType) {
return ancestorType.cast(getAncestor(ancestorType::isInstance));
}

Expand All @@ -217,24 +217,24 @@ public ASTNode getBranchAncestor(int limit, int skip, BiPredicate<ASTNode, ASTNo
}

@SuppressWarnings("unchecked")
public <T extends ASTNode> T getBranchAncestor(int limit, int skip, Class<T> branchClass,
Function<T, ? extends ASTNode> branchGetter) {
return (T) getBranchAncestor(limit, skip, (node, last) -> {
public <N extends ASTNode> N getBranchAncestor(int limit, int skip, Class<N> branchClass,
Function<N, ? extends ASTNode> branchGetter) {
return (N) getBranchAncestor(limit, skip, (node, last) -> {
if (!branchClass.isInstance(node)) {
return false;
}
return branchGetter.apply(branchClass.cast(node)) == last;
});
}

public <T extends ASTNode> T getBranchAncestor(Class<T> branchClass,
Function<T, ? extends ASTNode> branchGetter) {
public <N extends ASTNode> N getBranchAncestor(Class<N> branchClass,
Function<N, ? extends ASTNode> branchGetter) {
return getBranchAncestor(Integer.MAX_VALUE, 0, branchClass, branchGetter);
}

public <T extends ASTNode, R extends ASTNode> R getBranchAncestorContinue(Class<T> branchClass,
Function<T, ? extends ASTNode> branchGetter, Class<R> continueClass) {
var result = getBranchAncestor(branchClass, branchGetter);
public <N extends ASTNode, R extends ASTNode> R getBranchAncestorContinue(Class<N> branchClass,
Function<N, ? extends ASTNode> branchGetter, Class<R> continueClass) {
N result = getBranchAncestor(branchClass, branchGetter);
return result == null ? null : result.getAncestor(continueClass);
}

Expand Down Expand Up @@ -438,14 +438,14 @@ public static boolean swap(ASTNode a, ASTNode b) {
* already have a reference to the current root but will not be registered to it
* yet.
*
* @param <NodeType> Type of the node for passthrough
* @param <N> Type of the node for passthrough
* @param node The node to add
* @param setter The setter to replace the node in this parent
* @return The node itself
*/
public <NodeType extends ASTNode> NodeType setup(
NodeType node,
Consumer<? extends NodeType> setter) {
public <N extends ASTNode> N setup(
N node,
Consumer<? extends N> setter) {
if (node != null) {
node.setParent(this, setter);
}
Expand All @@ -457,17 +457,17 @@ public <NodeType extends ASTNode> NodeType setup(
* node. Both of them may be null if either the existing value is being removed
* or a new value is being set.
*
* @param <NodeType> The type of the nodes for pass-through
* @param <N> The type of the nodes for pass-through
* @param currentNode The current node
* @param newNode The new node
* @param setter The setter to replace the node in this parent (this is
* usually a method reference to a setter method of the
* parent, this node)
*/
public <NodeType extends ASTNode> void updateParents(
NodeType currentNode,
NodeType newNode,
Consumer<? extends NodeType> setter) {
public <N extends ASTNode> void updateParents(
N currentNode,
N newNode,
Consumer<? extends N> setter) {
if (currentNode == newNode && newNode.getParent() == this) {
return;
}
Expand Down Expand Up @@ -501,18 +501,18 @@ public ASTNode cloneSeparate() {
}

@SuppressWarnings("unchecked") // the nodes clone themselves correctly
public static <T extends ASTNode> T clone(T node) {
public static <N extends ASTNode> N clone(N node) {
if (node == null) {
return null;
}
if (node.template == null) {
return (T) node.clone();
return (N) node.clone();
}
var replacement = node.template.getReplacement(node);
return replacement == null ? (T) node.clone() : replacement;
N replacement = node.template.getReplacement(node);
return replacement == null ? (N) node.clone() : replacement;
}

public static <T extends ASTNode> Stream<T> clone(ChildNodeList<T> nodes) {
public static <N extends ASTNode> Stream<N> clone(ChildNodeList<N> nodes) {
return nodes == null ? null : nodes.getClonedStream();
}
}
Loading

0 comments on commit efb744b

Please sign in to comment.