View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.feed.synd;
18  
19  import com.sun.syndication.feed.module.DCSubjectI;
20  
21  import java.util.AbstractList;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /***
26   * List implementation for SyndCategory elements. To be directly used by the SyndFeed
27   * and SyndEntry classes only.
28   * <p>
29   * It acts as a facade on top of the DCSubject elements of the underlying list
30   * and remains in synch with it. It is possible to work on either list, the categories
31   * one or the subjects one and they remain in synch.
32   * <p>
33   * This is necessary because the SyndFeed categories are just a convenience to access
34   * the DublinCore subjects.
35   * <P>
36   * All this mess to avoid making DCSubject implement SyndCategoryI (which it would be odd).
37   * <p>
38   * @author Alejandro Abdelnur
39   *
40   */
41  public class SyndCategoryListFacade extends AbstractList {
42      private List _subjects;
43  
44      /***
45       * Default constructor. Creates and empty list.
46       */
47      public SyndCategoryListFacade() {
48          this(new ArrayList());
49      }
50  
51      /***
52       * Creates a facade list of categories on top the given subject list.
53       * <P>
54       * @param subjects the list of subjects to create the facade.
55       *
56       */
57      public SyndCategoryListFacade(List subjects) {
58          _subjects = subjects;
59      }
60  
61      /***
62       * Gets the category by index.
63       * <p>
64       * @param index the index position to retrieve the category.
65       * @return the SyndCategory in position index, <b>null</b> if none.
66       *
67       */
68      public Object get(int index) {
69          return new SyndCategory((DCSubjectI) _subjects.get(index));
70      }
71  
72      /***
73       * Returns the size of the list.
74       * <p>
75       * @return the size of the list.
76       *
77       */
78      public int size() {
79          return _subjects.size();
80      }
81  
82      /***
83       * Sets a category in an existing position in the list.
84       * <p>
85       * @param index position to set the category.
86       * @param obj the SyndCategory object to set.
87       * @return the SyndCategory object that is being replaced, <b>null</b> if none.
88       *
89       */
90      public Object set(int index,Object obj) {
91          SyndCategory sCat = (SyndCategory) obj;
92          DCSubjectI subject = (sCat!=null) ? sCat.getSubject() : null;
93          subject = (DCSubjectI) _subjects.set(index,subject);
94          return (subject!=null) ? new SyndCategory(subject) : null;
95      }
96  
97      /***
98      * Adds a category to the list.
99      * <p>
100     * @param index position to add the category.
101     * @param obj the SyndCategory object to add.
102      *
103      */
104     public void add(int index,Object obj) {
105         SyndCategory sCat = (SyndCategory) obj;
106         DCSubjectI subject = (sCat!=null) ? sCat.getSubject() : null;
107         _subjects.add(index,subject);
108     }
109 
110     /***
111      * Removes a category element from a specific position.
112      * <p>
113      * @param index position to remove the category from.
114      * @return the SyndCategory being removed from position index, <b>null</b> if none.
115      *
116      */
117     public Object remove(int index) {
118         DCSubjectI subject = (DCSubjectI) _subjects.remove(index);
119         return (subject!=null) ? new SyndCategory(subject) : null;
120     }
121 
122     /***
123      * Returns a list with the DCSubjectI elements of the SyndCategory list facade.
124      * To be used by the SyndFeed class only.
125      * <p>
126      * @param cList the list with SyndCategory elements to convert to subject list.
127      * @return a list with DCSubjectI elements corresponding to the categories in the given list.
128      *
129      */
130     public static List convertElementsSyndCategoryToSubject(List cList) {
131         List sList = null;
132         if (cList!=null) {
133             sList = new ArrayList();
134             for (int i=0;i<cList.size();i++) {
135                 SyndCategory sCat = (SyndCategory) cList.get(i);
136                 DCSubjectI subject = null;
137                 if (sCat!=null) {
138                     subject = sCat.getSubject();
139                 }
140                 sList.add(subject);
141             }
142         }
143         return sList;
144     }
145 
146 }