Skip to content

Class Tab

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

Synopsis of Class Tab

public class Tab { 
    /*
     * Forge (2)
     */
        Tab(); 
        Tab(String tab); 
    /*
     * Type (8)
     */
        final String tab; 
        String toString(); 
        void more(); 
        void less(); 
        String begin(); 
        String end(); 
        boolean isEmpty(); 
        void linef(String format, Object[] os); 
    /*
     * Nested types (1)
     */
        static class TEST { ... } 
}

Synopsis of Class Tab.TEST

public static class Tab.TEST { 
    /*
     * Forge (1)
     */
        TEST(); 
    /*
     * Type (16)
     */
        void testDone(); 
        void emtpyTrue(); 
        void emptyContent(); 
        void emptyFalse(); 
        void testOneMore(); 
        void testTwoMore(); 
        void testOneMoreOneLess(); 
        void testTwoMoreOneLess(); 
        void testTwoMoreTwoLessOneMore(); 
        void testTwoMoreTwoLessTwoMore(); 
        void testBeginAtZero(); 
        void testBeginAtLevelOne(); 
        void testEndAtLevelZero(); 
        void testEndAtLevelOne(); 
        void testEndAtLevelTwo(); 
        void testDecrementFailsWhenDone(); 
}

Code

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

import static il.ac.technion.cs.ssdl.utils.DBC.require;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import il.ac.technion.cs.ssdl.stereotypes.Instantiable;

import java.io.PrintStream;

import org.junit.Test;

/**
 * Prefix text with varying indentation level. Class can be used for an indented
 * printout of a hierarchical tree data structure, e.g.,:


   void printTree(TreeNode t) {
    printTree(new Tab(), t);
   }
   
   void printTree(Tab tab, TreeNode t) {
    System.out.println(tab + "Node: " + t.data);
    tab.more();
    printTree(tab, t.left);
    printTree(tab, t.right);
    tab.less();
   }

 * Author: Adrian Kuhn
 * See:  August 4th, 2008
 */
@Instantiable public class Tab {
    /**
     * What to add before each indented line of text?
     */
    private String indentation = "";
    /**
     * Indentation is increased by steps of this String.
     */
    public final String tab;
    
    /*
     * Provides the actual indentation String
     * @see java.lang.Object#toString()
     */
    @Override public String toString() {
        return indentation;
    }
    
    /**
     * Instantiate this class with the default '\t' tabulation
     * character.
     */
    public Tab() {
        this("\t");
    }
    
    /**
     * Instantiate this class with a specified tabulation String.
     * 
     * tab a String by which indentation should be increased at
     *            each #more() action.
     */
    public Tab(final String tab) {
        this.tab = tab;
    }
    
    /**
     * Increase indentation level
     */
    public void more() {
        indentation += tab;
    }
    
    /**
     * Decrease indentation level.
     */
    public void less() {
        require(!isEmpty());
        indentation = indentation.substring(0, indentation.length() - tab.length());
    }
    
    /**
     * Increase indentation but returns the previous tabulation string.
     * 
     * Return: the previous tabulation string.
     */
    public String begin() {
        final String $ = toString();
        more();
        return $;
    }
    
    /**
     * Decrease indentation level and returns the new tabulation string.
     * 
     * Return: the new tabulation string.
     */
    public String end() {
        less();
        return toString();
    }
    
    /**
     * Determine whether backward tabbing is not possible any more.
     * 
     * Return: true iff if this instance cannot
     *         provide any lesser indentation.
     */
    public boolean isEmpty() {
        return indentation.length() == 0;
    }
    
    /**
     * Send a formatted, indented by this instance, line to System#out.
     * 
     * format A format string as described in PrintStream#printf.
     *            This format string should not include the terminating
     *            '\n' character.
     * os Arguments, referenced by the format specifiers in the format
     *            string, as described
     */
    public void linef(final String format, final Object... os) {
        System.out.printf(toString() + format + "\n", os);
    }
    
