Class Separator

From Ssdlpedia
Jump to: navigation, search

Contents

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>.


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);
    }
}

Metrics

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

Statistics

Statitic Value
Average token length 3.1
Tokens/line 4.8
Visible characters/line 21
Code characters/line 15
Semicolons/tokens 6%
Comment text percentage 29%

Tokens by Kind

Token Kind Occurrences
KEYWORD 65
OPERATOR 34
LITERAL 6
ID 138
PUNCTUATION 132
COMMENT 2
OTHER 209
Personal tools