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 javax.xml.bind;
018    
019    import java.io.Serializable;
020    
021    import javax.xml.namespace.QName;
022    
023    public class JAXBElement<T> implements Serializable {
024    
025        private static final long serialVersionUID = 1L;
026    
027        public final static class GlobalScope {
028        }
029    
030        protected final QName name;
031        protected final Class<T> declaredType;
032        protected final Class scope;
033        protected T value;
034        protected boolean nil;
035    
036        public JAXBElement(QName name, Class<T> declaredType, Class scope, T value) {
037            this.nil = false;
038            if (declaredType == null || name == null) {
039                throw new IllegalArgumentException();
040            }
041            this.declaredType = declaredType;
042            if (scope == null) {
043                scope = GlobalScope.class;
044            }
045            this.scope = scope;
046            this.name = name;
047            setValue(value);
048        }
049    
050        public JAXBElement(QName name, Class<T> declaredType, T value) {
051            this(name, declaredType, GlobalScope.class, value);
052        }
053    
054        public Class<T> getDeclaredType() {
055            return declaredType;
056        }
057    
058        public QName getName() {
059            return name;
060        }
061    
062        public void setValue(T value) {
063            this.value = value;
064        }
065    
066        public T getValue() {
067            return value;
068        }
069    
070        public Class getScope() {
071            return scope;
072        }
073    
074        public boolean isNil() {
075            return value == null || nil;
076        }
077    
078        public void setNil(boolean nil) {
079            this.nil = nil;
080        }
081    
082        public boolean isGlobalScope() {
083            return scope == GlobalScope.class;
084        }
085    
086        public boolean isTypeSubstituted() {
087            if (value == null) {
088                return false;
089            } else {
090                return value.getClass() != declaredType;
091            }
092        }
093    }