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

com.relogiclabs.json.schema.internal.tree.FunctionCache Maven / Gradle / Ivy

Go to download

The New JSON Schema prioritizes simplicity, conciseness, and readability, making it user-friendly and accessible without the need for extensive prior knowledge. It offers efficient read-write facilities, precise JSON document definition through various data types and functions, and extensibility to meet modern web service diverse requirements.

There is a newer version: 1.12.1
Show newest version
package com.relogiclabs.json.schema.internal.tree;

import com.relogiclabs.json.schema.type.JFunction;
import com.relogiclabs.json.schema.type.JNode;
import lombok.Getter;
import lombok.Setter;
import lombok.Value;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.relogiclabs.json.schema.internal.util.MiscellaneousHelper.getDerived;

public class FunctionCache implements Iterable {

    @Value
    public static class Entry {
        MethodPointer methodPointer;
        Object[] arguments;

        public boolean isTargetMatch(JNode target) {
            return methodPointer.getParameter(0).getType().isInstance(getDerived(target));
        }

        public Object invoke(JFunction function, JNode target) {
            arguments[0] = getDerived(target);
            return methodPointer.invoke(function, arguments);
        }
    }

    @Getter @Setter
    private static int sizeLimit = 10;
    private final List cache;

    public FunctionCache() {
        this.cache = new ArrayList<>(sizeLimit);
    }

    public void add(MethodPointer methodPointer, Object[] arguments) {
        if(cache.size() > sizeLimit) cache.remove(0);
        arguments[0] = null;
        cache.add(new Entry(methodPointer, arguments));
    }

    @Override
    public Iterator iterator() {
        return cache.iterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy