Code example for Formatter

Methods: close, flush, out

0
            throw new RuntimeException("NF.flush should not be called");
        } 
    } 
 
    private static void test(Formatter f) {
        if (f.out() instanceof F) {
            // F.flush() called since F implements Flushable 
            try { 
                f.flush();
                throw new RuntimeException("F.flush not called");
            } catch (ExpectedException x) {
                System.out.println("  F.flush called");
            } 
        } else { 
            // NF.flush() not called since NF does not implement Flushable 
            f.flush();
        } 
 
        // Formatter is a Flushable 
        if (!(f instanceof Flushable))
            throw new RuntimeException("Formatter is not a Flushable");
 
        // flush() after close() throws a FormatterClosedException 
        f.close();
        try { 
            f.flush();
            throw new RuntimeException("FormatterClosedException not thrown");
        } catch (FormatterClosedException x) {
            System.out.println("  FormatterClosedException thrown");
        } 
    } 
 
    public static void main(String [] args) {
        System.out.println("testing Flushable");
        test(new Formatter(new F()));
        System.out.println("testing non-Flushable");
        test(new Formatter(new NF()));
    } 
}