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.concurrent.atomic.AtomicInteger;
020    
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlAttribute;
024    import javax.xml.bind.annotation.XmlElement;
025    import javax.xml.bind.annotation.XmlID;
026    import javax.xml.bind.annotation.XmlTransient;
027    import javax.xml.bind.annotation.XmlType;
028    import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
029    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
030    
031    /**
032     * Allows an element to have an optional ID specified
033     *
034     * @version $Revision: 41788 $
035     */
036    @XmlType(name = "optionalIdentifiedType")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public abstract class OptionalIdentifiedType<T extends OptionalIdentifiedType> {
039        @XmlTransient
040        protected static AtomicInteger nodeCounter = new AtomicInteger(1);
041        @XmlAttribute(required = false)
042        @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
043        @XmlID
044        private String id;
045        @XmlElement(required = false)
046        private Description description;
047    
048    
049        /**
050         * Gets the value of the id property.
051         *
052         * @return possible object is
053         *         {@link String }
054         */
055        public String getId() {
056            return id;
057        }
058    
059        /**
060         * Sets the value of the id property.
061         *
062         * @param value allowed object is
063         *              {@link String }
064         */
065        public void setId(String value) {
066            this.id = value;
067        }
068    
069        public Description getDescription() {
070            return description;
071        }
072    
073        public void setDescription(Description description) {
074            this.description = description;
075        }
076    
077        // Fluent API
078        // -------------------------------------------------------------------------
079        public T description(String text) {
080            if (description == null) {
081                description = new Description();
082            }
083            description.setText(text);
084            return (T) this;
085        }
086    
087        public T description(String text, String lang) {
088            description(text);
089            description.setLang(lang);
090            return (T) this;
091        }
092    
093        public T id(String id) {
094            setId(id);
095            return (T) this;
096        }
097    
098        public String idOrCreate() {
099            if (id == null) {
100                setId("node" + nodeCounter.incrementAndGet());
101            }
102            return getId();
103        }
104    }