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: 699876 $
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        public T description(String text) {
085            if (description == null) {
086                description = new Description();
087            }
088            description.setText(text);
089            return (T) this;
090        }
091    
092        public T description(String text, String lang) {
093            description(text);
094            description.setLang(lang);
095            return (T) this;
096        }
097    
098        public T id(String id) {
099            setId(id);
100            return (T) this;
101        }
102    
103        public String idOrCreate() {
104            if (id == null) {
105                setId(createId());
106            }
107            return getId();
108        }
109    
110        // Implementation methods
111        // -------------------------------------------------------------------------
112    
113        /**
114         * A helper method to create a new ID for this node
115         */
116        protected String createId() {
117            String key = getShortName();
118            return key + getNodeCounter(key).incrementAndGet();
119        }
120    
121        /**
122         * Returns the counter for the given node key, lazily creating one if necessary
123         */
124        protected static synchronized AtomicInteger getNodeCounter(String key) {
125            AtomicInteger answer = nodeCounters.get(key);
126            if (answer == null) {
127                answer = new AtomicInteger(0);
128                nodeCounters.put(key, answer);
129            }
130            return answer;
131        }
132    }