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: 750806 $
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 DescriptionDefinition 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 DescriptionDefinition getDescription() {
066            return description;
067        }
068    
069        public void setDescription(DescriptionDefinition 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        @SuppressWarnings("unchecked")
094        public T description(String id, String text, String lang) {
095            if (id != null) {
096                setId(id);
097            }
098            if (text != null) {
099                if (description == null) {
100                    description = new DescriptionDefinition();
101                }
102                description.setText(text);
103            }
104            if (lang != null) {
105                if (description == null) {
106                    description = new DescriptionDefinition();
107                }
108                description.setLang(lang);
109            }
110            return (T) this;
111        }
112    
113        /**
114         * Gets the node id, creating one if not already set.
115         */
116        public String idOrCreate() {
117            if (id == null) {
118                setId(createId());
119            }
120            return getId();
121        }
122    
123        // Implementation methods
124        // -------------------------------------------------------------------------
125    
126        /**
127         * A helper method to create a new id for this node
128         */
129        protected String createId() {
130            String key = getShortName();
131            return key + getNodeCounter(key).incrementAndGet();
132        }
133    
134        /**
135         * Returns the counter for the given node key, lazily creating one if necessary
136         */
137        protected static synchronized AtomicInteger getNodeCounter(String key) {
138            AtomicInteger answer = nodeCounters.get(key);
139            if (answer == null) {
140                answer = new AtomicInteger(0);
141                nodeCounters.put(key, answer);
142            }
143            return answer;
144        }
145    }