Sunday, August 25, 2013

Example of Decorator Pattern - LineNumberScanner

Create a LineNumberScanner like this

public class LineNumberScanner implements Iterator<String>
{
    Scanner sc;
    int i = 1;
    public LineNumberScanner(Scanner sc)
    {
        this.sc = sc;
    }

    public boolean hasNextLine() {
        return sc.hasNextLine();
    }

    public String nextLine() {
        String nextLine = sc.nextLine();
        if(nextLine==null) return nextLine;
        else
        {
            return (i++)+":"+nextLine;
        }
    }

    @Override
    public void remove() {
        sc.remove();
    }

    @Override
    public boolean hasNext() {
        return sc.hasNext();
    }

    @Override
    public String next() {
        return sc.next();
    }
    public void close()
    {
        sc.close();
    }
}

Use the code as follows

public static void main(String[] args) throws FileNotFoundException 
    {
        File file = new File("D:\\Yogesh.txt");
        FileInputStream fis = new FileInputStream(file);
        Scanner sc = new Scanner(fis);
        LineNumberScanner lineScanner = new LineNumberScanner(sc);
        while(lineScanner.hasNextLine())
        {
            System.out.println(lineScanner.nextLine());
        }
    }

Example of Decorator Pattern - LineNumberReader

Create a LineNumberReader like this

public class LineNumberReader extends BufferedReader 
{
    int lineNo = 1;
    public LineNumberReader(Reader in)
    {
        super((BufferedReader)in);
    }
    public void close() throws IOException
    {
        in.close();
    }
    /*
     * Add more responsibility of adding a line Number in the beginning
     */
    @Override
    public String readLine() throws IOException
    {
        String line = super.readLine();
        if(line==null) return line;
        line = lineNo + ":" + line;
        lineNo++;
        return line;
    }

}

Use the code as follows

public static void main(String[] args) throws FileNotFoundException 
    {
        File file = new File("D:\\Yogesh.txt");
        BufferedReader bufr = new BufferedReader(new FileReader(file));
        BufferedReader lnr = new LineNumberReader(bufr);
        Scanner sc ;
        String line = "";
        try
        {
            line=lnr.readLine();
            while(line!=null)
            {
                System.out.println(line);
                line=lnr.readLine();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

Saturday, August 24, 2013

Example of Decorator Pattern - LowerCaseInputStream

Create a LowerCaseInputStream class as follows

public class LowerCaseInputStream extends FilterInputStream 
{

    public LowerCaseInputStream(InputStream in) {
        super(in);
    }
    
    @Override
    public int read() throws IOException
    {
        int c = super.read();
        if(c==-1)
            return c;
        else
            return Character.toLowerCase(c);
    }
    @Override
    public int read(byte b[], int offset, int len) throws IOException
    {
        int result = super.read(b, offset, len);
        for (int i = offset; i < offset+result; i++) 
        {
            b[i] = (byte)Character.toLowerCase((char)b[i]);
        }      
        return result;
    }
    
}

Use LowerCaseInputStream as follows

public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("D:\\Yogesh.txt");
        BufferedInputStream bufin = new BufferedInputStream(fis);
        InputStream in = new LowerCaseInputStream(bufin);
        int c;
        try
        {
            while((c=in.read())!=-1)
            {
                System.out.print((char)c);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
            
    }

Find it really interesting? You can read more about Decorator pattern in Head First Design Pattern book.