001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.common.util.math;
019
020import java.util.ArrayList;
021import java.util.List;
022
023public class IPUtils {
024    public static boolean isValidIPV4(final String ip) {
025        String PATTERN =
026                "^((0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)\\.){3}(0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)$";
027
028        return ip.matches(PATTERN);
029    }
030
031    public static List<Byte> getDigitBytes(int num) {
032        List<Byte> digits = new ArrayList<>();
033        collectDigitBytes(num, digits);
034        return digits;
035    }
036
037    private static void collectDigitBytes(int num, List<Byte> digits) {
038        if (num / 10 > 0) {
039            collectDigitBytes(num / 10, digits);
040        }
041        digits.add((byte) (num % 10));
042    }
043
044    public static List<Integer> getDigits(int num) {
045        List<Integer> digits = new ArrayList<>();
046        collectDigits(num, digits);
047        return digits;
048    }
049
050    private static void collectDigits(int num, List<Integer> digits) {
051        if (num / 10 > 0) {
052            collectDigits(num / 10, digits);
053        }
054        digits.add(num % 10);
055    }
056}