a = |
|
x = |
|
0 ----0 |
|
b = |
|
y = |
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();
public class Car { private String name; private String other; public Car(String name, String other) { this.name = name; this.other = other; } //Getters and Setters }
public class DataModel extends LazyDataModel<Car> { List<Car> listVals; private List<Car> datasource; private int count = 200; private int backCount = 200; private String rowString = ""; private static final long serialVersionUID = 1L; public DataModel() { datasource = new ArrayList<>(); this.setRowCount(1000); } //Getter and Setter for rowString and listVals @Override public Car getRowData(String rowKey) { try { for (Car listVal : listVals) { if (listVal.getName().equals(rowKey)) { return listVal; } } } catch (ArrayIndexOutOfBoundsException ex) { ex.printStackTrace(); } return null; } @Override public Object getRowKey(Car car) { return car.getName(); } @Override public List<Car> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) { listVals = new ArrayList<>(); System.out.println("loading"); int end = (count + 50); for (int i = count; i < end && count <= 5000; i++, count++) { listVals.add(new Car("lamborghini" + count, "other" + count)); } datasource.addAll(listVals); return listVals; } public void loadPreCar() { listVals = new ArrayList<>(); System.out.println("loading pre"); int end = backCount; rowString=""; for (int i = backCount - 50; i < end && backCount >= 0; i++, backCount--) { listVals.add(new Car("lamborghini" + i, "other" + i)); } datasource.addAll(0, listVals); for (int i = 0; i < listVals.size(); i++) { Car car = listVals.get(i); rowString = rowString + "<tr class=\"ui-widget-content ui-datatable-even\" role=\"row\" data-ri=\"" + i + "\">" + "<td role=\"gridcell\">" + car.getName() + "</td>" + "<td role=\"gridcell\">" + car.getOther() + "</td>" + "</tr>"; i++; car = listVals.get(i); rowString = rowString + "<tr class=\"ui-widget-content ui-datatable-odd\" role=\"row\" data-ri=\"" + i + "\">" + "<td role=\"gridcell\">" + car.getName() + "</td>" + "<td role=\"gridcell\">" + car.getOther() + "</td>" + "</tr>"; } } }
import java.io.Serializable; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.inject.Named; import org.primefaces.model.LazyDataModel; @Named @SessionScoped public class DataBean implements Serializable { private static final long serialVersionUID = 1L; private String value; LazyDataModel<Car> data = null; public DataBean() { } public LazyDataModel<Car> getData() { return data; } public void setData(LazyDataModel<Car> data) { this.data = data; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @PostConstruct public void init() { data = new DataModel(); } }
<h:form id="mainForm"> <pf:growl id="growl"/> <pf:panel> <pf:dataTable var="car" scrollable="true" liveScroll="true" scrollHeight="300" scrollRows="50" value="#{dataBean.data}" id="carTable" lazy="true" > <pf:column headerText="Name"> <h:outputText value="#{car.name}" /> </pf:column> <pf:column headerText="Other"> <h:outputText value="#{car.other}" /> </pf:column> </pf:dataTable> </pf:panel> <h:inputHidden value="#{dataBean.data.rowString}" id="rowString"/> <pf:remoteCommand name="myRemote" actionListener="#{dataBean.data.loadPreCar()}" oncomplete="addRows()" update="rowString" /> </h:form>
<script type="text/javascript"> /* <![CDATA[ */ var lastScrollTop = 0; var delay = (function() { //Adding delay var timer = 0; return function(callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); $(document).ready(function() { $('#mainForm\\:carTable .ui-datatable-scrollable-body').on('scroll', null, function() { var scrollLocation = $('#mainForm\\:carTable .ui-datatable-scrollable-body').prop('scrollTop'); if (scrollLocation < 10) { var scrollB = $('#mainForm\\:carTable .ui-datatable-scrollable-body') if (scrollB.scrollTop() < lastScrollTop) { delay(function() { myRemote(); }, 300); } lastScrollTop = scrollB.scrollTop(); } }); }); function addRows() { var rows = $('#mainForm\\:carTable .ui-datatable-scrollable-body table tr'); for (i = 0; i < rows.length; i++) { var attrVal = parseInt(rows[i].getAttribute('data-ri')) + 50; rows[i].setAttribute('data-ri', attrVal); } var firstRow = $('#mainForm\\:carTable .ui-datatable-scrollable-body table tr:first'); var rowHeight = firstRow.height(); firstRow.before(document.getElementById('mainForm:rowString').value); var scrollB = $('#mainForm\\:carTable .ui-datatable-scrollable-body') scrollB.scrollTop('' + rowHeight * 50); lastScrollTop = rowHeight * 50; } /* ]]> */ </script>