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.configuration.parameters;
034:
035: import com.flexive.shared.configuration.Parameter;
036: import com.flexive.shared.configuration.ParameterData;
037: import com.flexive.shared.configuration.ParameterPath;
038: import com.flexive.shared.configuration.ParameterScope;
039:
040: import java.io.Serializable;
041: import java.util.ArrayList;
042: import java.util.List;
043:
044: /**
045: * Generic parameter implementation.
046: *
047: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: * @param <T> value type of the parameter
049: */
050: public abstract class ParameterImpl<T, TParameter extends ParameterImpl<T, TParameter>>
051: implements Parameter<T>, Serializable {
052: private static final long serialVersionUID = -2727270342944531283L;
053:
054: /** Parameter data */
055: private ParameterData<T> data;
056: private boolean frozen;
057:
058: /** Registers all known parameters */
059: private static final List<Parameter> registeredParameters = new ArrayList<Parameter>();
060:
061: /**
062: * Create a new parameter with the given data
063: * @param parameter parameter data
064: * @param registerParameter if the parameter should be registered in the static parameter table
065: * (don't do this for non-static parameter declarations)
066: */
067: protected ParameterImpl(ParameterData<T> parameter,
068: boolean registerParameter) {
069: this .data = parameter;
070: synchronized (registeredParameters) {
071: // TODO: need to rethink this - probably not very useful in its current state
072: if (registerParameter
073: && !registeredParameters.contains(this )) {
074: registeredParameters.add(this );
075: }
076: }
077: }
078:
079: /** {@inheritDoc} */
080: public ParameterData<T> getData() {
081: return this .data;
082: }
083:
084: /** {@inheritDoc} */
085: public T getDefaultValue() {
086: return data.getDefaultValue();
087: }
088:
089: /** {@inheritDoc} */
090: public String getKey() {
091: return data.getKey();
092: }
093:
094: /** {@inheritDoc} */
095: public ParameterPath getPath() {
096: return data.getPath();
097: }
098:
099: /** {@inheritDoc} */
100: public ParameterScope getScope() {
101: return data.getPath().getScope();
102: }
103:
104: /** {@inheritDoc} */
105: public boolean isValid(T value) {
106: // by default all parameters of the given type are valid
107: return true;
108: }
109:
110: /** {@inheritDoc} */
111: public String getDatabaseValue(T value) {
112: return value != null ? String.valueOf(value) : null;
113: }
114:
115: public Parameter<T> setData(ParameterData<T> parameterData) {
116: if (!frozen) {
117: this .data = parameterData;
118: } else {
119: throw new IllegalStateException("Parameter " + this
120: + " frozen and may not be changed.");
121: }
122: return this ;
123: }
124:
125: public Parameter<T> freeze() {
126: frozen = true;
127: return this ;
128: }
129:
130: /** {@inheritDoc} */
131: @Override
132: public String toString() {
133: return "[" + data.toString() + "]";
134: }
135: }
|