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: 672271 $
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 * @return possible object is
055 * {@link String }
056 */
057 public String getId() {
058 return id;
059 }
060
061 /**
062 * Sets the value of the id property.
063 *
064 * @param value allowed object is
065 * {@link String }
066 */
067 public void setId(String value) {
068 this.id = value;
069 }
070
071 public Description getDescription() {
072 return description;
073 }
074
075 public void setDescription(Description description) {
076 this.description = description;
077 }
078
079 /**
080 * Returns a short name for this node which can be useful for ID generation or referring to related resources like images
081 *
082 * @return defaults to "node" but derived nodes should overload this to provide a unique name
083 */
084 public String getShortName() {
085 return "node";
086 }
087
088 // Fluent API
089 // -------------------------------------------------------------------------
090 public T description(String text) {
091 if (description == null) {
092 description = new Description();
093 }
094 description.setText(text);
095 return (T) this;
096 }
097
098 public T description(String text, String lang) {
099 description(text);
100 description.setLang(lang);
101 return (T) this;
102 }
103
104 public T id(String id) {
105 setId(id);
106 return (T) this;
107 }
108
109 public String idOrCreate() {
110 if (id == null) {
111 setId(createId());
112 }
113 return getId();
114 }
115
116 // Implementation methods
117 // -------------------------------------------------------------------------
118
119 /**
120 * A helper method to create a new ID for this node
121 */
122 protected String createId() {
123 String key = getShortName();
124 return key + getNodeCounter(key).incrementAndGet();
125 }
126
127 /**
128 * Returns the counter for the given node key, lazily creating one if necessary
129 */
130 protected static synchronized AtomicInteger getNodeCounter(String key) {
131 AtomicInteger answer = nodeCounters.get(key);
132 if (answer == null) {
133 answer = new AtomicInteger(0);
134 nodeCounters.put(key, answer);
135 }
136 return answer;
137 }
138 }