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   * FragmentPreferenceValueList
23   *
24   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
25   * @version $Id$
26   */
27  class FragmentPreferenceValueList extends AbstractList
28  {
29      private FragmentPreferenceImpl preference;
30  
31      FragmentPreferenceValueList(FragmentPreferenceImpl preference)
32      {
33          super();
34          this.preference = preference;
35      }
36  
37      /***
38       * wrapValueStringForAdd
39       *
40       * Wraps and validates preference value string
41       * to be added to this list.
42       *
43       * @param value preference value string to add
44       * @return list element to add
45       */
46      private FragmentPreferenceValue wrapValueStringForAdd(String value)
47      {
48          // only non-null values supported
49          if (value == null)
50          {
51              throw new NullPointerException("Unable to add null to list.");
52          }
53          // wrap preference value string
54          FragmentPreferenceValue preferenceValue = new FragmentPreferenceValue();
55          preferenceValue.setValue(value);
56          return preferenceValue;
57      }
58  
59      /* (non-Javadoc)
60       * @see java.util.List#add(int,java.lang.Object)
61       */
62      public void add(int index, Object element)
63      {
64          // implement for modifiable AbstractList:
65          // validate index
66          if ((index < 0) || (index > preference.accessValues().size()))
67          {
68              throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
69          }
70          // wrap and verify preference value string
71          FragmentPreferenceValue preferenceValue = wrapValueStringForAdd((String)element);
72          // add to underlying ordered list
73          preference.accessValues().add(index, preferenceValue);
74          // set value order in added element
75          if (index > 0)
76          {
77              preferenceValue.setValueOrder(((FragmentPreferenceValue)preference.accessValues().get(index-1)).getValueOrder() + 1);
78          }
79          else
80          {
81              preferenceValue.setValueOrder(0);
82          }
83          // maintain value order in subsequent elements
84          for (int i = index, limit = preference.accessValues().size() - 1; (i < limit); i++)
85          {
86              FragmentPreferenceValue nextPreferenceValue = (FragmentPreferenceValue)preference.accessValues().get(i + 1);
87              if (nextPreferenceValue.getValueOrder() <= preferenceValue.getValueOrder())
88              {
89                  // adjust value order for next element
90                  nextPreferenceValue.setValueOrder(preferenceValue.getValueOrder() + 1);
91                  preferenceValue = nextPreferenceValue;
92              }
93              else
94              {
95                  // value order maintained for remaining list elements
96                  break;
97              }
98          }
99      }
100 
101     /* (non-Javadoc)
102      * @see java.util.List#get(int)
103      */
104     public Object get(int index)
105     {
106         // implement for modifiable AbstractList:
107         // unwrap preference value string
108         return ((FragmentPreferenceValue)preference.accessValues().get(index)).getValue();
109     }
110 
111     /* (non-Javadoc)
112      * @see java.util.List#remove(int)
113      */
114     public Object remove(int index)
115     {
116         // implement for modifiable AbstractList
117         return preference.accessValues().remove(index);
118     }
119 
120     /* (non-Javadoc)
121      * @see java.util.List#set(int,java.lang.Object)
122      */
123     public Object set(int index, Object element)
124     {
125         // implement for modifiable AbstractList:
126         // wrap and verify preference value string
127         FragmentPreferenceValue newPreferenceValue = wrapValueStringForAdd((String)element);
128         // set in underlying ordered list
129         FragmentPreferenceValue preferenceValue = (FragmentPreferenceValue)preference.accessValues().set(index, newPreferenceValue);
130         // set value order in new element
131         newPreferenceValue.setValueOrder(preferenceValue.getValueOrder());
132         // return unwrapped preference value string
133         return preferenceValue.getValue();
134     }
135 
136     /* (non-Javadoc)
137      * @see java.util.List#size()
138      */
139     public int size()
140     {
141         // implement for modifiable AbstractList
142         return preference.accessValues().size();
143     }
144 }