001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.structure;
034:
035: import org.apache.commons.lang.StringUtils;
036:
037: import java.io.Serializable;
038: import java.util.ArrayList;
039: import java.util.Collections;
040: import java.util.List;
041:
042: /**
043: * Option for structure elements (groups, properties, assignments)
044: *
045: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
046: */
047: public class FxStructureOption implements Serializable {
048:
049: private static final long serialVersionUID = 737017384585248182L;
050: public final static String OPTION_MULTILANG = "MULTILANG";
051: public final static String OPTION_SHOW_OVERVIEW = "SHOW.OVERVIEW";
052: public final static String OPTION_HTML_EDITOR = "HTML.EDITOR";
053: public final static String OPTION_SEARCHABLE = "SEARCHABLE";
054:
055: public final static String OPTION_MULTILINE = "MULTILINE";
056: public final static String VALUE_TRUE = "1";
057:
058: public final static String VALUE_FALSE = "0";
059: private String key;
060: protected boolean overrideable;
061: private String value;
062: private boolean set;
063:
064: /**
065: * Ctor
066: *
067: * @param key key identifying the option
068: * @param overrideable is the option overridable in assignments
069: * @param set is the option set? (non-existing options are returned as not-set options!)
070: * @param value the options value
071: */
072: public FxStructureOption(String key, boolean overrideable,
073: boolean set, String value) {
074: this .key = key.toUpperCase();
075: this .overrideable = overrideable;
076: this .set = set;
077: this .value = value;
078: }
079:
080: /**
081: * Copy Constructor
082: *
083: * @param o an FxStructureOption
084: */
085: public FxStructureOption(FxStructureOption o) {
086: this .key = o.key;
087: this .overrideable = o.overrideable;
088: this .set = o.set;
089: this .value = o.value;
090: }
091:
092: /**
093: * Get the option key
094: *
095: * @return option key
096: */
097: public String getKey() {
098: return key;
099: }
100:
101: /**
102: * Is the option overrideable (in assignments)?
103: *
104: * @return option overrideable (in assignments)?
105: */
106: public boolean isOverrideable() {
107: return overrideable;
108: }
109:
110: /**
111: * Is the option set, will return <code>false</code> if an unknown option is requested
112: *
113: * @return if option is set, will return <code>false</code> if an unknown option is requested
114: */
115: public boolean isSet() {
116: return set;
117: }
118:
119: /**
120: * Get the value assigned to the option
121: *
122: * @return value assigned to the option
123: */
124: public String getValue() {
125: return value;
126: }
127:
128: /**
129: * Convenience method to check if value is set to true
130: *
131: * @return if value is set to true
132: */
133: public boolean isValueTrue() {
134: return VALUE_TRUE.equals(value);
135: }
136:
137: /**
138: * Check if an option is set for the requested key
139: *
140: * @param key option key
141: * @param options the available options
142: * @return if an option is set for the requested key
143: */
144: public static boolean hasOption(String key,
145: List<FxStructureOption> options) {
146: if (key != null)
147: key = key.trim().toUpperCase();
148: if (key == null || key.length() == 0 || options == null
149: || options.size() == 0)
150: return false;
151: for (FxStructureOption option : options)
152: if (key.equals(option.getKey()))
153: return true;
154: return false;
155: }
156:
157: /**
158: * Get a default entry for an unknown option
159: *
160: * @param key the key
161: * @return an unknown option
162: */
163: private static FxStructureOption getUnknownOption(String key) {
164: return new FxStructureOption(key, false, false, "");
165: }
166:
167: /**
168: * Get an option entry for the given key, if the key is invalid or not found a <code>FxStructureOption</code> object
169: * will be returned with <code>set</code> set to <code>false</code>, overrideable set to <code>false</code> and value
170: * set to an empty String.
171: *
172: * @param key option key
173: * @param options the available options
174: * @return the found option or an object that indicates that the option is not set
175: */
176: public static FxStructureOption getOption(String key,
177: List<FxStructureOption> options) {
178: if (key != null)
179: key = key.trim().toUpperCase();
180: if (key == null || key.length() == 0 || options == null
181: || options.size() == 0)
182: return getUnknownOption(StringUtils.defaultString(key));
183: for (FxStructureOption option : options)
184: if (key.equals(option.getKey()))
185: return option;
186: return getUnknownOption(key);
187: }
188:
189: /**
190: * Get a list of empty options
191: *
192: * @param capacity desired capacity
193: * @return list of empty options
194: */
195: public static List<FxStructureOption> getEmptyOptionList(
196: int capacity) {
197: return new ArrayList<FxStructureOption>(capacity);
198: }
199:
200: /**
201: * Set or add a String value in a list of options
202: *
203: * @param options list of existing options
204: * @param key option key
205: * @param overrideable should the option be overrideable?
206: * @param value String value to set for the option
207: */
208: public static void setOption(List<FxStructureOption> options,
209: String key, boolean overrideable, String value) {
210: synchronized (options) {
211: if (hasOption(key, options)) {
212: FxStructureOption opt = getOption(key, options);
213: opt.overrideable = overrideable;
214: opt.value = value;
215: opt.set = true;
216: return;
217: }
218: FxStructureOption opt = new FxStructureOption(key,
219: overrideable, true, value);
220: options.add(opt);
221: }
222: }
223:
224: /**
225: * Set or add a boolean value in a list of options
226: *
227: * @param options list of existing options
228: * @param key option key
229: * @param overrideable should the option be overrideable?
230: * @param value boolean value to set for the option (will be converted internally to a String)
231: */
232: public static void setOption(List<FxStructureOption> options,
233: String key, boolean overrideable, boolean value) {
234: setOption(options, key, overrideable, value ? VALUE_TRUE
235: : VALUE_FALSE);
236: }
237:
238: /**
239: * Clear the option with the given key - removing it from the list if it exists
240: *
241: * @param options list options to clear the option for
242: * @param key key of the option to remove
243: */
244: public static void clearOption(List<FxStructureOption> options,
245: String key) {
246: if (key != null)
247: key = key.trim().toUpperCase();
248: if (key == null || key.length() == 0 || options == null
249: || options.size() == 0)
250: return;
251: synchronized (options) {
252: for (FxStructureOption option : options)
253: if (key.equals(option.getKey())) {
254: options.remove(option);
255: return;
256: }
257: }
258: }
259:
260: /**
261: * Get a copy of the a list of options
262: *
263: * @param options list to clone
264: * @return cloned list of options
265: */
266: public static List<FxStructureOption> cloneOptions(
267: List<FxStructureOption> options) {
268: if (options == null)
269: return null;
270: else {
271: ArrayList<FxStructureOption> clone = new ArrayList<FxStructureOption>(
272: options.size());
273: for (FxStructureOption o : options) {
274: clone.add(new FxStructureOption(o));
275: }
276: return clone;
277: }
278: }
279:
280: /**
281: * Convert a list of options to an unmodifieable list
282: *
283: * @param options list to convert
284: * @return unmodifieable list of options
285: */
286: public static List<FxStructureOption> getUnmodifieableOptions(
287: List<FxStructureOption> options) {
288: return Collections.unmodifiableList(options);
289: }
290:
291: /**
292: * Checks for equality
293: * @return if the option's values equal this
294: */
295: @Override
296: public boolean equals(Object o) {
297: if (o == null || !(o instanceof FxStructureOption))
298: return false;
299: if (o == this )
300: return true;
301: final FxStructureOption other = (FxStructureOption) o;
302: if (!this .key.equals(other.key)
303: || !this .value.equals(other.value)
304: || this .overrideable != other.overrideable
305: || this .set != other.set)
306: return false;
307: return true;
308: }
309:
310: /**
311: * {@inheritDoc}
312: */
313: @Override
314: public int hashCode() {
315: int result;
316: result = (key != null ? key.hashCode() : 0);
317: result = 31 * result + (overrideable ? 1 : 0);
318: result = 31 * result + (value != null ? value.hashCode() : 0);
319: result = 31 * result + (set ? 1 : 0);
320: return result;
321: }
322: }
|