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