001    package biweekly.component.marshaller;
002    
003    import java.util.Collection;
004    import java.util.List;
005    
006    import biweekly.component.ICalComponent;
007    import biweekly.property.ICalProperty;
008    
009    /*
010     Copyright (c) 2013, Michael Angstadt
011     All rights reserved.
012    
013     Redistribution and use in source and binary forms, with or without
014     modification, are permitted provided that the following conditions are met: 
015    
016     1. Redistributions of source code must retain the above copyright notice, this
017     list of conditions and the following disclaimer. 
018     2. Redistributions in binary form must reproduce the above copyright notice,
019     this list of conditions and the following disclaimer in the documentation
020     and/or other materials provided with the distribution. 
021    
022     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
023     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
024     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
025     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
026     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
027     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
030     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032     */
033    
034    /**
035     * Base class for iCalendar component marshallers.
036     * @param <T> the component class
037     * @author Michael Angstadt
038     */
039    public abstract class ICalComponentMarshaller<T extends ICalComponent> {
040            protected final Class<T> clazz;
041            protected final String componentName;
042    
043            /**
044             * Creates a new component marshaller.
045             * @param clazz the component's class
046             * @param componentName the component's name (e.g. "VEVENT")
047             */
048            public ICalComponentMarshaller(Class<T> clazz, String componentName) {
049                    this.clazz = clazz;
050                    this.componentName = componentName;
051            }
052    
053            /**
054             * Gets the component class.
055             * @return the component class.
056             */
057            public Class<T> getComponentClass() {
058                    return clazz;
059            }
060    
061            /**
062             * Gets the component's name.
063             * @return the compent's name (e.g. "VEVENT")
064             */
065            public String getComponentName() {
066                    return componentName;
067            }
068    
069            /**
070             * Creates a new instance of the component class that doesn't have any
071             * properties or sub-components.
072             * @return the new instance
073             */
074            public T emptyInstance() {
075                    T component = _newInstance();
076    
077                    //remove any properties/components that were created in the constructor
078                    component.getProperties().clear();
079                    component.getComponents().clear();
080    
081                    return component;
082            }
083    
084            /**
085             * Creates a new instance of the component class.
086             * @return the new instance
087             */
088            protected abstract T _newInstance();
089    
090            /**
091             * Gets the sub-components to marshal. Child classes can override this for
092             * better control over which components are marshalled.
093             * @param component the component
094             * @return the sub-components to marshal
095             */
096            public Collection<ICalComponent> getComponents(T component) {
097                    return component.getComponents().values();
098            }
099    
100            /**
101             * Gets the properties to marshal. Child classes can override this for
102             * better control over which properties are marshalled.
103             * @param component the component
104             * @return the properties to marshal
105             */
106            public List<ICalProperty> getProperties(T component) {
107                    return component.getProperties().values();
108            }
109    }