Skip to content

Commit

Permalink
Expose a TypeMarker factory to wrap an arbitrary Type (#2264)
Browse files Browse the repository at this point in the history
Expose a TypeMarker factory to wrap an arbitrary Type
  • Loading branch information
carterkozak authored Mar 22, 2024
1 parent e810a01 commit 912ac04
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2264.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Expose a TypeMarker factory to wrap an arbitrary Type
links:
- https://github.com/palantir/conjure-java/pull/2264
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Objects;

/**
* Captures generic type information.
Expand Down Expand Up @@ -49,6 +50,10 @@ protected TypeMarker() {
SafeArg.of("typeVariable", type));
}

private TypeMarker(Type type) {
this.type = Preconditions.checkNotNull(type, "Type is required");
}

public final Type getType() {
return type;
}
Expand All @@ -57,4 +62,29 @@ public final Type getType() {
public final String toString() {
return "TypeMarker{type=" + type + '}';
}

/** Create a new {@link TypeMarker} instance wrapping the provided {@link Type}. */
public static TypeMarker<?> of(Type type) {
return new WrappingTypeMarker(type);
}

private static final class WrappingTypeMarker extends TypeMarker<Object> {
private WrappingTypeMarker(Type type) {
super(type);
}

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

@Override
public boolean equals(Object other) {
if (other instanceof WrappingTypeMarker) {
WrappingTypeMarker otherMarker = (WrappingTypeMarker) other;
return Objects.equals(getType(), otherMarker.getType());
}
return false;
}
}
}

0 comments on commit 912ac04

Please sign in to comment.