Java Fraction Format formatIndexValuePairs(List indicies, List values, String valueSeparator, String rangeSeparator)

Here you can find the source of formatIndexValuePairs(List indicies, List values, String valueSeparator, String rangeSeparator)

Description

format Index Value Pairs

License

Open Source License

Declaration

private static String formatIndexValuePairs(List<Integer> indicies, List<Double> values, String valueSeparator,
            String rangeSeparator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2011 Atlas of Living Australia
 * All Rights Reserved.//from  www  .  ja v a 2 s.c o m
 * 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (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.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ******************************************************************************/

import java.text.DecimalFormat;

import java.util.List;

public class Main {
    private static String formatIndexValuePairs(List<Integer> indicies, List<Double> values, String valueSeparator,
            String rangeSeparator) {
        DecimalFormat formatter = new DecimalFormat("#,##0.#");

        StringBuilder builder = new StringBuilder();

        int startRange = 0;
        int previousIndex = 0;
        double rangeValue = 0;

        for (int i = 0; i < indicies.size(); i++) {
            int index = indicies.get(i);
            double value = values.get(i);

            if (i == 0) {
                startRange = index;
                rangeValue = value;
            } else {
                if (previousIndex < index - 1 || value != rangeValue) {
                    builder.append(" ");
                    builder.append(startRange);

                    if (previousIndex != startRange) {
                        builder.append(rangeSeparator);
                        builder.append(previousIndex);
                    }

                    builder.append(valueSeparator);
                    builder.append(formatter.format(rangeValue));

                    startRange = index;
                    rangeValue = value;
                }

                if (i == indicies.size() - 1) {
                    builder.append(" ");
                    builder.append(startRange);

                    if (index != startRange) {
                        builder.append(rangeSeparator);
                        builder.append(index);
                    }

                    builder.append(valueSeparator);
                    builder.append(formatter.format(rangeValue));

                    startRange = index;
                    rangeValue = value;
                }
            }

            previousIndex = index;
        }

        return builder.toString().trim();
    }
}

Related

  1. FormatFloat(float value, int point)
  2. formatFloat(String format, double f)
  3. formatFloatToStr(Float str)
  4. FormatFolat(Float num)
  5. formatGetal(float getal)
  6. formatInt(double value)
  7. formatIntoCurr(double num, int digits)
  8. formatIntRate(double rateInt)
  9. formatKnots(double v)