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