Skip to content

Enum Separate

Ori Roth edited this page Apr 2, 2017 · 3 revisions

Synopsis of Enum Separate

public enum Separate { 
    /*
     * Utilities (32)
     */
        static String byCommas(Object[] os); 
        static String bySpaces(Object[] os); 
        static String byDots(Object[] os); 
        static String by(boolean[] bs, String between); 
        static String by(byte[] bs, String between); 
        static String by(char[] cs, String between); 
        static String by(double[] ds, String between); 
        static String by(float[] fs, String between); 
        static String by(int[] is, String between); 
        static String by(long[] ls, String between); 
        static String by(short[] ss, String between); 
        static String by(T[] ts, String between); 
        static String by(Iterable<T> ts, String between); 
        static String by(F<T> f, T[] ts, String between); 
        static String by(F<T> f, Iterable<? extends T> ts, String between); 
        static String by(boolean[] bs, char between); 
        static String by(byte[] bs, char between); 
        static String by(char[] cs, char between); 
        static String by(double[] ds, char between); 
        static String by(float[] fs, char between); 
        static String by(int[] is, char between); 
        static String by(long[] ls, char between); 
        static String by(short[] ss, char between); 
        static String by(T[] ts, char between); 
        static String by(Iterable<T> ts, char between); 
        static String by(Map<Key, Value> map, String between, String arrow); 
        static String by(F<T> f, T[] ts, char between); 
        static String by(F<T> f, Iterable<T> ts, char between); 
        final static String NL; 
        static String nl(Iterable<String> ss); 
        static String nl(String[] ss); 
        static void main(String[] args); 
    /*
     * Nested types (2)
     */
        static abstract interface F<T> { ... } 
        static class TEST { ... } 
} 

Input types: F, Iterable<? extends T>, Iterable, Iterable, Map<Key,Value>.

Synopsis of Interface Separate.F

public interface Separate.F<T> { 
    /*
     * Type (1)
     */
        String _(T t); 
} 

Synopsis of Class Separate.TEST

public static class Separate.TEST { 
    /*
     * Forge (1)
     */
        TEST(); 
    /*
     * Type (29)
     */
        final void testByCommas(); 
        final void testBySpaces(); 
        final void testByBooleanArrayString(); 
        final void testByByteArrayString(); 
        final void testByCharArrayString(); 
        final void testByDoubleArrayString(); 
        final void testByFloatArrayString(); 
        final void testByIntArrayString(); 
        final void testByLongArrayString(); 
        final void testByShortArrayString(); 
        final void testByTArrayString(); 
        final void testByIterableOfTString(); 
        final void testByFOfTTArrayString(); 
        final void testByFOfTIterableOfTString(); 
        final void testByBooleanArrayChar(); 
        final void testByByteArrayChar(); 
        final void testByCharArrayChar(); 
        final void testByDoubleArrayChar(); 
        final void testByFloatArrayChar(); 
        final void testByIntArrayChar(); 
        final void testByLongArrayChar(); 
        final void testByShortArrayChar(); 
        final void testByTArrayChar(); 
        final void testByIterableOfTChar(); 
        final void testByMapOfKeyValueStringString(); 
        final void testByFOfTTArrayChar(); 
        final void testByFOfTIterableOfTChar(); 
        final void testNlIterableOfString(); 
        final void testNlStringArray(); 
} 

Code

// SSDLPedia
package il.ac.technion.cs.ssdl.utils;

import static il.ac.technion.cs.ssdl.strings.StringUtils.esc;
import static il.ac.technion.cs.ssdl.utils.Box.box;
import static il.ac.technion.cs.ssdl.utils.DBC.nonnull;
import static org.junit.Assert.assertEquals;
import il.ac.technion.cs.ssdl.stereotypes.Utility;

import java.util.*;

import org.junit.Test;

/**
 * A utility class providing library functions that take an array or a
 * collection, and return a String composed by the elements of this
 * collection, separated by a given String or char.
 * 
 * Author: Yossi Gil, the Technion. 
 *         
 * See:  07/08/2008
 */
@Utility
public enum Separate {
    ;
    private static final char COMMA = ',';
    private static final char SPACE = ' ';
    private static final char DOT = '.';
    
