Java ListIterator Usage normalizeVersion(String version, int length)

Here you can find the source of normalizeVersion(String version, int length)

Description

Normalizes a version string so that it has 'length' number of version numbers separated by '.'

License

Open Source License

Declaration

public static String normalizeVersion(String version, int length) 

Method Source Code

//package com.java2s;
/*/*from  w  ww .  ja va  2  s .  c  om*/
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

public class Main {
    /**
     * Normalizes a version string so that it has 'length' number of version numbers separated by '.'
     */
    public static String normalizeVersion(String version, int length) {
        if (version == null) {
            return null;
        }

        List<String> numbers = new ArrayList<String>(Arrays.asList(version
                .split("\\.")));

        while (numbers.size() < length) {
            numbers.add("0");
        }

        StringBuilder builder = new StringBuilder();

        for (ListIterator<String> iterator = numbers.listIterator(); iterator
                .hasNext() && iterator.nextIndex() < length;) {
            String number = iterator.next();

            if (iterator.hasNext() && iterator.nextIndex() < length) {
                builder.append(number + ".");
            } else {
                builder.append(number);
            }
        }

        return builder.toString();
    }
}

Related

  1. lastIndexOf(List list, T element)
  2. listEquals(List left, List right)
  3. listHasIdentContent(List list_1, List list_2)
  4. listToStringArray(java.util.List list)
  5. makeSortFieldFallbackExpr(List fieldNames)
  6. numberToDigits(int num)
  7. overlap(List> lists, int before, int after)
  8. parsePath(String sPath)
  9. partialSort(List list, int numTop, Comparator c)