Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: limit arrays #197

Open
yuraj11 opened this issue Mar 2, 2018 · 3 comments
Open

Feature request: limit arrays #197

yuraj11 opened this issue Mar 2, 2018 · 3 comments

Comments

@yuraj11
Copy link

yuraj11 commented Mar 2, 2018

Is there any way to limit arrays ? Something like this would be useful:
<myobj.items:{it|<it.something>}; limit=10 >

@yuraj11
Copy link
Author

yuraj11 commented Mar 6, 2018

Ok I have found a workaround for this:

group.defineDictionary("max", new MaxListItemsLimiter());

Usage (first item is always max. items count, second is List):
<max.(["50",myObject.items]):{msg|<msg.something>}>

final class MaxListItemsLimiter extends AbstractMap<String, Object> {

    @Override
    public Object get(Object key) {
        List items = (List) key;
        if (!items.isEmpty()) {
            //First item is max. count
            Integer limit = NumberUtils.toInt(items.get(0).toString(), -1); //use Integer.parseInt
            if (limit != -1) {
                return items.subList(1, Math.min(items.size(), limit + 1));
            } else {
                throw new AssertionError("First parameter in max must be number");
            }
        } else {
            return super.get(key);
        }
    }

    @Override
    public Set<Map.Entry<String, Object>> entrySet() {
        return Collections.emptySet();
    }

    @Override
    public boolean containsKey(Object key) {
        if (key instanceof List) {
            return true;
        } else {
            throw new AssertionError("You can use max only on Lists.");
        }
    }
}

@parrt
Copy link
Member

parrt commented Nov 8, 2018

Not a bad idea to have. thanks for the suggestion.

@Clashsoft
Copy link
Contributor

What is the use case? Pagination comes to mind, but that also requires a way to skip n items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants