001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.directory.shared.ldap.util;
020    
021    
022    import javax.naming.NamingEnumeration;
023    import javax.naming.NamingException;
024    import javax.naming.directory.Attribute;
025    import javax.naming.directory.Attributes;
026    
027    
028    /**
029     * Document me!
030     *
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev$, $Date$
033     */
034    public class ImmutableAttributesWrapper implements Attributes
035    {
036        private final Attributes wrapped;
037    
038    
039        public ImmutableAttributesWrapper( Attributes wrapped )
040        {
041            this.wrapped = wrapped;
042        }
043    
044    
045        public boolean isCaseIgnored()
046        {
047            return wrapped.isCaseIgnored();
048        }
049    
050    
051        public int size()
052        {
053            return wrapped.size();
054        }
055    
056    
057        public Attribute get( String attrID )
058        {
059            return new ImmutableAttributeWrapper( wrapped.get( attrID ) );
060        }
061    
062    
063        public NamingEnumeration<? extends Attribute> getAll()
064        {
065            return new ImmutableEnumeration( wrapped.getAll() );
066        }
067    
068    
069        public NamingEnumeration<String> getIDs()
070        {
071            return wrapped.getIDs();
072        }
073    
074    
075        public Attribute put( String attrID, Object val )
076        {
077            throw new UnsupportedOperationException( "Putting attributes not supported by immutable attributes" );
078        }
079    
080    
081        public Attribute put( Attribute attr )
082        {
083            throw new UnsupportedOperationException( "Putting attributes not supported by immutable attributes" );
084        }
085    
086    
087        public Attribute remove( String attrID )
088        {
089            throw new UnsupportedOperationException( "Removing attributes not supported by immutable attributes" );
090        }
091    
092    
093        @SuppressWarnings ( { "CloneDoesntCallSuperClone" } )
094        public Object clone()
095        {
096            throw new IllegalStateException( "Now why would you want to clone() an immutable object in the first place." );
097        }
098    
099    
100        class ImmutableEnumeration implements NamingEnumeration
101        {
102            private NamingEnumeration wrappedEnum;
103    
104    
105            public ImmutableEnumeration( NamingEnumeration<? extends Attribute> all )
106            {
107                wrappedEnum = all;
108            }
109    
110    
111            public Attribute next() throws NamingException
112            {
113                return new ImmutableAttributeWrapper( ( Attribute ) wrappedEnum.next() );
114            }
115    
116    
117            public boolean hasMore() throws NamingException
118            {
119                return wrappedEnum.hasMore();
120            }
121    
122    
123            public void close() throws NamingException
124            {
125                wrappedEnum.close();
126            }
127    
128    
129            public boolean hasMoreElements()
130            {
131                return wrappedEnum.hasMoreElements();
132            }
133    
134    
135            public Attribute nextElement()
136            {
137                return new ImmutableAttributeWrapper( ( Attribute ) wrappedEnum.nextElement() );
138            }
139        }
140    }