View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.property.basic;
25  
26  import java.util.Iterator;
27  import java.util.NoSuchElementException;
28  import net.jcip.annotations.Immutable;
29  import org.modeshape.graph.property.Name;
30  
31  /**
32   * An immutable version of a property that has exactly 1 value. This is done for efficiency of the in-memory representation, since
33   * many properties will have just a single value, while others will have multiple values.
34   */
35  @Immutable
36  public class BasicSingleValueProperty extends BasicProperty {
37      private static final long serialVersionUID = 1L;
38  
39      protected final Object value;
40  
41      /**
42       * Create a property with a single value
43       * 
44       * @param name the property name
45       * @param value the property value (which may be null)
46       */
47      public BasicSingleValueProperty( Name name,
48                                       Object value ) {
49          super(name);
50          this.value = value;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public boolean isEmpty() {
57          return false;
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      public boolean isMultiple() {
64          return false;
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      public boolean isSingle() {
71          return true;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      public int size() {
78          return 1;
79      }
80  
81      /**
82       * {@inheritDoc}
83       * 
84       * @see org.modeshape.graph.property.Property#getFirstValue()
85       */
86      public Object getFirstValue() {
87          return value;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public Iterator<Object> iterator() {
94          return new ValueIterator();
95      }
96  
97      protected class ValueIterator implements Iterator<Object> {
98  
99          private boolean done = false;
100 
101         protected ValueIterator() {
102         }
103 
104         /**
105          * {@inheritDoc}
106          */
107         public boolean hasNext() {
108             return !done;
109         }
110 
111         /**
112          * {@inheritDoc}
113          */
114         public Object next() {
115             if (!done) {
116                 done = true;
117                 return BasicSingleValueProperty.this.value;
118             }
119             throw new NoSuchElementException();
120         }
121 
122         /**
123          * {@inheritDoc}
124          */
125         public void remove() {
126             throw new UnsupportedOperationException();
127         }
128 
129     }
130 
131 }