Java Scanner Read load(InputStream in, int elementCountOverride)

Here you can find the source of load(InputStream in, int elementCountOverride)

Description

load

License

Apache License

Declaration

public static ArrayList<Integer> load(InputStream in, int elementCountOverride) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

public class Main {
    protected static final int PARAMETER_NAME = 1;
    protected static final int PARAMETER_VALUE = 2;

    public static ArrayList<Integer> load(InputStream in) {
        Scanner scan = new Scanner(in);
        Map<String, String> parameters = getParameters(scan, null);
        int elements = getIntegerParameter(parameters, "elements", -1);

        return loadElements(scan, elements, 0);
    }//w ww .j a  v a  2s  .  co  m

    public static ArrayList<Integer> load(InputStream in, int elementCountOverride) {
        Scanner scan = new Scanner(in);
        Map<String, String> parameters = getParameters(scan, null);
        int elements = elementCountOverride;

        return loadElements(scan, elements, 0);
    }

    public static ArrayList<Integer> load(InputStream in, int elementCountOverride, int skipCount) {
        Scanner scan = new Scanner(in);
        Map<String, String> parameters = getParameters(scan, null);
        int elements = elementCountOverride;
        return loadElements(scan, elements, skipCount);
    }

    protected static Map<String, String> getParameters(Scanner scan, Map<String, String> initialMap) {
        Map<String, String> ret;
        Pattern parameter = Pattern.compile("--(?<parameterName>.+)=\\{(?<parameter>.+)\\}");
        MatchResult mr = null;

        if (initialMap == null) {
            ret = new HashMap<String, String>();
        } else {
            ret = initialMap;
        }

        if (scan.findInLine(parameter) != null) {
            mr = scan.match();
        }

        while (mr != null) {
            if (mr instanceof Matcher) {
                Matcher m = (Matcher) mr;
                ret.put(m.group("parameterName"), m.group("parameter"));
            } else {
                ret.put(mr.group(PARAMETER_NAME), mr.group(PARAMETER_VALUE));
            }

            mr = null;
            if (scan.findInLine(parameter) != null) {
                mr = scan.match();
            }
        }

        return ret;
    }

    protected static int getIntegerParameter(Map<String, String> parameters, String parameterName, int defaultInt) {
        int ret = defaultInt;
        String parameterString = parameters.get(parameterName);
        if (parameterString != null) {
            try {
                ret = Integer.parseInt(parameterString);
            } catch (NumberFormatException nfe) {
                ret = defaultInt;
            }
        }
        return ret;
    }

    protected static ArrayList<Integer> loadElements(Scanner scan, int elementCount, int skipCount) {
        ArrayList<Integer> sequence;
        if (elementCount > 0) {
            sequence = new ArrayList<>(elementCount);
        } else {
            sequence = new ArrayList<>();
        }

        while (scan.hasNextInt() && skipCount-- != 0) {
            scan.nextInt();
        }

        while (scan.hasNextInt() && elementCount-- != 0) {
            sequence.add(scan.nextInt());
        }

        return sequence;
    }
}

Related

  1. getStreamLines(final InputStream is)
  2. getThreadScanner()
  3. load(Class cls, String location)
  4. load(File in)
  5. load(final String fileName)
  6. loadAWSCredentails(String file_path)
  7. loadBooleanArray(BufferedReader in)
  8. loadConf()
  9. loadDoubleArray(Scanner reader)