001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.harvester.datamodel;
025
026import java.util.List;
027
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029
030/**
031 * Number related utilities.
032 */
033public class NumberUtils {
034
035    /**
036     * Return the smallest value of two given positive longs, with the addition that -1 means infinity.
037     *
038     * @param l1 The first value
039     * @param l2 The second value
040     * @return Smallest value
041     */
042    public static long minInf(long l1, long l2) {
043        if (l1 != Constants.HERITRIX_MAXBYTES_INFINITY && l2 != Constants.HERITRIX_MAXBYTES_INFINITY) {
044            return Math.min(l1, l2);
045        } else if (l2 != Constants.HERITRIX_MAXBYTES_INFINITY) {
046            return l2;
047        } else {
048            return l1;
049        }
050    }
051
052    /**
053     * Compare two given positive longs, with the addition that -1 means infinity.
054     *
055     * @param l1 The first value
056     * @param l2 The second value
057     * @return -1 if first value is smallest, 0 if equal, 1 if second value is smallest
058     */
059    public static int compareInf(long l1, long l2) {
060        if (l1 == l2) {
061            return 0;
062        }
063        return minInf(l1, l2) == l1 ? -1 : 1;
064    }
065
066    /**
067     * Converts a list to an array of primitive values.
068     *
069     * @param list the list to convert
070     * @return an array of primitive values
071     */
072    public static final double[] toPrimitiveArray(List<Double> list) {
073        ArgumentNotValid.checkNotNull(list, "list");
074        if (list.isEmpty()) {
075            return new double[0];
076        }
077        double[] retArray = new double[list.size()];
078        for (int i = 0; i < list.size(); i++) {
079            retArray[i] = list.get(i);
080        }
081
082        return retArray;
083    }
084
085}