    /**
     * Separate a variables length list of arguments by a comma character.
     * 
     * os the objects to be separated.
     * Return: a concatenation of the comma separated Object#toString()
     *         representations of the elements of os.
     */
    public static String byCommas(final Object... os) {
        return by(os, COMMA);
    }
    
    /**
     * Separate a variables length list of arguments by a space character.
     * 
     * os the objects to be separated.
     * Return: a concatenation of the space separated Object#toString()
     *         representations of the elements of os.
     */
    public static String bySpaces(final Object... os) {
        return by(Prune.whites(os), SPACE);
    }
    
    /**
     * Separate a variables length list of arguments by a space character.
     * 
     * os the objects to be separated.
     * Return: a concatenation of the space separated Object#toString()
     *         representations of the elements of os.
     */
    public static String byDots(final Object... os) {
        return by(Prune.whites(os), DOT);
    }
    
    /**
     * Separate elements of a given array of booleans by
     * a given {@String}
     * 
     * bs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in bs separated by
     *         between
     */
    public static String by(final boolean[] bs, final String between) {
        return by(box(bs), between);
    }
    
    /**
     * Separate elements of a given array of bytes by a
     * given {@String}
     * 
     * bs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in bs separated by
     *         between
     */
    public static String by(final byte[] bs, final String between) {
        return by(box(bs), between);
    }
    
    /**
     * Separate elements of a given array of chars by a
     * given {@String}
     * 
     * cs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in cs separated by
     *         between
     */
    public static String by(final char[] cs, final String between) {
        return by(box(cs), between);
    }
    
    /**
     * Separate elements of a given array of doubles by
     * a given {@String}
     * 
     * ds an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ds separated by
     *         between
     */
    public static String by(final double[] ds, final String between) {
        return by(box(ds), between);
    }
    
    /**
     * Separate elements of a given array of floats by a
     * given {@String}
     * 
     * fs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in fs separated by
     *         between
     */
    public static String by(final float[] fs, final String between) {
        return by(box(fs), between);
    }
    
    /**
     * Separate elements of a given array of ints by a
     * given {@String}
     * 
     * is an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in is separated by
     *         between
     */
    public static String by(final int[] is, final String between) {
        return by(box(is), between);
    }
    
    /**
     * Separate elements of a given array of longs by a
     * given {@String}
     * 
     * ls an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ls separated by
     *         between
     */
    public static String by(final long[] ls, final String between) {
        return by(box(ls), between);
    }
    
    /**
     * Separate elements of a given array of shorts by a
     * given {@String}
     * 
     * ss an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ss separated by
     *         between
     */
    public static String by(final short[] ss, final String between) {
        return by(box(ss), between);
    }
    
    /**
     * Separate elements of a given array by a given {@String}
     * 
     * ts an array of elements to be separated
     * <T> type of elements in the array parameter
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ts separated by
     *         between
     */
    public static <T> String by(final T[] ts, final String between) {
        return by(new F<T>() {
            @Override
            public String _(final T t) {
                return t.toString();
            }
        }, ts, between);
    }
    
    /**
     * Separate elements of a given Iterable collection by a given
     * {@String}
     * 
     * ts an Iterable collection of elements to be separated
     * <T> type of elements in the Iterable collection parameter
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ts separated by
     *         between
     */
    public static <T> String by(final Iterable<T> ts, final String between) {
        final Separator s = new Separator(between);
        final StringBuffer $ = new StringBuffer();
        for (final T t : ts)
            $.append(s).append(t);
        return $.toString();
    }
    
    /**
     * Separate elements of a given generic array by a given {@String}, where
     * the textual representation of each object is obtained by a user supplied
     * function.
     * 
     * ts an array of elements to be separated
     * <T> type of elements in ts
     * f a function object, providing a function that translates an
     *            object of type T into a String
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the result of applying
     *         function f to the elements in ts,
     *         separated by between
     */
    public static <T> String by(final F<T> f, final T[] ts, final String between) {
        final Separator s = new Separator(between);
        final StringBuffer $ = new StringBuffer();
        for (final T t : ts)
            $.append(s).append(f._(t));
        return $.toString();
    }
    
