Package com.luxoft.dnepr.courses.regular.unit6.familytree.impl

Examples of com.luxoft.dnepr.courses.regular.unit6.familytree.impl.PersonImpl


        PersonImpl father = new PersonImpl("Vasiliy", Gender.MALE, "ukrainian", 40, null, null);
        PersonImpl mother = new PersonImpl("Ludmila", Gender.FEMALE, "ukrainian", 39, null, null);
        person.setFather(father);
        person.setMother(mother);

        FamilyTree familyTree = FamilyTreeImpl.create(person);

        IOUtils.save("file.txt", familyTree);
        FamilyTree fromJSON = IOUtils.load("file.txt");

        new File("file.txt").delete();

        Assert.assertEquals(familyTree.getRoot(), fromJSON.getRoot());
    }
View Full Code Here


            PersonImpl grandFather = new PersonImpl("Vitalii Ivanovich", Gender.MALE, "ukrainian", 67, null, null);
            person.setMother(mother);
            person.setFather(father);
            father.setFather(grandFather);

            FamilyTree familyTree = FamilyTreeImpl.create(person);

            IOUtils.save(baos, familyTree);

            try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
                FamilyTree familyTreeFromJSON = IOUtils.load(bais);
                Assert.assertEquals(familyTree.getRoot(), familyTreeFromJSON.getRoot());
            }

        }
    }
View Full Code Here

        jsonBuilder.append(CLOSE_BRACKET);
        return jsonBuilder.toString();
    }

    public static FamilyTree parseJSON(String JSON) {
        FamilyTree familyTree = null;
        String hash = JSONUtils.getHashWithoutBrackets(JSON);
        String[] keyValues = JSONUtils.getKeyValuePairs(hash);
        if (keyValues.length == 0) {
            return null;
        }
View Full Code Here

        oos.writeUTF(JSON);
    }

    private void readObject(ObjectInputStream ois) throws IOException {
        String JSON = ois.readUTF();
        FamilyTree familyTree = JSONFamilyTree.parseJSON(JSON);
        this.root = familyTree.getRoot();
    }
View Full Code Here

    private IOUtils() {
    }

    public static FamilyTree load(String filename) {
        File file = new File(filename);
        FamilyTree familyTree = null;
        if (file.exists() && file.isFile()) {
            try (FileInputStream fis = new FileInputStream(file)) {
                familyTree = load(fis);
            } catch (IOException e) {
                e.printStackTrace();
View Full Code Here

        }
        return familyTree;
    }

    public static FamilyTree load(InputStream is) {
        FamilyTree familyTree = null;
        try (ObjectInputStream ois = new ObjectInputStream(is)) {
            familyTree = (FamilyTree) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
View Full Code Here

        }
        return null;
    }

    public static FamilyTree load(InputStream is) {
        FamilyTree tree = null;
        try (ObjectInputStream objectInput = new ObjectInputStream(is)) {
            tree = (FamilyTree) objectInput.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
View Full Code Here

        IOUtils.save("kot5.json", FamilyTreeImpl.create(kot5));
        IOUtils.save("kot6.json", FamilyTreeImpl.create(kot6));
        IOUtils.save("kot7.json", FamilyTreeImpl.create(kot7));
        IOUtils.save("nullkot.json", null);

        FamilyTree tree1 = IOUtils.load("kot1.json");
        assertEquals(kot1, tree1.getRoot());

        FamilyTree tree5 = IOUtils.load("kot5.json");
        assertEquals(kot5, tree5.getRoot());
        assertEquals(tree5, FamilyTreeImpl.create(kot5));

        FamilyTree tree6 = IOUtils.load("kot6.json");
        assertEquals(kot6, tree6.getRoot());
        assertEquals(tree6, FamilyTreeImpl.create(kot6));

        FamilyTree tree7 = IOUtils.load("kot7.json");
        assertEquals(kot7, tree7.getRoot());

        FamilyTree nulltree = IOUtils.load("nullkot.json");
        assertTrue(nulltree == null);

    }
View Full Code Here

    private static final String ROOT_ATTRIBUTE = "root";

    public static String getJSONString(FamilyTree familyTree) {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.append(OPEN_BRACKET);
        Person root = familyTree.getRoot();
        if (root != null) {
            jsonBuilder.append(QUOTE_SYMBOL + ROOT_ATTRIBUTE + QUOTE_SYMBOL);
            jsonBuilder.append(KEYVALUE_DELIMITER);
            jsonBuilder.append(JSONPerson.getJSONString(root));
        }
View Full Code Here

        }
        String keyValue = keyValues[0];
        String key = JSONUtils.getKey(keyValue);
        String value = JSONUtils.getValue(keyValue);
        if (key.equals(ROOT_ATTRIBUTE) && JSONUtils.isHashItem(value)) {
            Person person = JSONPerson.parseJSON(value);
            familyTree = FamilyTreeImpl.create(person);
        }
        return familyTree;
    }
View Full Code Here

TOP

Related Classes of com.luxoft.dnepr.courses.regular.unit6.familytree.impl.PersonImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.