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.impl.ObjectBean;
20  import com.sun.syndication.feed.module.DCSubjectImpl;
21  import com.sun.syndication.feed.module.DCSubject;
22  import com.sun.syndication.feed.impl.ObjectBean;
23  
24  import java.util.AbstractList;
25  import java.util.List;
26  import java.util.ArrayList;
27  import java.io.Serializable;
28  
29  /***
30   * Bean for categories of SyndFeedImpl feeds and entries.
31   * <p>
32   * @author Alejandro Abdelnur
33   *
34   */
35  public class SyndCategoryImpl implements Serializable,SyndCategory {
36      private ObjectBean _objBean;
37      private DCSubject _subject;
38  
39      /***
40       * For implementations extending SyndContentImpl to be able to use the ObjectBean functionality
41       * with extended interfaces.
42       * <p>
43       * @param subject the DC subject to wrap.
44       */
45      SyndCategoryImpl(DCSubject subject) {
46          _objBean = new ObjectBean(SyndCategory.class,this);
47          _subject = subject;
48      }
49  
50      /***
51       * Creates a deep 'bean' clone of the object.
52       * <p>
53       * @return a clone of the object.
54       * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
55       *
56       */
57      public Object clone() throws CloneNotSupportedException {
58          return _objBean.clone();
59      }
60  
61      /***
62       * Indicates whether some other object is "equal to" this one as defined by the Object equals() method.
63       * <p>
64       * @param other he reference object with which to compare.
65       * @return <b>true</b> if 'this' object is equal to the 'other' object.
66       *
67       */
68      public boolean equals(Object other) {
69          return _objBean.equals(other);
70      }
71  
72      /***
73       * Returns a hashcode value for the object.
74       * <p>
75       * It follows the contract defined by the Object hashCode() method.
76       * <p>
77       * @return the hashcode of the bean object.
78       *
79       */
80      public int hashCode() {
81          return _objBean.hashCode();
82      }
83  
84      /***
85       * Returns the String representation for the object.
86       * <p>
87       * @return String representation for the object.
88       *
89       */
90      public String toString() {
91          return _objBean.toString();
92      }
93  
94      /***
95       * Package private constructor, used by SyndCategoryListFacade.
96       * <p>
97       * @return the DC subject being wrapped.
98       *
99       */
100     DCSubject getSubject() {
101         return _subject;
102     }
103 
104     /***
105      * Default constructor. All properties are set to <b>null</b>.
106      * <p>
107      *
108      */
109     public SyndCategoryImpl() {
110         this(new DCSubjectImpl());
111     }
112 
113     /***
114      * Returns the category name.
115      * <p>
116      * @return the category name, <b>null</b> if none.
117      *
118      */
119     public String getName() {
120         return _subject.getValue();
121     }
122 
123     /***
124      * Sets the category name.
125      * <p>
126      * @param name the category name to set, <b>null</b> if none.
127      *
128      */
129     public void setName(String name) {
130         _subject.setValue(name);
131     }
132 
133     /***
134      * Returns the category taxonomy URI.
135      * <p>
136      * @return the category taxonomy URI, <b>null</b> if none.
137      *
138      */
139     public String getTaxonomyUri() {
140         return _subject.getTaxonomyUri();
141     }
142 
143     /***
144      * Sets the category taxonomy URI.
145      * <p>
146      * @param taxonomyUri the category taxonomy URI to set, <b>null</b> if none.
147      *
148      */
149     public void setTaxonomyUri(String taxonomyUri) {
150         _subject.setTaxonomyUri(taxonomyUri);
151     }
152 
153 }
154 
155 
156 /***
157  * List implementation for SyndCategoryImpl elements. To be directly used by the SyndFeedImpl
158  * and SyndEntryImpl classes only.
159  * <p>
160  * It acts as a facade on top of the DCSubjectImpl elements of the underlying list
161  * and remains in synch with it. It is possible to work on either list, the categories
162  * one or the subjects one and they remain in synch.
163  * <p>
164  * This is necessary because the SyndFeedImpl categories are just a convenience to access
165  * the DublinCore subjects.
166  * <P>
167  * All this mess to avoid making DCSubjectImpl implement SyndCategory (which it would be odd).
168  * <p>
169  * @author Alejandro Abdelnur
170  *
171  */
172 class SyndCategoryListFacade extends AbstractList {
173     private List _subjects;
174 
175     /***
176      * Default constructor. Creates and empty list.
177      */
178     public SyndCategoryListFacade() {
179         this(new ArrayList());
180     }
181 
182     /***
183      * Creates a facade list of categories on top the given subject list.
184      * <P>
185      * @param subjects the list of subjects to create the facade.
186      *
187      */
188     public SyndCategoryListFacade(List subjects) {
189         _subjects = subjects;
190     }
191 
192     /***
193      * Gets the category by index.
194      * <p>
195      * @param index the index position to retrieve the category.
196      * @return the SyndCategoryImpl in position index, <b>null</b> if none.
197      *
198      */
199     public Object get(int index) {
200         return new SyndCategoryImpl((DCSubject) _subjects.get(index));
201     }
202 
203     /***
204      * Returns the size of the list.
205      * <p>
206      * @return the size of the list.
207      *
208      */
209     public int size() {
210         return _subjects.size();
211     }
212 
213     /***
214      * Sets a category in an existing position in the list.
215      * <p>
216      * @param index position to set the category.
217      * @param obj the SyndCategoryImpl object to set.
218      * @return the SyndCategoryImpl object that is being replaced, <b>null</b> if none.
219      *
220      */
221     public Object set(int index,Object obj) {
222         SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
223         DCSubject subject = (sCat!=null) ? sCat.getSubject() : null;
224         subject = (DCSubject) _subjects.set(index,subject);
225         return (subject!=null) ? new SyndCategoryImpl(subject) : null;
226     }
227 
228     /***
229     * Adds a category to the list.
230     * <p>
231     * @param index position to add the category.
232     * @param obj the SyndCategoryImpl object to add.
233      *
234      */
235     public void add(int index,Object obj) {
236         SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
237         DCSubject subject = (sCat!=null) ? sCat.getSubject() : null;
238         _subjects.add(index,subject);
239     }
240 
241     /***
242      * Removes a category element from a specific position.
243      * <p>
244      * @param index position to remove the category from.
245      * @return the SyndCategoryImpl being removed from position index, <b>null</b> if none.
246      *
247      */
248     public Object remove(int index) {
249         DCSubject subject = (DCSubject) _subjects.remove(index);
250         return (subject!=null) ? new SyndCategoryImpl(subject) : null;
251     }
252 
253     /***
254      * Returns a list with the DCSubject elements of the SyndCategoryImpl list facade.
255      * To be used by the SyndFeedImpl class only.
256      * <p>
257      * @param cList the list with SyndCategoryImpl elements to convert to subject list.
258      * @return a list with DCSubject elements corresponding to the categories in the given list.
259      *
260      */
261     public static List convertElementsSyndCategoryToSubject(List cList) {
262         List sList = null;
263         if (cList!=null) {
264             sList = new ArrayList();
265             for (int i=0;i<cList.size();i++) {
266                 SyndCategoryImpl sCat = (SyndCategoryImpl) cList.get(i);
267                 DCSubject subject = null;
268                 if (sCat!=null) {
269                     subject = sCat.getSubject();
270                 }
271                 sList.add(subject);
272             }
273         }
274         return sList;
275     }
276 
277 }