code: public static <T extends Tag<?>> T checkTagCast(Tag<?> tag, Class<T> type) throws IllegalArgumentException {
          if (tag == null) {
              throw new IllegalArgumentException("Expected tag of type " + type.getName() + ", was null");
          } else if (!type.isInstance(tag)) {
              throw new IllegalArgumentException("Expected tag to be a " + type.getName() + ", was a " + tag.getClass().getName());
          }
          return type.cast(tag);
      }
docs: Checks that a tag is not null and of the required type
  
  @param tag The tag to check
  @param type The type of tag required
  @param <T> The type parameter of {@code type}
  @return The casted tag
  @throws IllegalArgumentException if the tag is null or not of the required type