Skip to main content

Posts

Showing posts from October, 2013

Object Oriented Design: Relationship Types

Inheritance Inheritance relationship defines a parent child relationship in which child is said to be type of parent. Child inherits common actions and attributes from parent. Inheritance relationship allows rich abstractions to reduce code repetition and to increase code reuse. Consider the Passenger and StudentPassenger objects. StudentPassenger object is of type Passenger. StudentPassenger inherits all actions and attributes of Passenger. Additionally a StudentPassenger has a pass card and pays less than a normal Passenger. It is phrased as StudentPassenger is a Passenger . Association Association is a relationship in which one object uses another object to achieve some task. They don’t own each other and  their lifecycle is independent of each other.   Consider the relationship between bus and bus station. They don’t own each other and they exists regardless of each other. It is phrased as Bus uses a Bus Stop to pick passengers. Aggregation Aggregation is li

Java NIO: Working With Path Objects

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; public class PathDemo { public static void main(String[] args) { createTestDirectoryStructure(); Path path = Paths.get(".\\testroot\\path1\\sub1\\..\\sub2\\file1"); System.out.printf("File:\"%s\" %n\t File name:%s %n\t Parent name:%s %n\t Root:%s %n\t Absolute:%b %n",path,path.getFileName(), path.getParent(), path.getRoot(),path.isAbsolute()); System.out.printf("%n\t Name count:%d %n\t First name:%s %n\t Third name:%s %n",path.getNameCount(), path.getName(0), path.getName(1)); System.out.printf("%n\t URI:%s%n",path.toUri()); /** OUTPUT File:".\testroot\path1\sub1\..\sub2\file1"

Watching A Directory For Change

import java.io.IOException; import java.nio.file.*; public class ListenDirectory { public static void main(String[] args) { Path path = Paths.get(args[0]); WatchService watchService = null; try { watchService = path.getFileSystem().newWatchService(); path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE); } catch (IOException e) { e.printStackTrace(); } WatchKey key = null; try{ //infinite loop while((key = watchService.take())!=null){// blocks until a key is available // iterate for each event for(WatchEvent<?> event:key.pollEvents()){ switch(event.kind().name()){ case "OVERFLOW":

Using Files walkFileTree Method

There are several common uses of Files walkFileTree  method. 1.        Finding File import java . io . IOException ; import java . nio . file .*; import java . nio . file . attribute .*; class FileFindVisitor extends SimpleFileVisitor < Path > {     private PathMatcher matcher ;     public FileFindVisitor ( String pattern ){         try { matcher = FileSystems . getDefault (). getPathMatcher ( pattern );         } catch ( IllegalArgumentException e ) {             e . printStackTrace ();         }     }     public FileVisitResult visitFile ( Path path , BasicFileAttributes fileAttributes ){         find ( path );         return FileVisitResult . CONTINUE ;     }         private void find ( Path path ) {         Path name = path . getFileName ();         if ( matcher . matches ( name ))             System . out . println ( "Match found:" + path . getFileName ());     }         public FileVisitR