Skip to content

Class CSVLine

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

Synopsis of Class CSVLine

public abstract class CSVLine { 
    /*
     * Forge (1)
     */
        CSVLine(); 
    /*
     * Type (14)
     */
        abstract String line(); 
        abstract String header(); 
        abstract void put(String key, String value); 
        final void put(String key); 
        final void put(String key, Object value); 
        final void put(String key, float value); 
        final void put(String key, double value); 
        final void put(String key, int value); 
        final void put(String key, long value); 
        final void put(String key, short value); 
        final void put(String key, char value); 
        final void put(String key, boolean value); 
        final void put(String key, Object[] a, int i); 
        final void put(String key, Object[] os); 
    /*
     * Utilities (8)
     */
        static final char QUOTE; 
        static final char DELIMETER; 
        static final String ARRAY_SEPARATOR; 
        static String QUOTE(); 
        static String quote(String s); 
        static String makeLine(Iterable<String> ss); 
        static String makeLine(String[] ss); 
        static String makeLine(Object[] ss); 
    /*
     * Nested types (2)
     */
        static final class Sorterd extends CSVLine { ... } 
        static final class Ordered extends CSVLine { ... } 
} 

Input types: Iterable<String>.

Synopsis of Class CSVLine.Sorterd

public static final class CSVLine.Sorterd extends CSVLine { 
    /*
     * Forge (1)
     */
        Sorterd(); 
    /*
     * Type (3)
     */
        String header(); 
        String line(); 
        final void put(String key, String value); 
}

Synopsis of Class CSVLine.Ordered

public static final class CSVLine.Ordered extends CSVLine { 
    /*
     * Forge (1)
     */
        Ordered(); 
    /*
     * Type (3)
     */
        final String header(); 
        final String line(); 
        final void put(String key, String value); 
} 

Synopsis of Class CSVLine.Ordered.Record

private static final class CSVLine.Ordered.Record { 
    /*
     * Forge (1)
     */
        Record(String key, String value); 
    /*
     * Type (2)
     */
        final String key; 
        String value; 
}

Code

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

import static il.ac.technion.cs.ssdl.utils.DBC.nonnull;
import il.ac.technion.cs.ssdl.stereotypes.Canopy;
import il.ac.technion.cs.ssdl.stereotypes.Instantiable;
import il.ac.technion.cs.ssdl.utils.Separate;

import java.util.*;

/**
 * Create a line in an "Comma Separated Values" format from a sequence of named
 * values.
 * 
 * Author: Yossi Gil
 */
@Instantiable public abstract class CSVLine {
    /**
     * Wraps values in a CSV line. Occurrences of this character in field
     * content are escaped by typing it twice.
     */
    public static final char QUOTE = '\"';
    /**
     * Separates values in a CSV line
     */
    public static final char DELIMETER = ',';
    /**
     * Separator of multi-values, i.e., array elements stored in a single field
     */
    public static final String ARRAY_SEPARATOR = ";";
    
    /**
     * A total inspector
     * 
     * Return: the content of the CSV line as per all recorded values.
     */
    public abstract String line();
    
    /**
     * A total inspector
     * 
     * Return: the header of the CSV line
     */
    public abstract String header();
    
    /**
     * A mutator to add a key and a general String value to the CSV line
     * 
     * key the key to be added
     * value The value associated with the key
     */
    public abstract void put(final String key, final String value);
    
    /**
     * Add a key without a value to the current CSV line.
     * 
     * key The key to be inserted
     */
    public final void put(final String key) {
        nonnull(key);
        put(key, "");
    }
    
    /**
     * Add a key and a general Object value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final Object value) {
        nonnull(key);
        if (value == null)
            put(key);
        else
            put(key, value.toString());
    }
    
    /**
     * Add a key and a general float<tt> value to the CSV line</font>
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final float value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    /**
     * Add a key and a <tt>double<tt> value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final double value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    /**
     * Add a key and an <tt>int</tt> value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final int value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    /**
     * Add a key and a <tt>long</tt> value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final long value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    /**
     * Add a key and a <tt>short</tt> value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final short value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    /**
     * Add a key and a <tt>char</tt> value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final char value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    /**
     * Add a key and a <tt>boolean</tt> value to the CSV line
     * 
     * key The key to be added
     * value The value associated with the key
     */
    public final void put(final String key, final boolean value) {
        nonnull(key);
        put(key, "" + value);
    }
    
    public final void put(final String key, final Object a[], final int i) {
        nonnull(key);
        put(key, a == null || i < 0 || i >= a.length ? null : a[i]);
    }
    
    public final void put(final String key, final Object[] os) {
        nonnull(key);
        put(key, os == null ? null : Separate.by(os, ARRAY_SEPARATOR));
    }
    
    /**
     * Return: the QUOTE
     */
    public static String QUOTE() {
        return "" + QUOTE;
    }
    
    public static String quote(final String s) {
        return QUOTE() + //
                (s == null ? "" : s.replaceAll(QUOTE(), QUOTE() + QUOTE())) //
                + QUOTE();
    }
    
    static public String makeLine(final Iterable<String> ss) {
        return Separate.by(new Separate.F<String>() {
            public final String _(final String o) {
                return quote(o);
            }
        }, ss, DELIMETER);
    }
    
    static public String makeLine(final String... ss) {
        return Separate.by(new Separate.F<String>() {
            public final String _(final String o) {
                return quote(o);
            }
        }, ss, DELIMETER);
    }
    
    static public String makeLine(final Object... ss) {
        return Separate.by(new Separate.F<Object>() {
            public final String _(final Object o) {
                return quote(o.toString());
            }
        }, ss, DELIMETER);
    }
    
    @Canopy public static final class Sorterd extends CSVLine {
        private final Map<String, String> map = new TreeMap<String, String>();
        
        @Override public String header() {
            return makeLine(map.keySet());
        }
        
        @Override public String line() {
            return makeLine(map.values());
        }
        
        @Override public final void put(final String key, final String value) {
            map.put(key, value);
        }
    }
    
    @Canopy public static final class Ordered extends CSVLine {
        private final static class Record {
            public final String key;
            public String value;
            
            public Record(final String key, final String value) {
                this.key = key;
                this.value = value;
            }
        }
        
        private final List<Record> map = new ArrayList<Record>();
        
        @Override public final String header() {
            return Separate.by(new Separate.F<Record>() {
                @Override public String _(final Record r) {
                    return quote(r.key);
                }
            }, map, DELIMETER);
        }
        
        @Override public final String line() {
            return Separate.by(new Separate.F<Record>() {
                @Override public String _(final Record r) {
                    return quote(r.value);
                }
            }, map, DELIMETER);
        }
        
        @Override public final void put(final String key, final String value) {
            for (final Record r : map)
                if (key.equals(r.key)) {
                    r.value = value;
                    return;
                }
            map.add(new Record(key, value));
        }
    }
}

Metrics

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

Statistics

Statistic Value
Average token length 3
Tokens/line 4.1
Visible characters/line 19
Code characters/line 12
Semicolons/tokens 5%
Comment text percentage 35%

Tokens by type

Token Kind Occurrences
KEYWORD 196
OPERATOR 51
LITERAL 14
ID 346
PUNCTUATION 435
COMMENT 20
OTHER 552
Clone this wiki locally