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

com.ocadotechnology.config.ConfigParsers Maven / Gradle / Ivy

There is a newer version: 16.6.21
Show newest version
/*
 * Copyright © 2017-2023 Ocado (Ocava)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocadotechnology.config;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collector;

import com.google.common.base.Enums;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.ocadotechnology.id.Id;
import com.ocadotechnology.id.StringId;
import com.ocadotechnology.physics.units.LengthUnit;
import com.ocadotechnology.validation.Failer;

/**
 * Collection of parser functions used in the parsing of config values
 */
public class ConfigParsers {

    private ConfigParsers() {
        // Utility class
    }

    /**
     * Parses the input string as a Double, using {@link ConfigParsers#parseDouble(String)}, and validates that it lies between 0 and 1.
     *
     * This is useful in code relating to probability - to validate a value is valid for use as a probability.
     * @throws IllegalStateException if the input is not between 0 and 1.
     * @throws NumberFormatException if the config value cannot be parsed to a double.
     */
    public static double parseFraction(String s) {
        var proportion = parseDouble(s);
        Preconditions.checkState(proportion >= 0 && proportion <= 1, "Value must be between 0 and 1");
        return proportion;
    }

    /**
     * If the String is a number defers to {@link Integer#parseInt(String)}, if it is the String "max" or "min"
     * (case-insensitive) returns {@link Integer#MAX_VALUE} or {@link Integer#MIN_VALUE} respectively
     */
    public static int parseInt(String configValue) {
        if (configValue.equalsIgnoreCase("max")) {
            return Integer.MAX_VALUE;
        } else if (configValue.equalsIgnoreCase("min")) {
            return Integer.MIN_VALUE;
        }
        return Integer.parseInt(configValue);
    }

    /**
     * If the String is a number defers to {@link Long#parseLong(String)}, if it is the String "max" or "min"
     * (case-insensitive) returns {@link Long#MAX_VALUE} or {@link Long#MIN_VALUE} respectively
     */
    public static long parseLong(String configValue) {
        if (configValue.equalsIgnoreCase("max")) {
            return Long.MAX_VALUE;
        } else if (configValue.equalsIgnoreCase("min")) {
            return Long.MIN_VALUE;
        }
        return Long.parseLong(configValue);
    }

    /**
     * defers to {@link Double#parseDouble(String)}
     */
    public static double parseDouble(String configValue) {
        return Double.parseDouble(configValue);
    }

    /**
     * Unlike {@link Boolean#parseBoolean}, this parser only accepts TRUE or FALSE (case-insensitive). Other values will
     * cause a failure.
     *
     * @param value String value to parse
     */
    public static boolean parseBoolean(String value) {
        if ("true".equalsIgnoreCase(value)) {
            return true;
        } else if ("false".equalsIgnoreCase(value)) {
            return false;
        } else {
            throw Failer.fail("Invalid boolean value (%s).  Must be equal to true or false, case insensitive", value);
        }
    }

    /**
     * Parse the given String into a length value in the given {@link LengthUnit}. The String can specify input
     * length unit. If none are specified the SI Units are used.
     * 
*
* Example: *
     *     parseAcceleration("1, CENTIMETERS", METERS) => 0.01 m
     *     parseAcceleration("1", METERS) => 1 m
     * 
*
* SI Unit is {@link LengthUnit#METERS} * * @param value A String to parse. Must be either a number or a number with a {@link LengthUnit} separated by a * comma (",") or a colon (":"). */ public static double parseLength(String value, LengthUnit returnLengthUnit) { String[] parts = parseParts(value); double length = Double.parseDouble(parts[0].trim()); LengthUnit sourceUnit; if (parts.length == 1) { sourceUnit = LengthUnit.METERS; } else if (parts.length == 2) { sourceUnit = LengthUnit.valueOf(parts[1].trim()); } else { throw Failer.fail("Length values (%s) need to be specified without units (for SI) or in the following format: ',' or ':'", Arrays.toString(parts)); } return length * returnLengthUnit.getUnitsIn(sourceUnit); } /** * Parse the given String into a fractional time value in the given {@link TimeUnit}. The String can specify input * time unit. If none are specified the SI Units are used. *
*
* Example: *
     *     parseAcceleration("1, MINUTES", SECONDS) => 60 s
     *     parseAcceleration("1", SECONDS) => 1 s
     * 
*
* SI Unit is {@link TimeUnit#SECONDS} * * @param value A String to parse. Must be either a number or a number with a {@link TimeUnit} separated by a * comma (",") or a colon (":"). */ public static double parseFractionalTime(String value, TimeUnit returnTimeUnit) { String[] parts = parseParts(value); double time = Double.parseDouble(parts[0].trim()); TimeUnit sourceUnit; if (parts.length == 1) { sourceUnit = TimeUnit.SECONDS; } else if (parts.length == 2) { sourceUnit = TimeUnit.valueOf(parts[1].trim()); } else { throw Failer.fail("Time values (%s) need to be specified without units (for SI) or in the following format: ',




© 2015 - 2024 Weber Informatics LLC | Privacy Policy