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

org.jtwig.plural.PluralOptions Maven / Gradle / Ivy

There is a newer version: 5.87.0.RELEASE
Show newest version
package org.jtwig.plural;

import com.google.common.base.Optional;
import org.jtwig.plural.model.PluralOption;
import org.jtwig.plural.parse.parboiled.ParserContext;
import org.jtwig.plural.parse.parboiled.PluralOptionsParser;
import org.parboiled.errors.ParserRuntimeException;
import org.parboiled.parserunners.BasicParseRunner;
import org.parboiled.support.ParsingResult;

import java.util.Collection;

import static org.parboiled.Parboiled.createParser;

public class PluralOptions {
    public static PluralOptions parse (String representation) {
        PluralOptionsParser parser = createParser(PluralOptionsParser.class, new ParserContext());

        BasicParseRunner> runner = new BasicParseRunner>(parser.Rule());

        try {
            ParsingResult> result = runner.run(representation);
            return new PluralOptions(result.resultValue);
        } catch (ParserRuntimeException e) {
            if (e.getCause() instanceof IllegalArgumentException) {
                throw (IllegalArgumentException) e.getCause();
            }
            throw e;
        }
    }

    private final Collection pluralOption;

    public PluralOptions(Collection pluralOption) {
        this.pluralOption = pluralOption;
    }

    public Optional select (int value) {
        for (PluralOption option : pluralOption) {
            if (option.apply(value)) {
                return Optional.of(option.getMessage());
            }
        }
        return Optional.absent();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy