001package biweekly.parameter; 002 003import java.util.Collection; 004import java.util.Collections; 005import java.util.HashMap; 006import java.util.Map; 007 008import biweekly.ICalVersion; 009import biweekly.util.CaseClasses; 010 011/* 012 Copyright (c) 2013-2015, Michael Angstadt 013 All rights reserved. 014 015 Redistribution and use in source and binary forms, with or without 016 modification, are permitted provided that the following conditions are met: 017 018 1. Redistributions of source code must retain the above copyright notice, this 019 list of conditions and the following disclaimer. 020 2. Redistributions in binary form must reproduce the above copyright notice, 021 this list of conditions and the following disclaimer in the documentation 022 and/or other materials provided with the distribution. 023 024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 034 */ 035 036/** 037 * Defines what level of participation is expected from a calendar user. Note 038 * that this class does not correspond to a particular parameter. The parameter 039 * varies depending on the iCalendar version. 040 * @author Michael Angstadt 041 * @see <a href="http://tools.ietf.org/html/rfc5545#page-25">RFC 5545 p.25-6</a> 042 * @see <a href="http://www.imc.org/pdi/vcal-10.doc">vCal 1.0 p.26-7</a> 043 */ 044public class ParticipationLevel { 045 private static final CaseClasses<ParticipationLevel, String> enums = new CaseClasses<ParticipationLevel, String>(ParticipationLevel.class) { 046 @Override 047 protected ParticipationLevel create(String value) { 048 return new ParticipationLevel(value); 049 } 050 051 @Override 052 protected boolean matches(ParticipationLevel object, String value) { 053 for (String v : object.values.values()) { 054 if (v.equalsIgnoreCase(value)) { 055 return true; 056 } 057 } 058 return false; 059 } 060 }; 061 062 /** 063 * Indicates that the user's participation is required. 064 */ 065 public static final ParticipationLevel REQUIRED; 066 static { 067 Map<ICalVersion, String> values = new HashMap<ICalVersion, String>(); 068 values.put(ICalVersion.V1_0, "REQUIRE"); 069 values.put(ICalVersion.V2_0_DEPRECATED, "REQ-PARTICIPANT"); 070 values.put(ICalVersion.V2_0, "REQ-PARTICIPANT"); 071 REQUIRED = new ParticipationLevel(values); 072 } 073 074 /** 075 * Indicates that the user's participation is optional. 076 */ 077 public static final ParticipationLevel OPTIONAL; 078 static { 079 Map<ICalVersion, String> values = new HashMap<ICalVersion, String>(); 080 values.put(ICalVersion.V1_0, "REQUEST"); 081 values.put(ICalVersion.V2_0_DEPRECATED, "OPT-PARTICIPANT"); 082 values.put(ICalVersion.V2_0, "OPT-PARTICIPANT"); 083 OPTIONAL = new ParticipationLevel(values); 084 } 085 086 /** 087 * Indicates that the user has been notified about the event for 088 * informational purposes only and does not need to attend. 089 */ 090 public static final ParticipationLevel FYI; 091 static { 092 Map<ICalVersion, String> values = new HashMap<ICalVersion, String>(); 093 values.put(ICalVersion.V1_0, "FYI"); 094 values.put(ICalVersion.V2_0_DEPRECATED, "NON-PARTICIPANT"); 095 values.put(ICalVersion.V2_0, "NON-PARTICIPANT"); 096 FYI = new ParticipationLevel(values); 097 } 098 099 private final Map<ICalVersion, String> values; 100 101 private ParticipationLevel(Map<ICalVersion, String> values) { 102 this.values = Collections.unmodifiableMap(values); 103 } 104 105 private ParticipationLevel(String value) { 106 Map<ICalVersion, String> values = new HashMap<ICalVersion, String>(); 107 for (ICalVersion version : ICalVersion.values()) { 108 values.put(version, value); 109 } 110 this.values = Collections.unmodifiableMap(values); 111 } 112 113 /** 114 * Gets the value of the parameter 115 * @param version the version 116 * @return the parameter value 117 */ 118 public String getValue(ICalVersion version) { 119 return values.get(version); 120 } 121 122 /** 123 * Searches for a parameter value that is defined as a static constant in 124 * this class. 125 * @param value the parameter value 126 * @return the object or null if not found 127 */ 128 public static ParticipationLevel find(String value) { 129 return enums.find(value); 130 } 131 132 /** 133 * Searches for a parameter value and creates one if it cannot be found. All 134 * objects are guaranteed to be unique, so they can be compared with 135 * {@code ==} equality. 136 * @param value the parameter value 137 * @return the object 138 */ 139 public static ParticipationLevel get(String value) { 140 return enums.get(value); 141 } 142 143 /** 144 * Gets all of the parameter values that are defined as static constants in 145 * this class. 146 * @return the parameter values 147 */ 148 public static Collection<ParticipationLevel> all() { 149 return enums.all(); 150 } 151}