Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field version of Tuple1, ... , Tuple9 #579

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/main/groovy/lang/AbstractTuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed 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.4.0
*/
public abstract class AbstractTuple extends AbstractList {
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;
}
}
40 changes: 3 additions & 37 deletions src/main/groovy/lang/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@
*/
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>
* @version $Revision$
*/
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;
}
Expand All @@ -43,36 +39,6 @@ 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;
Expand Down
47 changes: 47 additions & 0 deletions src/main/groovy/lang/Tuple1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed 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.4.0
*/
public class Tuple1<T1> extends AbstractTuple {
private final T1 first;

public Tuple1(T1 first) {
this.first = first;
}

public Object get(int index) {
switch (index) {
case 0:
return first;
default:
throw new IndexOutOfBoundsException();
}
}

public int size() {
return 1;
}

public T1 getFirst() {
return first;
}
}
32 changes: 26 additions & 6 deletions src/main/groovy/lang/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,43 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package groovy.lang;

/**
* Represents a list of 2 typed Objects.
*
* @since 2.4.0
*/
public class Tuple2<T1, T2> extends Tuple {
public class Tuple2<T1, T2> extends AbstractTuple {
private final T1 first;
private final T2 second;

public Tuple2(T1 first, T2 second) {
super(new Object[]{first, second});
this.first = first;
this.second = second;
}

public Object get(int index) {
switch (index) {
case 0:
return first;
case 1:
return second;
default:
throw new IndexOutOfBoundsException();
}
}

public int size() {
return 2;
}

@SuppressWarnings("unchecked")
public T1 getFirst() {
return (T1) get(0);
return first;
}

@SuppressWarnings("unchecked")
public T2 getSecond() {
return (T2) get(1);
return second;
}
}
63 changes: 63 additions & 0 deletions src/main/groovy/lang/Tuple3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed 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.4.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;
}

public Object get(int index) {
switch (index) {
case 0:
return first;
case 1:
return second;
case 2:
return third;
default:
throw new IndexOutOfBoundsException();
}
}

public int size() {
return 3;
}

public T1 getFirst() {
return first;
}

public T2 getSecond() {
return second;
}

public T3 getThird() {
return third;
}
}
71 changes: 71 additions & 0 deletions src/main/groovy/lang/Tuple4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed 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.4.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;
}

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();
}
}

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;
}
}
Loading