Java Iterable Join join(final Iterable source)

Here you can find the source of join(final Iterable source)

Description

join

License

Apache License

Declaration

public static <T> String join(final Iterable<T> source) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w. j ava  2 s  . c om*/
   Copyright 2009 Tomer Gabel <tomer@tomergabel.com>
    
   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
    
    
   ant-intellij-tasks project (http://code.google.com/p/ant-intellij-tasks/)
    
   $Id$
*/

import java.util.*;

public class Main {
    private static final Object DEFAULT_JOIN_SEPARATOR = ',';
    private static final boolean DEFAULT_JOIN_NULL_BEHAVIOR = false;

    public static <T> String join(final boolean renderNulls, final T... values) {
        return join(Arrays.asList(values), DEFAULT_JOIN_SEPARATOR, renderNulls);
    }

    public static <T> String join(final Object separator, final T... values) {
        return join(Arrays.asList(values), separator, DEFAULT_JOIN_NULL_BEHAVIOR);
    }

    public static <T> String join(final boolean renderNulls, final Object separator, final T... values) {
        return join(Arrays.asList(values), separator, renderNulls);
    }

    public static <T> String join(final Iterable<T> source) {
        return join(source, DEFAULT_JOIN_SEPARATOR, DEFAULT_JOIN_NULL_BEHAVIOR);
    }

    public static <T> String join(final Iterable<T> source, final boolean renderNulls) {
        return join(source, DEFAULT_JOIN_SEPARATOR, renderNulls);
    }

    public static <T> String join(final Iterable<T> source, final Object separator) {
        return join(source, separator, DEFAULT_JOIN_NULL_BEHAVIOR);
    }

    public static <T> String join(final Iterable<T> source, final Object separator, final boolean renderNulls) {
        final StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (final T value : source) {
            if (value == null && !renderNulls)
                continue;

            if (!first)
                sb.append(separator);
            else
                first = false;

            sb.append(value);
        }
        return sb.toString();
    }
}

Related

  1. join(final Iterable iterable, final String separator)
  2. join(final Iterable container)
  3. join(final Iterable iterable, final String joinString)
  4. join(final Iterable objs, final String delimiter)
  5. join(final Iterable objs, final String delimiter)
  6. join(final Iterable... iterables)
  7. join(final String delimiter, final Iterable objs)
  8. join(Iterable elements, String delimiter)
  9. join(Iterable iterable, String delimiter)