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

com.networknt.schema.resource.MapSchemaMapper Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
package com.networknt.schema.resource;

import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

import com.networknt.schema.AbsoluteIri;

/**
 * Map implementation of {@link SchemaMapper}.
 */
public class MapSchemaMapper implements SchemaMapper {
    private final Function mappings;
    
    public MapSchemaMapper(Map mappings) {
        this(mappings::get);
    }

    public MapSchemaMapper(Function mappings) {
        this.mappings = mappings;
    }

    /**
     * Apply the mapping function if the predicate is true.
     * 
     * @param test     the predicate
     * @param mappings the mapping
     */
    public MapSchemaMapper(Predicate test, Function mappings) {
        this.mappings = iri -> {
            if (test.test(iri)) {
                return mappings.apply(iri);
            }
            return null;
        };
    }

    @Override
    public AbsoluteIri map(AbsoluteIri absoluteIRI) {
        String mapped = this.mappings.apply(absoluteIRI.toString());
        if (mapped != null) {
            return AbsoluteIri.of(mapped);
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy