Coverage Report - org.argouml.kernel.MemberList
 
Classes in this File Line Coverage Branch Coverage Complexity
MemberList
42%
52/121
33%
23/68
3.259
 
 1  
 /* $Id: MemberList.java 17822 2010-01-12 18:47:46Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2004-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.kernel;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.Iterator;
 44  
 import java.util.List;
 45  
 import java.util.ListIterator;
 46  
 
 47  
 import org.apache.log4j.Logger;
 48  
 import org.argouml.uml.ProjectMemberModel;
 49  
 import org.argouml.uml.cognitive.ProjectMemberTodoList;
 50  
 import org.argouml.uml.diagram.ArgoDiagram;
 51  
 import org.argouml.uml.diagram.ProjectMemberDiagram;
 52  
 
 53  
 /**
 54  
  * List of ProjectMembers. <p>
 55  
  * 
 56  
  * <p>The project members are grouped into 4 categories: 
 57  
  * model, diagrams, the todo item list and the profile configuration. <p>
 58  
  *
 59  
  * <p>The purpose of these categories is to make sure that members are read 
 60  
  * and written in the correct order. 
 61  
  * 
 62  
  * <p>When reading the todo items it will fail if the diagrams elements or model
 63  
  * elements have not yet been read that they refer to. When reading diagrams
 64  
  * that will fail if the model elements don't yet exist that they refer to.
 65  
  * When loading the model that may fail if the correct profile has not been
 66  
  * loaded.
 67  
  * 
 68  
  * <p>Hence, the save (and therefore load) order is profile, model, diagrams,
 69  
  * todo items.
 70  
  *
 71  
  * <p>This implementation supports only one profile configuration, one model
 72  
  * member, multiple diagram members, one todo list member.
 73  
  * 
 74  
  * <p>Comments by mvw: <p>
 75  
  * This class should be reworked to be independent 
 76  
  * of the org.argouml.uml package. That can be done by extending the 
 77  
  * ProjectMember interface with functions returning the sorting order, 
 78  
  * and if multiple entries of the same type are allowed. <p>
 79  
  * 
 80  
  * In preparation, this class is made simpler by deprecating 
 81  
  * all operations that are not part of the List interface.
 82  
  * 
 83  
  * @author Bob Tarling
 84  
  */
 85  6942
 class MemberList implements List<ProjectMember> {
 86  
 
 87  
     /**
 88  
      * Logger.
 89  
      */
 90  900
     private static final Logger LOG = Logger.getLogger(MemberList.class);
 91  
 
 92  
     private AbstractProjectMember model;
 93  
 
 94  968
     private List<ProjectMemberDiagram> diagramMembers = 
 95  
         new ArrayList<ProjectMemberDiagram>(10);
 96  
 
 97  
     private AbstractProjectMember todoList;
 98  
     private AbstractProjectMember profileConfiguration;
 99  
 
 100  
     /**
 101  
      * The constructor.
 102  
      */
 103  968
     public MemberList() {
 104  968
         LOG.info("Creating a member list");
 105  968
     }
 106  
 
 107  
     public synchronized boolean add(ProjectMember member) {
 108  
 
 109  5974
         if (member instanceof ProjectMemberModel) {
 110  
             // Always put the model at the top
 111  968
             model = (AbstractProjectMember) member;
 112  968
             return true;
 113  5006
         } else if (member instanceof ProjectMemberTodoList) {
 114  
             // otherwise add the diagram at the start
 115  1922
             setTodoList((AbstractProjectMember) member);
 116  1922
             return true;
 117  3084
         } else if (member instanceof ProfileConfiguration) {
 118  968
             profileConfiguration = (AbstractProjectMember) member;
 119  968
             return true;
 120  2116
         } else if (member instanceof ProjectMemberDiagram) {
 121  
             // otherwise add the diagram at the start
 122  2116
             return diagramMembers.add((ProjectMemberDiagram) member);
 123  
         }
 124  0
         return false;
 125  
     }
 126  
 
 127  
     public synchronized boolean remove(Object member) {
 128  38
         LOG.info("Removing a member");
 129  38
         if (member instanceof ArgoDiagram) {
 130  38
             return removeDiagram((ArgoDiagram) member);
 131  
         }
 132  0
         ((AbstractProjectMember) member).remove();
 133  0
         if (model == member) {
 134  0
             model = null;
 135  0
             return true;
 136  0
         } else if (todoList == member) {
 137  0
             LOG.info("Removing todo list");
 138  0
             setTodoList(null);
 139  0
             return true;
 140  0
         } else if (profileConfiguration == member) {
 141  0
             LOG.info("Removing profile configuration");
 142  0
             profileConfiguration = null;
 143  0
             return true;
 144  
         } else {
 145  0
             final boolean removed = diagramMembers.remove(member);
 146  0
             if (!removed) {
 147  0
                 LOG.warn("Failed to remove diagram member " + member);
 148  
             }
 149  0
             return removed;
 150  
         }
 151  
     }
 152  
 
 153  
     public synchronized Iterator<ProjectMember> iterator() {
 154  0
         return buildOrderedMemberList().iterator();
 155  
     }
 156  
 
 157  
     public synchronized ListIterator<ProjectMember> listIterator() {
 158  0
         return buildOrderedMemberList().listIterator();
 159  
     }
 160  
 
 161  
     public synchronized ListIterator<ProjectMember> listIterator(int arg0) {
 162  0
         return buildOrderedMemberList().listIterator(arg0);
 163  
     }
 164  
 
 165  
     /**
 166  
      * @return the list of members in the order that they need to be written 
 167  
      *         out in.
 168  
      */
 169  
     private List<ProjectMember> buildOrderedMemberList() {
 170  0
         List<ProjectMember> temp = 
 171  
             new ArrayList<ProjectMember>(size());
 172  0
         if (profileConfiguration != null) {
 173  0
             temp.add(profileConfiguration);
 174  
         }
 175  0
         if (model != null) {
 176  0
             temp.add(model);
 177  
         }
 178  0
         temp.addAll(diagramMembers);
 179  0
         if (todoList != null) {
 180  0
             temp.add(todoList);
 181  
         }
 182  0
         return temp;
 183  
     }
 184  
     
 185  
     private boolean removeDiagram(ArgoDiagram d) {
 186  38
         for (ProjectMemberDiagram pmd : diagramMembers) {
 187  44
             if (pmd.getDiagram() == d) {
 188  38
                 pmd.remove();
 189  38
                 diagramMembers.remove(pmd);
 190  38
                 return true;
 191  
             }
 192  
         }
 193  0
         LOG.debug("Failed to remove diagram " + d);
 194  0
         return false;
 195  
     }
 196  
 
 197  
     public synchronized int size() {
 198  6928
         int size = diagramMembers.size();
 199  6928
         if (model != null) {
 200  6928
             ++size;
 201  
         }
 202  6928
         if (todoList != null) {
 203  4038
             ++size;
 204  
         }
 205  6928
         if (profileConfiguration != null) {
 206  6928
             ++size;
 207  
         }
 208  6928
         return size;
 209  
     }
 210  
 
 211  
     public synchronized boolean contains(Object member) {
 212  0
         if (todoList == member) {
 213  0
             return true;
 214  
         }
 215  0
         if (model == member) {
 216  0
             return true;
 217  
         }
 218  0
         if (profileConfiguration == member) {
 219  0
             return true;
 220  
         }
 221  0
         return diagramMembers.contains(member);
 222  
     }
 223  
 
 224  
     public synchronized void clear() {
 225  68
         LOG.info("Clearing members");
 226  68
         if (model != null) {
 227  68
             model.remove();
 228  
         }
 229  68
         if (todoList != null) {
 230  68
             todoList.remove();
 231  
         }
 232  68
         if (profileConfiguration != null) {
 233  68
             profileConfiguration.remove();
 234  
         }
 235  68
         Iterator membersIt = diagramMembers.iterator();
 236  204
         while (membersIt.hasNext()) {
 237  136
             ((AbstractProjectMember) membersIt.next()).remove();
 238  
         }
 239  68
         diagramMembers.clear();
 240  68
     }
 241  
 
 242  
     public synchronized ProjectMember get(int i) {
 243  968
         if (model != null) {
 244  0
             if (i == 0) {
 245  0
                 return model;
 246  
             }
 247  0
             --i;
 248  
         }
 249  
 
 250  968
         if (i == diagramMembers.size()) {
 251  968
             if (todoList != null) {
 252  0
                 return todoList;
 253  
             } else {
 254  968
                 return profileConfiguration;
 255  
             }
 256  
         }
 257  
         
 258  0
         if (i == (diagramMembers.size() + 1)) {
 259  0
             return profileConfiguration;
 260  
         }
 261  
 
 262  0
         return diagramMembers.get(i);
 263  
     }
 264  
 
 265  
     public synchronized boolean isEmpty() {
 266  0
         return size() == 0;
 267  
     }
 268  
 
 269  
     public synchronized ProjectMember[] toArray() {
 270  0
         ProjectMember[] temp = new ProjectMember[size()];
 271  0
         int pos = 0;
 272  0
         if (model != null) {
 273  0
             temp[pos++] = model;
 274  
         }
 275  0
         for (ProjectMemberDiagram d : diagramMembers) {
 276  0
             temp[pos++] = d;
 277  
         }
 278  0
         if (todoList != null) {
 279  0
             temp[pos++] = todoList;
 280  
         }
 281  0
         if (profileConfiguration != null) {
 282  0
             temp[pos++] = profileConfiguration;
 283  
         }
 284  0
         return temp;
 285  
     }
 286  
 
 287  
     private void setTodoList(AbstractProjectMember member) {
 288  1922
         LOG.info("Setting todoList to " + member);
 289  1922
         todoList = member;
 290  1922
     }
 291  
 
 292  
     public <T> T[] toArray(T[] a) {
 293  0
         throw new UnsupportedOperationException();
 294  
     }
 295  
 
 296  
     public boolean containsAll(Collection< ? > arg0) {
 297  0
         throw new UnsupportedOperationException();
 298  
     }
 299  
 
 300  
     public boolean addAll(Collection< ? extends ProjectMember> arg0) {
 301  0
         throw new UnsupportedOperationException();
 302  
     }
 303  
 
 304  
     public boolean addAll(int arg0, Collection< ? extends ProjectMember> arg1) {
 305  0
         throw new UnsupportedOperationException();
 306  
     }
 307  
 
 308  
     public boolean removeAll(Collection< ? > arg0) {
 309  0
         throw new UnsupportedOperationException();
 310  
     }
 311  
 
 312  
     public boolean retainAll(Collection< ? > arg0) {
 313  0
         throw new UnsupportedOperationException();
 314  
     }
 315  
 
 316  
     public ProjectMember set(int arg0, ProjectMember arg1) {
 317  0
         throw new UnsupportedOperationException();
 318  
     }
 319  
 
 320  
     public void add(int arg0, ProjectMember arg1) {
 321  0
         throw new UnsupportedOperationException();
 322  
     }
 323  
 
 324  
     public ProjectMember remove(int arg0) {
 325  0
         throw new UnsupportedOperationException();
 326  
     }
 327  
 
 328  
     public int indexOf(Object arg0) {
 329  0
         throw new UnsupportedOperationException();
 330  
     }
 331  
 
 332  
     public int lastIndexOf(Object arg0) {
 333  0
         throw new UnsupportedOperationException();
 334  
     }
 335  
 
 336  
     public List<ProjectMember> subList(int arg0, int arg1) {
 337  0
         throw new UnsupportedOperationException();
 338  
     }
 339  
 
 340  
 
 341  
 }