Skip to content

Commit

Permalink
Field version of Tuple1, Tuple3, ... , Tuple9
Browse files Browse the repository at this point in the history
  • Loading branch information
yukoba authored and daniellansun committed Sep 16, 2017
1 parent a86a4f1 commit 0dd461d
Show file tree
Hide file tree
Showing 11 changed files with 786 additions and 42 deletions.
61 changes: 61 additions & 0 deletions src/main/groovy/lang/AbstractTuple.java
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;
}
}
40 changes: 4 additions & 36 deletions src/main/groovy/lang/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 51 additions & 0 deletions src/main/groovy/lang/Tuple1.java
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;
}
}
31 changes: 25 additions & 6 deletions src/main/groovy/lang/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,37 @@
/**
* Represents a list of 2 typed Objects.
*/
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;
}

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

@Override
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;
}
}
68 changes: 68 additions & 0 deletions src/main/groovy/lang/Tuple3.java
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;
}
}
76 changes: 76 additions & 0 deletions src/main/groovy/lang/Tuple4.java
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;
}
}
Loading

1 comment on commit 0dd461d

@daniellansun
Copy link
Contributor

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.

Please sign in to comment.