View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.classifications2.impl;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  
24  import org.mycore.datamodel.classifications2.MCRCategory;
25  
26  class MCRCategoryChildList extends ArrayList<MCRCategory> {
27      private static final long serialVersionUID = 5844882597476033744L;
28  
29      private MCRCategory root;
30  
31      private MCRCategory thisCategory;
32  
33      MCRCategoryChildList(MCRCategory root, MCRCategory thisCategory) {
34          super();
35          this.root = root;
36          this.thisCategory = thisCategory;
37      }
38  
39      @Override
40      public void add(int index, MCRCategory element) {
41          super.add(index, MCRCategoryImpl.wrapCategory(element, thisCategory, root));
42      }
43  
44      @Override
45      public boolean add(MCRCategory e) {
46          return super.add(MCRCategoryImpl.wrapCategory(e, thisCategory, root));
47      }
48  
49      @Override
50      public boolean addAll(Collection<? extends MCRCategory> c) {
51          return super.addAll(MCRCategoryImpl.wrapCategories(c, thisCategory, root));
52      }
53  
54      @Override
55      public boolean addAll(int index, Collection<? extends MCRCategory> c) {
56          return super.addAll(index, MCRCategoryImpl.wrapCategories(c, thisCategory, root));
57      }
58  
59      @Override
60      public void clear() {
61          for (int i = 0; i < size(); i++) {
62              removeAncestorReferences(get(i));
63          }
64          super.clear();
65      }
66  
67      @Override
68      public MCRCategory remove(int index) {
69          MCRCategory category = super.remove(index);
70          removeAncestorReferences(category);
71          return category;
72      }
73  
74      @Override
75      public boolean remove(Object o) {
76          boolean removed = super.remove(o);
77          if (removed) {
78              removeAncestorReferences((MCRCategory) o);
79          }
80          return removed;
81      }
82  
83      /**
84       * @param category
85       */
86      private void removeAncestorReferences(MCRCategory category) {
87          if (category instanceof MCRAbstractCategoryImpl) {
88              ((MCRAbstractCategoryImpl) category).parent = null;
89              ((MCRAbstractCategoryImpl) category).root = null;
90          }
91      }
92  
93      @Override
94      protected void removeRange(int fromIndex, int toIndex) {
95          for (int i = fromIndex; i < toIndex; i++) {
96              removeAncestorReferences(get(i));
97          }
98          super.removeRange(fromIndex, toIndex);
99      }
100 
101     @Override
102     public MCRCategory set(int index, MCRCategory element) {
103         MCRCategory category = super.set(index, element);
104         if (category != element) {
105             removeAncestorReferences(category);
106         }
107         return category;
108     }
109 
110 }