Skip to content

Commit

Permalink
Add cast methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
thegatesdev committed Jan 5, 2023
1 parent 4529c31 commit 4ff5879
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/main/java/io/github/thegatesdev/maple/data/DataElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,28 @@ public final void ifList(Consumer<DataList> listConsumer) {
}

/**
* Unsafe cast to E
* Unsafe cast this element to E
*
* @param elementClass The class to cast to.
* @param <E> The type to cast to.
* @param <E> The type to cast this element to.
* @return The cast element.
*/
@SuppressWarnings("unchecked")
public <E extends DataElement> E asUnsafe(Class<E> elementClass) {
public <E extends DataElement> E unsafeCast() {
return (E) this;
}

/**
* Cast this element to E
*
* @param <E> The type to cast this element to.
* @param elementClass The class to cast this element with.
* @return The same DataElement as E, or null if this element does not conform to elementClass.
*/
@SuppressWarnings("unchecked")
public <E extends DataElement> E castOrNull(Class<E> elementClass) {
return elementClass.isInstance(this) ? (E) this : null;
}

/**
* @return {@code true} if this element's name is not {@code null}
*/
Expand Down

0 comments on commit 4ff5879

Please sign in to comment.