vendredi 6 janvier 2017

Java EMF declaring new class

I'm using Neon.1a Release (4.6.1) and trying to create a new class using Modeler with my aird file. Currently I have a Composite model:

enter image description here

How can I model my new class Filesystem? This class returns a Node type which is a Directory.

enter image description here

or

enter image description here

This is my current code in Java:

public class FileSystem {
    private static Node _root;

    private FileSystem() {}

    public static Node getFileSystem() {
        if (_root == null) {
            _root = new Directory("/");
        }
        return _root;
    }
}

public abstract class Node {

    public void add(Node newNode) {
        throw new UnsupportedOperationException();
    }

    public void name() {
        throw new UnsupportedOperationException();
    }

    public void ls()  {
        throw new UnsupportedOperationException();
    }
}


import java.util.ArrayList;

public class Directory extends Node {

    private String m_name;
    private ArrayList m_files = new ArrayList();

    public void name() {}

    public Directory(String name) { m_name = name; }

    public void add(Node obj)
    {
            m_files.add(obj);
    }


    public void ls() {
        System.out.println(CompositeMain.g_indent + m_name);
        CompositeMain.g_indent.append("   ");
        for (int i = 0; i < m_files.size(); ++i)
        {           
            Node obj = (Node)m_files.get(i);
            obj.ls();
        }
        CompositeMain.g_indent.setLength(CompositeMain.g_indent.length() - 3);      
    }
}

public class File extends Node {

    private String m_name;

    public File(String name) { m_name = name; }

    public void name() {}

    public void ls() 
    {
        System.out.println(CompositeMain.g_indent + m_name);
    }
}

public class CompositeMain {

    public static StringBuffer g_indent = new StringBuffer();

    public static void main(String[] args) {
        Node root = FileSystem.getFileSystem();
        Node one = new Directory("dir1");
        Node two = new Directory("dir2");
        Node thr = new Directory("dir3");
        Node a = new File("a");
        Node b = new File("b"); 
        Node c = new File("c"); 
        Node d = new File("d");
        Node e = new File("e");

        root.add(one);
        root.add(two);
        one.add(a);
        one.add(two);
        one.add(b);
        two.add(c);
        two.add(d);
        two.add(thr);
        thr.add(e);
        root.ls();

    }
}

Aucun commentaire:

Enregistrer un commentaire