-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Field version of Tuple1, Tuple3, ... , Tuple9
- Loading branch information
1 parent
a86a4f1
commit 0dd461d
Showing
11 changed files
with
786 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package groovy.lang; | ||
|
||
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; | ||
|
||
import java.util.AbstractList; | ||
|
||
/** | ||
* @since 2.5.0 | ||
*/ | ||
public abstract class AbstractTuple<E> extends AbstractList<E> { | ||
private int hashCode; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || !(o instanceof AbstractTuple)) return false; | ||
|
||
AbstractTuple that = (AbstractTuple) o; | ||
if (size() != that.size()) return false; | ||
for (int i = 0; i < size(); i++) { | ||
if (!DefaultTypeTransformation.compareEqual(get(i), that.get(i))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (hashCode == 0) { | ||
for (int i = 0; i < size(); i++) { | ||
Object value = get(i); | ||
int hash = (value != null) ? value.hashCode() : 0xbabe; | ||
hashCode ^= hash; | ||
} | ||
if (hashCode == 0) { | ||
hashCode = 0xbabe; | ||
} | ||
} | ||
return hashCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,63 +18,31 @@ | |
*/ | ||
package groovy.lang; | ||
|
||
import java.util.AbstractList; | ||
import java.util.List; | ||
|
||
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; | ||
|
||
/** | ||
* Represents a list of Objects. | ||
* | ||
* @author <a href="mailto:[email protected]">James Strachan</a> | ||
*/ | ||
public class Tuple extends AbstractList { | ||
public class Tuple extends AbstractTuple { | ||
private final Object[] contents; | ||
private int hashCode; | ||
|
||
public Tuple(Object[] contents) { | ||
public Tuple(Object... contents) { | ||
if (contents == null) throw new NullPointerException(); | ||
this.contents = contents; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return contents[index]; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return contents.length; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || !(o instanceof Tuple)) return false; | ||
|
||
Tuple that = (Tuple) o; | ||
if (size() != that.size()) return false; | ||
for (int i = 0; i < contents.length; i++) { | ||
if (!DefaultTypeTransformation.compareEqual(contents[i], that.contents[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (hashCode == 0) { | ||
for (int i = 0; i < contents.length; i++ ) { | ||
Object value = contents[i]; | ||
int hash = (value != null) ? value.hashCode() : 0xbabe; | ||
hashCode ^= hash; | ||
} | ||
if (hashCode == 0) { | ||
hashCode = 0xbabe; | ||
} | ||
} | ||
return hashCode; | ||
} | ||
|
||
@Override | ||
public List subList(int fromIndex, int toIndex) { | ||
int size = toIndex - fromIndex; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package groovy.lang; | ||
|
||
/** | ||
* Represents a list of 1 typed Object. | ||
* | ||
* @since 2.5.0 | ||
*/ | ||
public class Tuple1<T1> extends AbstractTuple { | ||
private final T1 first; | ||
|
||
public Tuple1(T1 first) { | ||
this.first = first; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
switch (index) { | ||
case 0: | ||
return first; | ||
default: | ||
throw new IndexOutOfBoundsException("index: " + index); | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 1; | ||
} | ||
|
||
public T1 getFirst() { | ||
return first; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package groovy.lang; | ||
|
||
/** | ||
* Represents a list of 3 typed Objects. | ||
* | ||
* @since 2.5.0 | ||
*/ | ||
public class Tuple3<T1, T2, T3> extends AbstractTuple { | ||
private final T1 first; | ||
private final T2 second; | ||
private final T3 third; | ||
|
||
public Tuple3(T1 first, T2 second, T3 third) { | ||
this.first = first; | ||
this.second = second; | ||
this.third = third; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
switch (index) { | ||
case 0: | ||
return first; | ||
case 1: | ||
return second; | ||
case 2: | ||
return third; | ||
default: | ||
throw new IndexOutOfBoundsException("index: " + index); | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 3; | ||
} | ||
|
||
public T1 getFirst() { | ||
return first; | ||
} | ||
|
||
public T2 getSecond() { | ||
return second; | ||
} | ||
|
||
public T3 getThird() { | ||
return third; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package groovy.lang; | ||
|
||
/** | ||
* Represents a list of 4 typed Objects. | ||
* | ||
* @since 2.5.0 | ||
*/ | ||
public class Tuple4<T1, T2, T3, T4> extends AbstractTuple { | ||
private final T1 first; | ||
private final T2 second; | ||
private final T3 third; | ||
private final T4 fourth; | ||
|
||
public Tuple4(T1 first, T2 second, T3 third, T4 fourth) { | ||
this.first = first; | ||
this.second = second; | ||
this.third = third; | ||
this.fourth = fourth; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
switch (index) { | ||
case 0: | ||
return first; | ||
case 1: | ||
return second; | ||
case 2: | ||
return third; | ||
case 3: | ||
return fourth; | ||
default: | ||
throw new IndexOutOfBoundsException("index: " + index); | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 4; | ||
} | ||
|
||
public T1 getFirst() { | ||
return first; | ||
} | ||
|
||
public T2 getSecond() { | ||
return second; | ||
} | ||
|
||
public T3 getThird() { | ||
return third; | ||
} | ||
|
||
public T4 getFourth() { | ||
return fourth; | ||
} | ||
} |
Oops, something went wrong.
0dd461d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge groovy/groovy-core#579 with some tweaks.