    /**
     * Separate elements of a given {@Iterable} collection of objects by a given
     * String, where the textual representation of each object is
     * obtained by a user supplied function.
     * 
     * ts an Iterable collection of elements to be separated
     * <T> type of elements in the Iterable collection parameter
     * f a function object, providing a function that translates an
     *            object of type T into a String
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the result of applying
     *         function f to the elements in ts,
     *         separated by between
     */
    public static <T> String by(final F<T> f, final Iterable<? extends T> ts, final String between) {
        final Separator s = new Separator(between);
        final StringBuffer $ = new StringBuffer();
        for (final T t : ts)
            $.append(s).append(f._(t));
        return $.toString();
    }
    
    /**
     * Separate elements of a given array of booleans by
     * a given char
     * 
     * bs an array of elements to be separated
     * between what should be used for separating these elements.
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in bs separated by
     *         between
     */
    public static String by(final boolean[] bs, final char between) {
        return by(box(bs), "" + between);
    }
    
    /**
     * Separate elements of a given array of bytes by a
     * given char
     * 
     * bs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in bs separated by
     *         between
     */
    public static String by(final byte[] bs, final char between) {
        return by(box(bs), "" + between);
    }
    
    /**
     * Separate elements of a given array of chars by a
     * given char
     * 
     * cs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in cs separated by
     *         between
     */
    public static String by(final char[] cs, final char between) {
        return by(box(cs), "" + between);
    }
    
    /**
     * Separate elements of a given array of doubles by
     * a given char
     * 
     * ds an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ds separated by
     *         between
     */
    public static String by(final double[] ds, final char between) {
        return by(box(ds), "" + between);
    }
    
    /**
     * Separate elements of a given array of floats by a
     * given char
     * 
     * fs an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in fs separated by
     *         between
     */
    public static String by(final float[] fs, final char between) {
        return by(box(fs), "" + between);
    }
    
    /**
     * Separate elements of a given array of ints by a
     * given char
     * 
     * is an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in is separated by
     *         between
     */
    public static String by(final int[] is, final char between) {
        return by(box(is), "" + between);
    }
    
    /**
     * Separate elements of a given array of longs by a
     * given char
     * 
     * ls an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ls separated by
     *         between
     */
    public static String by(final long[] ls, final char between) {
        return by(box(ls), "" + between);
    }
    
    /**
     * Separate elements of a given array of shorts by a
     * given char
     * 
     * ss an array of elements to be separated
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ss separated by
     *         between
     */
    public static String by(final short[] ss, final char between) {
        return by(box(ss), "" + between);
    }
    
    /**
     * Separate elements of a given array by a given char
     * 
     * ts an array of elements to be separated
     * <T> type of elements in the array parameter
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ts separated by
     *         between
     */
    public static <T> String by(final T[] ts, final char between) {
        return by(ts, "" + between);
    }
    
    /**
     * Separate elements of a given Iterable collection by a given
     * char
     * 
     * ts an Iterable collection of elements to be separated
     * <T> type of elements in the Iterable collection parameter
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the textual representation
     *         of the elements in ts separated by
     *         between
     */
    public static <T> String by(final Iterable<T> ts, final char between) {
        return by(ts, "" + between);
    }
    
    /**
     * A simple minded separation of members of a Map data type.
     * 
     * <Key> type of elements serving as keys of the map.
     * <Value> type of elements serving as values of the map.
     * map a non-null Map objects whose
     *            entries are to be separated.
     * between a non-null specifying what should
     *            be used for separating these entries.
     * arrow a non-null specifying what separates
     *            a key from a value
     * Return: a concatenation of all map entries, separated by
     *         separator, and where the key of each entry is
     *         separated from the value by arrow.
     */
    public static <Key, Value> String by(final Map<Key, Value> map, final String between, final String arrow) {
        nonnull(map);
        nonnull(between);
        nonnull(arrow);
        final Separator s = new Separator(between);
        final StringBuffer $ = new StringBuffer();
        for (final Key k : map.keySet())
            $.append(s).append(k).append(arrow).append(map.get(k));
        return $.toString();
    }
    
    /**
     * Separate elements of a given generic array by a given
     * char, where the textual representation of each
     * object is obtained by a user supplied function.
     * 
     * ts an array of elements to be separated
     * <T> type of elements in ts
     * f a function object, providing a function that translates an
     *            object of type T into a String
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the result of applying
     *         function f to the elements in ts,
     *         separated by between
     */
    public static <T> String by(final F<T> f, final T[] ts, final char between) {
        return by(f, ts, "" + between);
    }
    
