001 /*
002 $Id: SpreadList.java,v 1.6 2005/07/19 04:41:52 phk Exp $
003
004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005
006 Redistribution and use of this software and associated documentation
007 ("Software"), with or without modification, are permitted provided
008 that the following conditions are met:
009
010 1. Redistributions of source code must retain copyright
011 statements and notices. Redistributions must also contain a
012 copy of this document.
013
014 2. Redistributions in binary form must reproduce the
015 above copyright notice, this list of conditions and the
016 following disclaimer in the documentation and/or other
017 materials provided with the distribution.
018
019 3. The name "groovy" must not be used to endorse or promote
020 products derived from this Software without prior written
021 permission of The Codehaus. For written permission,
022 please contact info@codehaus.org.
023
024 4. Products derived from this Software may not be called "groovy"
025 nor may "groovy" appear in their names without prior written
026 permission of The Codehaus. "groovy" is a registered
027 trademark of The Codehaus.
028
029 5. Due credit should be given to The Codehaus -
030 http://groovy.codehaus.org/
031
032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043 OF THE POSSIBILITY OF SUCH DAMAGE.
044
045 */
046 package groovy.lang;
047
048 import java.util.AbstractList;
049 import java.util.List;
050
051 import org.codehaus.groovy.runtime.InvokerHelper;
052
053 /**
054 * Spreads a list as individual objects to support the spread operator (*) for lists.
055 * For examples, <pre>
056 * def fn(a, b, c, d) { return a + b + c + d }
057 * println fn(1, 2, 3, 4)
058 *
059 * def x = [10, 100]
060 * def y = [1, *x, 1000, *[10000, 100000]]
061 * assert y == [1, 10, 100, 1000, 10000, 100000]
062 * </pre><br>
063 *
064 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
065 * @author Pilho Kim
066 * @version $Revision: 1.6 $
067 */
068 public class SpreadList extends AbstractList {
069
070 private Object[] contents;
071 private int hashCode;
072
073 /**
074 * Generator.
075 *
076 * @param contents an array of objects to be converted to a SpreadList
077 */
078 public SpreadList(Object[] contents) {
079 this.contents = contents;
080 }
081
082 /**
083 * Returns the object in <code>this</code> of the indicated position.
084 *
085 * @param index the indicated position in <code>this</code>
086 */
087 public Object get(int index) {
088 return contents[index];
089 }
090
091 /**
092 * Returns the size of <code>this</code>.
093 *
094 * @param index the indicated position in <code>this</code>
095 */
096 public int size() {
097 return contents.length;
098 }
099
100 /**
101 * Compares <code>this</code> with another object.
102 *
103 * @param that another object to be compared with <code>this</code>
104 * @return Returns <code>true</code> if this equals to <code>that</code>, <code>false</code> otherwise
105 */
106 public boolean equals(Object that) {
107 if (that instanceof SpreadList) {
108 return equals((SpreadList) that);
109 }
110 return false;
111 }
112
113 /**
114 * Compares <code>this</code> with another spreadlist.
115 *
116 * @param that another spreadlist to be compared with <code>this</code>
117 * @return Returns <code>true</code> if this equals to <code>that</code>, <code>false</code> otherwise
118 */
119 public boolean equals(SpreadList that) {
120 if (contents.length == that.contents.length) {
121 for (int i = 0; i < contents.length; i++) {
122 if (! InvokerHelper.compareEqual(this.contents[i], that.contents[i])) {
123 return false;
124 }
125 }
126 return true;
127 }
128 return false;
129 }
130
131
132 /**
133 * Returns the hash code of <code>this</code>.
134 *
135 * @return Returns the hash code of <code>this</code>
136 */
137 public int hashCode() {
138 if (hashCode == 0) {
139 for (int i = 0; i < contents.length; i++ ) {
140 Object value = contents[i];
141 int hash = (value != null) ? value.hashCode() : 0xbabe;
142 hashCode ^= hash;
143 }
144 if (hashCode == 0) {
145 hashCode = 0xbabe;
146 }
147 }
148 return hashCode;
149 }
150
151 /**
152 * Returns a sublist of <code>this</code> from <code>fromIndex</code> to <code>toIndex</code>.
153 *
154 * @param fromIndex the first index in <code>this</code> to be taken
155 * @param toIndex the last index in <code>this</code> to be taken
156 * @return Returns the sublist of <code>thin</code> in the given scope
157 */
158 public List subList(int fromIndex, int toIndex) {
159 int size = toIndex - fromIndex;
160 Object[] newContent = new Object[size];
161 System.arraycopy(contents, fromIndex, newContent, 0, size);
162 return new SpreadList(newContent);
163 }
164
165 /**
166 * Returns the string expression of <code>this</code>.
167 *
168 * @return Returns the string expression of <code>this</code>
169 */
170 public String toString() {
171 return "*" + super.toString();
172 }
173 }