To Kroshenvold:
I think you did not understand my intention. Maybe I should make myself clearer.
It is assumed that Tree and TreeBuilder are in the same package.
As you can see, the Tree constructor and freeze () method has access to the package level. Thus, you cannot create it outside the package, and you also cannot freeze it outside the package.
The only way to do this is to use the build () method. Only TreeBuilder can create a Tree using a build method that is synchronized.
Now I even realized that you can even simplify removing the flag for reading and modify the Tree.addChild () method to also display visibility. Therefore, you get a tree that does not have public mutators, only accessors.
As I said, Tree is not syncing. TreeBuilder is the place where your synchronization takes place. Get to know accessories and mutators. Look where the public and batch modifiers are located, and you will see that the only way to change the tree is when you are in the same package, so only the tree builder can do it.
public class Tree<T extends Filterable>{ private final T data; private Tree<T> parent; private List<Tree<T>> children; private List<FilterChain<T>> filterChain; private boolean readonly = false; Tree(T data) {...} Tree(Tree<T> parent, T data) {...} void addChild(Tree<T> child){ children.add(child); } public List<?> getResults(){ return data.returnResults(filterChain); }
}
public class TreeBuilder<T>{ public synchronized TreeNode createRoot(T data); public synchronized void addSubElement(TreeNode parentNode ,T data); public synchronized void addFilter(TreeNode node, Filter<T> filter); public Tree<T> synchronized build(){ Tree<T> tree= ...
}
Marcin michalski
source share