001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.model;
018
019 import java.util.HashMap;
020 import java.util.Map;
021 import java.util.concurrent.atomic.AtomicInteger;
022
023 import javax.xml.bind.annotation.XmlAccessType;
024 import javax.xml.bind.annotation.XmlAccessorType;
025 import javax.xml.bind.annotation.XmlAttribute;
026 import javax.xml.bind.annotation.XmlElement;
027 import javax.xml.bind.annotation.XmlID;
028 import javax.xml.bind.annotation.XmlTransient;
029 import javax.xml.bind.annotation.XmlType;
030 import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
031 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
032
033 /**
034 * Allows an element to have an optional ID specified
035 *
036 * @version $Revision: 765885 $
037 */
038 @XmlType(name = "optionalIdentifiedType")
039 @XmlAccessorType(XmlAccessType.FIELD)
040 public abstract class OptionalIdentifiedType<T extends OptionalIdentifiedType> {
041 @XmlTransient
042 protected static Map<String, AtomicInteger> nodeCounters = new HashMap<String, AtomicInteger>();
043 @XmlAttribute(required = false)
044 @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
045 @XmlID
046 private String id;
047 @XmlElement(required = false)
048 private Description description;
049
050
051 /**
052 * Gets the value of the id property.
053 */
054 public String getId() {
055 return id;
056 }
057
058 /**
059 * Sets the value of the id property.
060 */
061 public void setId(String value) {
062 this.id = value;
063 }
064
065 public Description getDescription() {
066 return description;
067 }
068
069 public void setDescription(Description description) {
070 this.description = description;
071 }
072
073 /**
074 * Returns a short name for this node which can be useful for ID generation or referring to related resources like images
075 *
076 * @return defaults to "node" but derived nodes should overload this to provide a unique name
077 */
078 public String getShortName() {
079 return "node";
080 }
081
082 // Fluent API
083 // -------------------------------------------------------------------------
084
085 /**
086 * Sets the description of this node
087 *
088 * @param id sets the id, use null to not set an id
089 * @param text sets the text description, use null to not set a text
090 * @param lang sets the language for the description, use null to not set a language
091 * @return the builder
092 */
093 public T describe(String id, String text, String lang) {
094 if (id != null) {
095 setId(id);
096 }
097 if (text != null) {
098 if (description == null) {
099 description = new Description();
100 }
101 description.setText(text);
102 }
103 if (lang != null) {
104 if (description == null) {
105 description = new Description();
106 }
107 description.setLang(lang);
108 }
109 return (T) this;
110 }
111
112 /**
113 * Sets the description of this node
114 *
115 * @param text the description
116 * @return the builder
117 * @deprecated will be removed in Camel 2.0
118 */
119 public T description(String text) {
120 if (description == null) {
121 description = new Description();
122 }
123 description.setText(text);
124 return (T) this;
125 }
126
127 /**
128 * Sets the description of this node
129 *
130 * @param text the description
131 * @param lang the language for the description
132 * @return the builder
133 * @deprecated will be removed in Camel 2.0
134 */
135 public T description(String text, String lang) {
136 description(text);
137 description.setLang(lang);
138 return (T) this;
139 }
140
141 /**
142 * Sets the id of this node
143 *
144 * @param id the id
145 * @return the builder
146 */
147 public T id(String id) {
148 setId(id);
149 return (T) this;
150 }
151
152 public String idOrCreate() {
153 if (id == null) {
154 setId(createId());
155 }
156 return getId();
157 }
158
159 // Implementation methods
160 // -------------------------------------------------------------------------
161
162 /**
163 * A helper method to create a new ID for this node
164 */
165 protected String createId() {
166 String key = getShortName();
167 return key + getNodeCounter(key).incrementAndGet();
168 }
169
170 /**
171 * Returns the counter for the given node key, lazily creating one if necessary
172 */
173 protected static synchronized AtomicInteger getNodeCounter(String key) {
174 AtomicInteger answer = nodeCounters.get(key);
175 if (answer == null) {
176 answer = new AtomicInteger(0);
177 nodeCounters.put(key, answer);
178 }
179 return answer;
180 }
181 }