matches

public static boolean matches(TypeToken<?> expected, TypeToken<?> actual)

Tells whether the actual type matches the expected.

Use Cases

Legend

Examples below refer to the types for simplicity; in fact, the respective TypeTokens for the types are passed into the method.

Rules

  • The same types always match; e.g. UserId.class always matches UserId.class.
  • Different types always don't match. e.g. UserId.class always does not match Nothing.class;
  • If actual is a subtype of expected, they match. e.g. matches(EventMessage, Nothing) returns true.
  • The generic parameters are taken into account in a similar manner: same always match, a subtype matches a parent type, different types aren't matching; e.g. matches(Collection<EventMessage>, Set<ProjectEvent>) is true; matches(Collection<EventMessage>, Set<CommandMessage>) is false.
  • If both expected and actual types define generic parameters, and the parameters contain Optional<T>, the latter is "unpacked" for comparison. e.g. matches( Iterable<EventMessage>, Triplet<ProjectCreated, ProjectAssigned, Optional<ProjectStarted> ) is true, matches(Optional<EventMessage>, Optional<AddTask>) is false.
  • Generic parameters with wildcard are not supported.