    /**
     * A JUnit test class for the enclosing class.
     * 
     * Author: Yossi Gil, the Technion.
     * See:  05/08/2008
     */
    public static class TEST {
        @Test public void testDone() {
            final Tab t = new Tab();
            assertTrue(t.isEmpty());
        }
        
        @Test public void emtpyTrue() {
            final Tab t = new Tab();
            assertTrue(t.isEmpty());
        }
        
        @Test public void emptyContent() {
            final Tab n = new Tab("abc");
            assertEquals("", n.toString());
        }
        
        @Test public void emptyFalse() {
            final Tab t = new Tab("abc");
            t.more();
            assertFalse(t.isEmpty());
        }
        
        @Test public void testOneMore() {
            final Tab t = new Tab("abc");
            t.more();
            assertEquals("abc", t.toString());
        }
        
        @Test public void testTwoMore() {
            final Tab t = new Tab("abc");
            t.more();
            t.more();
            assertEquals("abcabc", t.toString());
        }
        
        @Test public void testOneMoreOneLess() {
            final Tab t = new Tab("abc");
            t.more();
            t.less();
            assertEquals("", t.toString());
        }
        
        @Test public void testTwoMoreOneLess() {
            final Tab t = new Tab("abc");
            t.more();
            t.more();
            t.less();
            assertEquals("abc", t.toString());
        }
        
        @Test public void testTwoMoreTwoLessOneMore() {
            final Tab t = new Tab("abc");
            t.more();
            t.more();
            t.less();
            t.less();
            t.more();
            assertEquals("abc", t.toString());
        }
        
        @Test public void testTwoMoreTwoLessTwoMore() {
            final Tab t = new Tab("abc");
            t.more();
            t.more();
            t.less();
            t.less();
            t.more();
            t.more();
            assertEquals("abcabc", t.toString());
        }
        
        private static String cat(final String s1, final String s2) {
            return "[[" + s1 + "]]" + "[[" + s2 + "]]";
        }
        
        @Test public void testBeginAtZero() {
            final Tab t = new Tab("abc");
            assertEquals(cat("", "abc"), cat(t.begin(), t.toString()));
        }
        
        @Test public void testBeginAtLevelOne() {
            final Tab t = new Tab("abc");
            t.more();
            assertEquals(cat("abc", "abcabc"), cat(t.begin(), t.toString()));
        }
        
        @Test(expected = DBC.Bug.Contract.Precondition.class)//
        public void testEndAtLevelZero() {
            final Tab t = new Tab("abc");
            assertEquals(cat("", ""), cat(t.end(), t.toString()));
        }
        
        @Test public void testEndAtLevelOne() {
            final Tab t = new Tab("abc");
            t.more();
            assertEquals(cat("", ""), cat(t.end(), t.toString()));
        }
        
        @Test public void testEndAtLevelTwo() {
            final Tab t = new Tab("abc");
            t.more();
            t.more();
            assertEquals(cat("abc", "abc"), cat(t.end(), t.toString()));
        }
        
        @Test(expected = DBC.Bug.Contract.Precondition.class)//
        public void testDecrementFailsWhenDone() {
            final Tab t = new Tab("abc");
            t.less();
        }
    }
}

Metrics

Metric Value Acronym Explanation
LOC 248 Lines Of Code Total number of lines in the code
SCC 80 SemiColons Count Total number of semicolon tokens found in the code.
NOT 1004 Number Of Tokens Comments, whitespace and text which cannot be made into a token not included.
VCC 4497 Visible Characters Count The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC 2848 Code Characters Count Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC 66 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 2.8
Tokens/line 4
Visible characters/line 18
Code characters/line 11
Semicolons/tokens 7%
Comment text percentage 36%

Tokens by kind

Token Kind Occurrences
KEYWORD 114
OPERATOR 33
LITERAL 40
ID 307
PUNCTUATION 510
COMMENT 16
OTHER 423
Clone this wiki locally