View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.om.page.impl;
18  
19  import java.util.AbstractList;
20  
21  /***
22   * SecurityConstraintDefList
23   *
24   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
25   * @version $Id$
26   */
27  class SecurityConstraintDefList extends AbstractList
28  {
29      private SecurityConstraintsDefImpl constraintsDef;
30  
31      SecurityConstraintDefList(SecurityConstraintsDefImpl constraintsDef)
32      {
33          super();
34          this.constraintsDef = constraintsDef;
35      }
36  
37      /***
38       * validateConstraintForAdd
39       *
40       * Validates constraint to be added to this list.
41       *
42       * @param constraint constraint to add
43       * @return list element to add
44       */
45      private PageSecuritySecurityConstraintImpl validateConstraintForAdd(PageSecuritySecurityConstraintImpl constraint)
46      {
47          // validate constraint instance class
48          if (constraint == null)
49          {
50              throw new NullPointerException("Unable to add null to list.");
51          }
52          return constraint;
53      }
54  
55      /* (non-Javadoc)
56       * @see java.util.List#add(int,java.lang.Object)
57       */
58      public void add(int index, Object element)
59      {
60          // implement for modifiable AbstractList:
61          // validate index
62          if ((index < 0) || (index > constraintsDef.accessConstraintDefs().size()))
63          {
64              throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
65          }
66          // verify constraint
67          PageSecuritySecurityConstraintImpl constraint = validateConstraintForAdd((PageSecuritySecurityConstraintImpl)element);
68          // add to underlying ordered list
69          constraintsDef.accessConstraintDefs().add(index, constraint);
70          // set apply order in added element
71          if (index > 0)
72          {
73              constraint.setApplyOrder(((PageSecuritySecurityConstraintImpl)constraintsDef.accessConstraintDefs().get(index-1)).getApplyOrder() + 1);
74          }
75          else
76          {
77              constraint.setApplyOrder(0);
78          }
79          // maintain apply order in subsequent elements
80          for (int i = index, limit = constraintsDef.accessConstraintDefs().size() - 1; (i < limit); i++)
81          {
82              PageSecuritySecurityConstraintImpl nextConstraint = (PageSecuritySecurityConstraintImpl)constraintsDef.accessConstraintDefs().get(i + 1);
83              if (nextConstraint.getApplyOrder() <= constraint.getApplyOrder())
84              {
85                  // adjust apply order for next element
86                  nextConstraint.setApplyOrder(constraint.getApplyOrder() + 1);
87                  constraint = nextConstraint;
88              }
89              else
90              {
91                  // apply order maintained for remaining list elements
92                  break;
93              }
94          }
95      }
96  
97      /* (non-Javadoc)
98       * @see java.util.List#get(int)
99       */
100     public Object get(int index)
101     {
102         // implement for modifiable AbstractList
103         return constraintsDef.accessConstraintDefs().get(index);
104     }
105 
106     /* (non-Javadoc)
107      * @see java.util.List#remove(int)
108      */
109     public Object remove(int index)
110     {
111         // implement for modifiable AbstractList
112         return constraintsDef.accessConstraintDefs().remove(index);
113     }
114 
115     /* (non-Javadoc)
116      * @see java.util.List#set(int,java.lang.Object)
117      */
118     public Object set(int index, Object element)
119     {
120         // implement for modifiable AbstractList:
121         // verify constraint
122         PageSecuritySecurityConstraintImpl newConstraint = validateConstraintForAdd((PageSecuritySecurityConstraintImpl)element);
123         // set in underlying ordered list
124         PageSecuritySecurityConstraintImpl constraint = (PageSecuritySecurityConstraintImpl)constraintsDef.accessConstraintDefs().set(index, newConstraint);
125         // set apply order in new element
126         newConstraint.setApplyOrder(constraint.getApplyOrder());
127         // return constraint
128         return constraint;
129     }
130 
131     /* (non-Javadoc)
132      * @see java.util.List#size()
133      */
134     public int size()
135     {
136         // implement for modifiable AbstractList
137         return constraintsDef.accessConstraintDefs().size();
138     }
139 }