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