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

org.ranksys.formats.parsing.Parsers Maven / Gradle / Ivy

Go to download

RankSys module, providing utilities for reading and writing into different formats of preference data, recommendations, etc.

The newest version!
/* 
 * Copyright (C) 2015 Information Retrieval Group at Universidad Autónoma
 * de Madrid, http://ir.ii.uam.es
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.ranksys.formats.parsing;

import static java.lang.Double.parseDouble;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import static java.util.stream.IntStream.range;

/**
 * Generic implementations of the interface Parser.
 *
 * @author Saúl Vargas ([email protected])
 */
public class Parsers {

    /**
     * Parse to int.
     */
    public static final ToIntFunction pip = from -> {
        int n = from.charAt(0) == '-' ? 1 : 0;
        int m = from.charAt(0) == '-' ? -1 : 1;
        return m * range(n, from.length())
                .map(i -> (from.charAt(i) - '0'))
                .reduce(0, (a, b) -> a * 10 + b);
    };

    /**
     * Parse to Integer.
     */
    public static Parser ip = from -> pip.applyAsInt(from);
    
    /**
     * Parse to long.
     */
    public static final ToLongFunction plp = from -> {
        int n = from.charAt(0) == '-' ? 1 : 0;
        int m = from.charAt(0) == '-' ? -1 : 1;
        return m * range(n, from.length())
                .mapToLong(i -> (from.charAt(i) - '0'))
                .reduce(0, (a, b) -> a * 10 + b);
    };

    /**
     * Parse to Long.
     */
    public static Parser lp = from -> plp.applyAsLong(from);

    /**
     * Parse to String.
     */
    public static Parser sp = from -> from.toString();

    /**
     * Parse to Float.
     */
    public static Parser fp = from -> Float.parseFloat(from.toString());

    /**
     * Parse to double.
     */
    public static ToDoubleFunction pdp = from -> parseDouble(from.toString());
    
    /**
     * Parse to Double.
     */
    public static Parser dp = from -> pdp.applyAsDouble(from);

    /**
     * Parse to Void.
     */
    public static Parser vp = from -> null;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy