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.Iterator;
027 import java.util.NoSuchElementException;
028 import net.jcip.annotations.Immutable;
029 import org.jboss.dna.graph.property.Name;
030
031 /**
032 * An immutable version of a property that has exactly 1 value. This is done for efficiency of the in-memory representation, since
033 * 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 BasicSingleValueProperty extends BasicProperty {
039
040 protected final Object value;
041
042 /**
043 * Create a property with a single value
044 *
045 * @param name the property name
046 * @param value the property value (which may be null)
047 */
048 public BasicSingleValueProperty( Name name,
049 Object value ) {
050 super(name);
051 this.value = value;
052 }
053
054 /**
055 * {@inheritDoc}
056 */
057 public boolean isEmpty() {
058 return false;
059 }
060
061 /**
062 * {@inheritDoc}
063 */
064 public boolean isMultiple() {
065 return false;
066 }
067
068 /**
069 * {@inheritDoc}
070 */
071 public boolean isSingle() {
072 return true;
073 }
074
075 /**
076 * {@inheritDoc}
077 */
078 public int size() {
079 return 1;
080 }
081
082 /**
083 * {@inheritDoc}
084 *
085 * @see org.jboss.dna.graph.property.Property#getFirstValue()
086 */
087 public Object getFirstValue() {
088 return value;
089 }
090
091 /**
092 * {@inheritDoc}
093 */
094 public Iterator<Object> iterator() {
095 return new ValueIterator();
096 }
097
098 protected class ValueIterator implements Iterator<Object> {
099
100 private boolean done = false;
101
102 protected ValueIterator() {
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public boolean hasNext() {
109 return !done;
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public Object next() {
116 if (!done) {
117 done = true;
118 return BasicSingleValueProperty.this.value;
119 }
120 throw new NoSuchElementException();
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public void remove() {
127 throw new UnsupportedOperationException();
128 }
129
130 }
131
132 }