    /**
     * Separate elements of a given {@Iterable} collection of objects by a given
     * char, where the textual representation of each
     * object is obtained by a user supplied function.
     * 
     * ts an Iterable collection of elements to be separated
     * <T> type of elements in the Iterable collection parameter
     * f a function object, providing a function that translates an
     *            object of type T into a String
     * between what should be used for separating these elements
     * Return: a {@String} obtained by concatenating the result of applying
     *         function f to the elements in ts,
     *         separated by between
     */
    public static <T> String by(final F<T> f, final Iterable<T> ts, final char between) {
        return by(f, ts, "" + between);
    }
    
    /**
     * An interface supplying a function object pointer, where the function
     * return value is String. To create such a pointer, create a
     * subclass that implements this interface (typically as an anonymous
     * class), giving an implementation to function #_(Object), and
     * then pass an instance of this subclass class.
     * 
     * Author: Yossi Gil, the Technion. 
     *         
 * See:  07/08/2008
     * <T> type of values that the function takes
     */
    public interface F<T> {
        /**
         * Anonymous function for translating an object into text
         * 
         * t an object of type T
         * Return: a textual
         */
        String _(T t);
    }
    
    public static final String NL = "\n";
    
    public static String nl(final Iterable<String> ss) {
        return by(ss, NL);
    }
    
    public static String nl(final String... ss) {
        return by(ss, NL);
    }
    
    /**
     * A simple program demonstrating the use of this class. This program prints
     * a comma separated list of its arguments, where special characters in each
     * argument are escaped prior to printing.
     * 
     * args list of the command line arguments.
     */
    public static void main(final String[] args) {
        System.out.println("Arguments are: " + Separate.by(new F<String>() {
            public String _(final String s) {
                return "\"" + esc(s) + "\"";
            }
        }, args, ", "));
    }
    
    public static class TEST {
        @Test
        public final void testByCommas() {
            assertEquals("A,B,C", Separate.byCommas("A", "B", "C"));
        }
        
        @Test
        public final void testBySpaces() {
            assertEquals("A B C", Separate.bySpaces("A", "B", "C"));
        }
        
        @Test
        public final void testByBooleanArrayString() {
            assertEquals("true; false", Separate.by(new boolean[] { true, false }, "; "));
        }
        
        @Test
        public final void testByByteArrayString() {
            assertEquals("-1; 2", Separate.by(new byte[] { -1, 2 }, "; "));
        }
        
        @Test
        public final void testByCharArrayString() {
            assertEquals("a; x", Separate.by(new char[] { 'a', 'x' }, "; "));
        }
        
        @Test
        public final void testByDoubleArrayString() {
            assertEquals("-1.0; 2.0", Separate.by(new double[] { -1.0, 2.0 }, "; "));
        }
        
        @Test
        public final void testByFloatArrayString() {
            assertEquals("-1.0; 2.0", Separate.by(new float[] { -1F, 2F }, "; "));
        }
        
        @Test
        public final void testByIntArrayString() {
            assertEquals("-1; 2", Separate.by(new int[] { -1, 2 }, "; "));
        }
        
        @Test
        public final void testByLongArrayString() {
            assertEquals("-1; 2", Separate.by(new long[] { -1L, 2L }, "; "));
        }
        
        @Test
        public final void testByShortArrayString() {
            assertEquals("-1; 2", Separate.by(new short[] { (short) -1, (short) 2 }, "; "));
        }
        
        @Test
        public final void testByTArrayString() {
            assertEquals("Hello, World", Separate.by(new String[] { "Hello", "World" }, ", "));
        }
        
        private static <T> Collection<T> makeCollection(final T... ts) {
            final ArrayList<T> $ = new ArrayList<T>();
            for (final T t : ts)
                $.add(t);
            return $;
        }
        
        @Test
        public final void testByIterableOfTString() {
            assertEquals("Hello, World", Separate.by(makeCollection("Hello", "World"), ", "));
        }
        
        @Test
        public final void testByFOfTTArrayString() {
            assertEquals("'Hello', 'World'", Separate.by(new Separate.F<String>() {
                @Override
                public String _(final String a) {
                    return "'" + a + '\'';
                }
            }, new String[] { "Hello", "World" }, ", "));
        }
        
