package org.csstudio.alarm.diirt.datasource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.csstudio.alarm.beast.Messages;
import org.csstudio.alarm.beast.client.AlarmTreeItem;
import org.csstudio.alarm.beast.client.AlarmTreeLeaf;
import org.csstudio.alarm.beast.client.AlarmTreeRoot;

public class CompositeAlarmTreeRoot extends AlarmTreeRoot {
    private static final long serialVersionUID = 8577521363075445385L;

    private final List<AlarmTreeRoot> subRoots;

    public CompositeAlarmTreeRoot(String name) {
        super(name, -1);
        subRoots = Collections.synchronizedList(new ArrayList<>());
    }

    @Override
    public synchronized int getLeafCount() {
        int count = 0;
        synchronized (subRoots) {
            for (AlarmTreeRoot child : subRoots) count += child.getLeafCount();
        }
        return count;
    }

    public synchronized void addAlarmTreeRoot(AlarmTreeRoot child) {
        subRoots.add(child);
        addChild(child);
    }

    public synchronized void removeAlarmTreeRoot(AlarmTreeRoot child) {
      subRoots.remove(child);
      removeChild(child);
  }

    @Override
    public void addLeavesToList(final List<AlarmTreeLeaf> leaves) {
        synchronized (subRoots) {
            subRoots.stream().forEach(root -> root.addLeavesToList(leaves));
        }
    }

    @Override
    public synchronized int getElementCount() {
        int count = 1;   // count the root as well
        synchronized (subRoots) {
            for (AlarmTreeRoot root : subRoots) count += root.getElementCount();
        }
        return count;
    }

    @Override
    public AlarmTreeRoot getRoot() {
        return this;
    }

    @Override
    public AlarmTreeItem getParent() {
        return null;
    }

    @Override
    public AlarmTreeItem getChild(final int i) {
        return subRoots.get(i);
    }

    @Override
    public AlarmTreeItem getChild(final String name) {
        synchronized (subRoots) {
            for (final AlarmTreeRoot root : subRoots) {
                if (root.getName().equals(name)) return root;
            }
        }
        return null;
    }

    @Override
    public String getConfigTime() {
        return Messages.Unknown;
    }

    @Override
    public void acknowledge(final boolean acknowledge) {
        synchronized (subRoots) {
            for (final AlarmTreeRoot root : subRoots) {
                root.acknowledge(acknowledge);
            }
        }
    }

    @Override
    public synchronized boolean maximizeSeverity() {
        // This method synchronizes on the components before calling the parent.
        synchronized (subRoots) {
            return super.maximizeSeverity();
        }
    }
}

@@ -60,114 +60,14 @@
 til.
-Array
 List;%0A
-import java.util.Collections;%0Aimport java.util.List;%0A%0Aimport org.csstudio.alarm.beast.Messages;
 %0Aimp
@@ -362,57 +362,8 @@
 L;%0A%0A
-    private final List%3CAlarmTreeRoot%3E subRoots;%0A%0A
     
@@ -436,76 +436,8 @@
 1);%0A
-        subRoots = Collections.synchronizedList(new ArrayList%3C%3E());%0A
     
@@ -510,167 +510,35 @@
     
-int count = 0;%0A        synchronized (subRoots) %7B%0A            for (AlarmTreeRoot child : subRoots) count += child.getLeafCount();%0A        %7D%0A        return count
+return super.getLeafCount()
 ;%0A  
@@ -615,37 +615,8 @@
 ) %7B%0A
-        subRoots.add(child);%0A
     
@@ -721,36 +721,8 @@
 %7B%0A  
-    subRoots.remove(child);%0A
     
@@ -743,16 +743,18 @@
 child);%0A
+  
   %7D%0A%0A   
@@ -845,83 +845,12 @@
    s
-ynchronized (subRoots) %7B%0A            subRoots.stream().forEach(root -%3E root
+uper
 .add
@@ -869,28 +869,17 @@
 (leaves)
-);%0A        %7D
+;
 %0A    %7D%0A%0A
@@ -952,196 +952,38 @@
     
-int count = 1;   // count the root as well%0A        synchronized (subRoots) %7B%0A            for (AlarmTreeRoot root : subRoots) count += root.getElementCount();%0A        %7D%0A        return count
+return super.getElementCount()
 ;%0A  
@@ -1231,18 +1231,20 @@
 n su
-bRoots.get
+per.getChild
 (i);
@@ -1332,195 +1332,35 @@
     
-synchronized (subRoots) %7B%0A            for (final AlarmTreeRoot root : subRoots) %7B%0A                if (root.getName().equals(name)) return root;%0A            %7D%0A        %7D%0A        return null
+return super.getChild(name)
 ;%0A  
@@ -1433,24 +1433,29 @@
 urn 
-Messages.Unknown
+super.getConfigTime()
 ;%0A  
@@ -1543,109 +1543,12 @@
    s
-ynchronized (subRoots) %7B%0A            for (final AlarmTreeRoot root : subRoots) %7B%0A                root
+uper
 .ack
@@ -1574,32 +1574,8 @@
 e);%0A
-            %7D%0A        %7D%0A
     
@@ -1648,127 +1648,8 @@
 ) %7B%0A
-        // This method synchronizes on the components before calling the parent.%0A        synchronized (subRoots) %7B%0A    
     
@@ -1689,18 +1689,8 @@
 ();%0A
-        %7D%0A
     

Improve implementation of class CompositeAlarmTreeRoot (as it inherits from AlarmTreeRoot it does not need its own list of subroots).