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