        @Test
        public final void testByFOfTIterableOfTString() {
            assertEquals("'Hello', 'World'", Separate.by(new Separate.F<String>() {
                @Override
                public String _(final String a) {
                    return "'" + a + '\'';
                }
            }, makeCollection("Hello", "World"), ", "));
        }
        
        @Test
        public final void testByBooleanArrayChar() {
            assertEquals("true:false", Separate.by(new boolean[] { true, false }, ':'));
        }
        
        @Test
        public final void testByByteArrayChar() {
            assertEquals("3:-5", Separate.by(new byte[] { 3, -5 }, ':'));
        }
        
        @Test
        public final void testByCharArrayChar() {
            assertEquals("3:x", Separate.by(new char[] { '3', 'x' }, ':'));
        }
        
        @Test
        public final void testByDoubleArrayChar() {
            assertEquals("3.3:4.2", Separate.by(new double[] { 3.3, 4.2 }, ':'));
        }
        
        @Test
        public final void testByFloatArrayChar() {
            assertEquals("3.3:4.2", Separate.by(new float[] { 3.3F, 4.2F }, ':'));
        }
        
        @Test
        public final void testByIntArrayChar() {
            assertEquals("3:4", Separate.by(new int[] { 3, 4 }, ':'));
        }
        
        @Test
        public final void testByLongArrayChar() {
            assertEquals("3:4", Separate.by(new long[] { 3, 4 }, ':'));
        }
        
        @Test
        public final void testByShortArrayChar() {
            assertEquals("3:4", Separate.by(new short[] { 3, 4 }, ':'));
        }
        
        @Test
        public final void testByTArrayChar() {
            assertEquals("Hello,World", Separate.by(new String[] { "Hello", "World" }, ','));
        }
        
        @Test
        public final void testByIterableOfTChar() {
            assertEquals("Hello,World", Separate.by(makeCollection("Hello", "World"), ','));
        }
        
        @Test
        public final void testByMapOfKeyValueStringString() {
            final Map<String, Integer> map = new TreeMap<String, Integer>();
            map.put("One", box(1));
            map.put("Two", box(2));
            map.put("Three", box(3));
            map.put("Four", box(4));
            assertEquals("Four->4, One->1, Three->3, Two->2", Separate.by(map, ", ", "->"));
        }
        
        @Test
        public final void testByFOfTTArrayChar() {
            assertEquals("'Hello' 'World'", Separate.by(new Separate.F<String>() {
                @Override
                public String _(final String a) {
                    return "'" + a + '\'';
                }
            }, makeCollection("Hello", "World"), ' '));
        }
        
        @Test
        public final void testByFOfTIterableOfTChar() {
            assertEquals("<A> <B>", Separate.by(new Separate.F<String>() {
                @Override
                public String _(final String a) {
                    return "<" + a + '>';
                }
            }, new String[] { "A", "B" }, ' '));
        }
        
        @Test
        public final void testNlIterableOfString() {
            assertEquals("Hello\nWorld", Separate.nl(makeCollection("Hello", "World")));
        }
        
        @Test
        public final void testNlStringArray() {
            assertEquals("Hello\nWorld", Separate.nl("Hello", "World"));
        }
    }
}

Metrics

Metric Value Acronym Explanation
LOC 686 Lines Of Code Total number of lines in the code
SCC 103 SemiColons Count Total number of semicolon tokens found in the code.
NOT 2451 Number Of Tokens Comments, whitespace and text which cannot be made into a token not included.
VCC 18404 Visible Characters Count The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC 7543 Code Characters Count Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC 108 Unique Identifiers Count The number of different identifiers found in the code
WHC 6 Weighted Horizontal Complexity A heuritistic on horizontal complexity

Statistics

Statistic Value
Average token length 3.1
Tokens/line 3.6
Visible characters/line 27
Code characters/line 11
Semicolons/tokens 4%
Comment text percentage 59%

Tokens by Kind

Token Kind Occurrences
KEYWORD 404
OPERATOR 110
LITERAL 145
ID 672
PUNCTUATION 1120
COMMENT 33
OTHER 1189
Clone this wiki locally