001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.property.basic;
025    
026    import java.util.Arrays;
027    import java.util.Iterator;
028    import java.util.List;
029    import net.jcip.annotations.Immutable;
030    import org.jboss.dna.common.util.CheckArg;
031    import org.jboss.dna.graph.property.Name;
032    
033    /**
034     * An immutable version of a property that has 2 or more values. This is done for efficiency of the in-memory representation,
035     * since many properties will have just a single value, while others will have multiple values.
036     * 
037     * @author Randall Hauch
038     */
039    @Immutable
040    public class BasicMultiValueProperty extends BasicProperty {
041    
042        private final List<Object> values;
043    
044        /**
045         * Create a property with 2 or more values. Note that the supplied list may be modifiable, as this object does not expose any
046         * means for modifying the contents.
047         * 
048         * @param name the property name
049         * @param values the property values
050         * @throws IllegalArgumentException if the values is null or does not have at least 2 values
051         */
052        public BasicMultiValueProperty( Name name,
053                                        List<Object> values ) {
054            super(name);
055            CheckArg.isNotNull(values, "values");
056            CheckArg.hasSizeOfAtLeast(values, 2, "values");
057            this.values = values;
058        }
059    
060        /**
061         * Create a property with 2 or more values.
062         * 
063         * @param name the property name
064         * @param values the property values
065         * @throws IllegalArgumentException if the values is null or does not have at least 2 values
066         */
067        public BasicMultiValueProperty( Name name,
068                                        Object... values ) {
069            super(name);
070            CheckArg.isNotNull(values, "values");
071            CheckArg.hasSizeOfAtLeast(values, 2, "values");
072            this.values = Arrays.asList(values);
073        }
074    
075        /**
076         * {@inheritDoc}
077         */
078        public boolean isEmpty() {
079            return false;
080        }
081    
082        /**
083         * {@inheritDoc}
084         */
085        public boolean isMultiple() {
086            return true;
087        }
088    
089        /**
090         * {@inheritDoc}
091         */
092        public boolean isSingle() {
093            return false;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public int size() {
100            return values.size();
101        }
102    
103        /**
104         * {@inheritDoc}
105         * 
106         * @see org.jboss.dna.graph.property.Property#getFirstValue()
107         */
108        public Object getFirstValue() {
109            return values.get(0);
110        }
111    
112        /**
113         * {@inheritDoc}
114         */
115        public Iterator<Object> iterator() {
116            return new ReadOnlyIterator(values.iterator());
117        }
118    
119        protected class ReadOnlyIterator implements Iterator<Object> {
120    
121            private final Iterator<Object> values;
122    
123            protected ReadOnlyIterator( Iterator<Object> values ) {
124                assert values != null;
125                this.values = values;
126            }
127    
128            /**
129             * {@inheritDoc}
130             */
131            public boolean hasNext() {
132                return values.hasNext();
133            }
134    
135            /**
136             * {@inheritDoc}
137             */
138            public Object next() {
139                return values.next();
140            }
141    
142            /**
143             * {@inheritDoc}
144             */
145            public void remove() {
146                throw new UnsupportedOperationException();
147            }
148    
149        }
150    }