Java String Join joinWithSeparation(String a, String separator, String b)

Here you can find the source of joinWithSeparation(String a, String separator, String b)

Description

Appends two strings, inserting separator if either is empty

License

Open Source License

Declaration

public static String joinWithSeparation(String a, String separator,
        String b) 

Method Source Code

//package com.java2s;

import java.util.Iterator;

import java.util.Map;

public class Main {
    /**/*from  w  w w  .  ja va 2s .  com*/
     * Appends two strings, inserting separator if either is empty
     */
    public static String joinWithSeparation(String a, String separator,
            String b) {
        if (a.length() == 0)
            return b;
        if (b.length() == 0)
            return a;
        return a + separator + b;
    }

    /**
     * Appends two strings, inserting separator if either is empty. Modifies first map
     */
    public static Map<String, String> joinWithSeparation(
            Map<String, String> a, String separator, Map<String, String> b) {
        for (Iterator<String> it = b.keySet().iterator(); it.hasNext();) {
            String key = it.next();
            String bvalue = b.get(key);
            String avalue = a.get(key);
            if (avalue != null) {
                if (avalue.trim().equals(bvalue.trim()))
                    continue;
                bvalue = joinWithSeparation(avalue, separator, bvalue);
            }
            a.put(key, bvalue);
        }
        return a;
    }

    /**
     * Type-safe get
     * @param map
     * @param key
     * @return value
     */
    public static <K, V, M extends Map<K, V>> V get(M map, K key) {
        return map.get(key);
    }

    public static boolean equals(Object a, Object b) {
        return a == b ? true : a == null || b == null ? false : a.equals(b);
    }
}

Related

  1. joinPaths(String... paths)
  2. joinRepeat(int size, String s, String delim)
  3. joinSplit(String record, String regex)
  4. joinString(String separator, String... elements)
  5. joinValues(Map map, String separator)
  6. merge(ArrayList pArrayList, String pJoin)
  7. mergeDependencies(Set bundleDeps, List deps, Map disjointSets)
  8. prefixed_join(String padder, Vector v, boolean quoted)
  9. strjoin(String sep, String... args)