Class Separator

From Ssdlpedia

Jump to: navigation, search

Contents

[edit] Synopsis of Class Separator

public final class Separator { 
    /*
     * Forge (2)
     */
        Separator(char c); 
        Separator(String s); 
    /*
     * Type (1)
     */
        String toString(); 
    /*
     * Utilities (4)
     */
        static boolean isEmpty(Iterable<T> items); 
        static String wrap(String wrap, Iterable<T> items, String between); 
        static String wrap(String begin, String end, Iterable<T> items, String between); 
        static String wrap(String begin, String end, T[] items, String between); 
} 

Input types: Iterable<T>.


[edit] Code

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

import il.ac.technion.cs.ssdl.stereotypes.Instantiable;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
 * A class representing a separator string, which can be used for separating
 * elements of a sequence while printing it, without special case treatment of
 * the first or last element. For example, the following program prints a list
 * of its arguments separated by commas, without using any conditionals.

   static void main(String[] args) {
   	Separator s = new Separator(", ");
   	for (String a : args)
   		System.out.print(s + a);
   }

 * Author: Yossi Gil (
 * See:  12/02/2006)
 */
@Instantiable public final class Separator {
    private final String s;
    boolean first = true;
    
    public Separator(final char c) {
        s = "" + c;
    }
    
    public Separator(final String s) {
        this.s = s;
    }
    
    @Override public String toString() {
        if (!first)
            return s;
        first = false;
        return "";
    }
    
    public static <T> boolean isEmpty(final Iterable<T> items) {
        return !items.iterator().hasNext();
    }
    
    public static <T> String wrap(final String wrap, final Iterable<T> items, final String between) {
        return wrap(wrap, wrap, items, between);
    }
    
    public static <T> String wrap(final String begin, final String end, final Iterable<T> items, final String between) {
        if (isEmpty(items))
            return "";
        String $ = begin;
        final Separator s = new Separator(between);
        for (final T t : items)
            $ += s + t.toString();
        return $ + end;
    }
    
    public static <T> String wrap(@NonNull final String begin, @NonNull final String end, @NonNull final T[] items,
            final String between) {
        if (items.length == 0)
            return "";
        String $ = begin;
        final Separator s = new Separator(between);
        for (final T t : items)
            $ += s + t.toString();
        return $ + end;
    }
    
    static void main(final String[] args) {
        final Separator s = new Separator(", ");
        for (final String a : args)
            System.out.print(s + a);
    }
}

[edit] Metrics

MetricValueAcronymExplanation
LOC78Lines Of CodeTotal number of lines in the code
SCC24SemiColons CountTotal number of semicolon tokens found in the code.
NOT375Number Of TokensComments, whitespace and text which cannot be made into a token not included.
VCC1651Visible Characters CountThe total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC1157Code Characters CountTotal number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC38Unique Identifiers CountThe number of different identifiers found in the code
WHC3Weighted Horizontal ComplexityA heuritistic on horizontal complexity

[edit] Statistics

StatiticValue
Average token length 3.1
Tokens/line 4.8
Visible characters/line 21
Code characters/line 15
Semicolons/tokens6%
Comment text percentage29%

[edit] Tokens by Kind

Token KindOccurrences
KEYWORD65
OPERATOR34
LITERAL6
ID138
PUNCTUATION132
COMMENT2
OTHER209
Personal tools