Other converters -
Weight converter
a = |
|
x = |
|
0 ----0 |
|
b = |
|
y = |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <first> <second atName="one"> <number id="one">1</number> <number id="two">2</number> </second> <second atName="two"> <number id="one">1</number> <number id="two">2</number> </second> </first>
public class DomParser { public static void main(String[] args) { try { File file = new File("src/testXML.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); String tab = ""; System.out.println("Staring Parsing..."); //Process root Node Node root = doc.getDocumentElement(); System.out.println(root.getNodeName()); processNode(root, "\t" + tab); System.out.println("Parsing Complete..."); } catch (Exception e) { e.printStackTrace(); } } public static void processNode(Node node, String tab) { try { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node ele = children.item(i); //Printing the node name or the text value in case of a Text node if (ele.getNodeName().equals("#text")) { System.out.print(" " + ele.getNodeValue()); } else { System.out.print(tab + ele.getNodeName()); } //Printing attributes of the current node. if (ele.hasAttributes()) { NamedNodeMap attrs = ele.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Attr attribute = (Attr) attrs.item(j); System.out.print(" " + attribute.getName() + "=" + attribute.getValue()); } } //Process children processNode(ele, "\t" + tab); } } catch (DOMException e) { e.printStackTrace(); } } }
Staring Parsing... first second atName=one number id=one 1 number id=two 2 second atName=two number id=one 1 number id=two 2 Parsing Complete...
package factory; public interface Country { public void printCountryCode(); }
package country; import factory.Country; public class India implements Country { @Override public void printCountryCode() { System.out.println("+91"); } }
package country; import factory.Country; public class Germany implements Country { @Override public void printCountryCode() { System.out.println("+49"); } }
package country; import factory.Country; public class Japan implements Country { @Override public void printCountryCode() { System.out.println("+81"); } }
package factory; import country.Germany; import country.India; import country.Japan; public class CountryFactory { public static void main(String[] args) { Country india = getCountry("India"); india.printCountryCode(); //Prints +91 Country germany = getCountry("Germany"); germany.printCountryCode(); //Prints +49
Country japan = getCountry("Japan"); japan.printCountryCode(); //Prints +81 } public static Country getCountry(String name) { if (name.equals("India")) { return new India(); } else if (name.equals("Germany")) { return new Germany(); } else if (name.equals("Japan")) { return new Japan(); } return null; } }
package factory; public class CountryReflectionFactory { public static void main(String[] args) { Country india = getCountry("country.India"); india.printCountryCode(); //Prints +91 Country germany = getCountry("country.Germany"); germany.printCountryCode(); //Prints +49 Country japan = getCountry("country.Japan"); japan.printCountryCode(); //Prints +81 } public static Country getCountry(String pathName) { try { return (Country) Class.forName(pathName).newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) { ex.printStackTrace(); return null; } } }
public class SingletonClass { private static SingletonClass singleton; private SingletonClass() { //private is used to restrict class instantiantion } public static SingletonClass getInstance() { if (singleton == null) { singleton = new SingletonClass(); } return singleton; } public static void releaseInstance() { singleton = null; } }
@Test public void UniqueTest_multithreading() throws InterruptedException { SingletonClass s1; SingletonClass s2; Runnable run = new Runnable() { @Override public void run() { try { SingletonClass s = SingletonClass.getInstance(); System.out.println(s.toString()); //Prints different object instances } catch (Exception e) { e.printStackTrace(); } } }; Thread t1 = new Thread(run, "First"); t1.start(); Thread t2 = new Thread(run, "Second"); t2.start(); t1.join(); t2.join(); }
public static SingletonClass getInstance() { if (singleton == null) { additionalFunctionality(); singleton = new SingletonClass(); } return singleton; } private static void additionalFunctionality() { if (Thread.currentThread().getName().equals("First")) { try { Thread.sleep(100); } catch (InterruptedException ex) { ex.printStackTrace(); } } }
public static SingletonClass getInstance() { synchronized (SingletonClass.class) { if (singleton == null) { additionalFunctionality(); singleton = new SingletonClass(); } } return singleton; }
@Test public void UniqueTest_serializable() { SingletonClass singleton = SingletonClass.getInstance(); writeSingletonObjectToDisk(singleton); SingletonClass s1 = readSingletonObjectFromDisk();
SingletonClass s2 = readSingletonObjectFromDisk();
assertEquals(s1, s2); //Fails }
private Object readResolve() { return SingletonClass.getInstance(); }
String path = "Path\\to\\target\\folder\\"; File folder = new File(path); File[] listOfFiles = folder.listFiles(); //Iterate through the files for (File f : listOfFiles) { f.delete(); }
folder.delete();