All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sdl.dxa.api.datamodel.model.util.CanGetAndCast Maven / Gradle / Ivy

Go to download

DXA Data Model artifact provides API for de-/serializing CM JSON content into a DXA model

There is a newer version: 2.3.7
Show newest version
package com.sdl.dxa.api.datamodel.model.util;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Functional interface to get element from a {@link Collection} and cast it.
 *
 * @param  type of an identifier (e.g. key for a {@link Map})
 */
@FunctionalInterface
public interface CanGetAndCast {

    /**
     * Returns and element from the collection and casts it to a given class.
     * Basically calls {@link Map#get(Object)} or {@link List#get(int)} and casts.
     * Throws a {@link ClassCastException} is casting is not successful.
     *
     * @param identifier    key or index of the element
     * @param expectedClass class to cast to
     * @param            a required type
     * @return an element if any, null otherwise
     */
    @JsonIgnore
    default  T getAndCast(F identifier, @NotNull Class expectedClass) {
        return expectedClass.cast(getElement(identifier));
    }

    @JsonIgnore
    Object getElement(F identifier);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy