/* * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * This source code is provided to illustrate the usage of a given feature * or technique and has been deliberately simplified. Additional steps * required for a production-quality application, such as security checks, * input validation and proper error handling, might not be present in * this sample code. */ package stream.data; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * DataUtilities is a singleton helper class that creates test data. This class * uses jaxws API to read xml into java object, which includes 3 list of data 1. * Customers 2. Products 3. Vendors Note in this demo here we don't use stream * API to change data, if we use stream API to populate source data, we should * be aware this is not free and not thread-safe. */ interface XMLList { public List getList(); } /** * class for PRODUCTS_XML's root */ @XmlRootElement(name = "products") class Products implements XMLList { /** * node products only contains product list */ @XmlElement(name = "product") private ArrayList products; /** * product list */ @Override public List getList() { return products; } } /** * class for VENDORS_XML's root */ @XmlRootElement(name = "vendors") class Vendors implements XMLList { /** * node Vendors only contains vendor list */ @Override public List getList() { return vendors; } /** * vendor list */ @XmlElement(name = "vendor") private ArrayList vendors; } /** * class for CUSTOMERS_XML's root */ @XmlRootElement(name = "customers") class Customers implements XMLList { /** * node customers only contains customer list */ @Override public List getList() { return customers; } /** * customer list */ @XmlElement(name = "customer") private ArrayList customers; } public class DataUtilities { /** * xml file name for Customers object */ private final static String CUSTOMERS_XML = "Customers.xml"; /** * xml file name for Products object */ private final static String PRODUCTS_XML = "Products.xml"; /** * xml file name for Products object */ private final static String VENDORS_XML = "Vendors.xml"; /** * Lazy initiate static inner class for */ private static class UtilitiesHolder { static DataUtilities instance = new DataUtilities(); } /** * private constructor is needed for singleton */ private DataUtilities() { } //products private List productList; //customer private List customerList; //vendors private List vendorList; /** * Lazy initial to get product list * * @return initiated product list */ public List getProductList() { if (productList == null) { createLists(); } return productList; } /** * Lazy initial to get customer list * * @return initiated customer list */ public List getCustomerList() { if (customerList == null) { createLists(); } return customerList; } /** * Lazy initial to get vendor list * * @return initiated vendor list */ public List getVendorList() { if (vendorList == null) { createLists(); } return vendorList; } /** * convert xml files to java objects */ private void createLists() { customerList = xmlToObject(Customers.class, CUSTOMERS_XML); productList = xmlToObject(Products.class, PRODUCTS_XML); vendorList = xmlToObject(Vendors.class, VENDORS_XML); } /** * Convert a xml file to instance of class by given file name * * @param clazz to be converted java object's class * @param file xml file name * @return a list of object that xml's root contains */ private > List xmlToObject(Class clazz, String file) { try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller um = context.createUnmarshaller(); @SuppressWarnings("unchecked") V cs = (V) um.unmarshal(new File(file)); return cs.getList(); } catch (JAXBException ex) { throw new RuntimeException(ex); } } /** * static factory method for singleton * * @return a DataUtitlities signgleton instance */ public static DataUtilities getUtilities() { return UtilitiesHolder.instance; } }