Skip to content

Commit

Permalink
Merge branch 'master' into delegatesto
Browse files Browse the repository at this point in the history
  • Loading branch information
melix committed Oct 16, 2012
2 parents 68c7a5c + d7a7c33 commit 5d9ca7f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/main/groovy/time/BaseDuration.java
Expand Up @@ -104,8 +104,11 @@ public String toString() {
if (this.hours != 0) buffer.add(this.hours + " hours");
if (this.minutes != 0) buffer.add(this.minutes + " minutes");

if (this.seconds != 0 || this.millis != 0)
buffer.add((seconds == 0 ? (millis < 0 ? "-0" : "0") : seconds) + "." + StringGroovyMethods.padLeft("" + Math.abs(millis), 3, "0") + " seconds");
if (this.seconds != 0 || this.millis != 0) {
int norm_millis = millis % 1000;
int norm_seconds = seconds + DefaultGroovyMethods.intdiv(millis - norm_millis, 1000).intValue();
buffer.add((norm_seconds == 0 ? (norm_millis < 0 ? "-0" : "0") : norm_seconds) + "." + StringGroovyMethods.padLeft("" + Math.abs(norm_millis), 3, "0") + " seconds");
}

if (buffer.size() != 0) {
return DefaultGroovyMethods.join(buffer, ", ");
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/transform/TupleConstructor.java
Expand Up @@ -47,7 +47,7 @@
* including all of the parameters - giving a no-arg constructor which can be used with the map-style naming conventions.
* <p/>
* The order of parameters is given by the properties of any super classes with most super first
* (if {@code includeSuperProperties} is set) followed by the properties of the class followed by the
* (if {@code includeSuperProperties} is set) followed by the properties of the class followed
* by the fields of the class (if {@code includeFields} is set). Within each grouping the order
* is as attributes appear within the respective class.
* <p/>
Expand Down
33 changes: 31 additions & 2 deletions src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
Expand Up @@ -3211,7 +3211,9 @@ public static boolean containsAll(Collection self, Object[] items) {
* @since 1.7.2
*/
public static boolean removeAll(Collection self, Object[] items) {
return self.removeAll(Arrays.asList(items));
Collection pickFrom = new TreeSet(new NumberAwareComparator());
pickFrom.addAll(Arrays.asList(items));
return self.removeAll(pickFrom);
}

/**
Expand All @@ -3229,7 +3231,9 @@ public static boolean removeAll(Collection self, Object[] items) {
* @since 1.7.2
*/
public static boolean retainAll(Collection self, Object[] items) {
return self.retainAll(Arrays.asList(items));
Collection pickFrom = new TreeSet(new NumberAwareComparator());
pickFrom.addAll(Arrays.asList(items));
return self.retainAll(pickFrom);
}

/**
Expand Down Expand Up @@ -5124,6 +5128,31 @@ public static <T> T getAt(Iterator<T> self, int idx) {

return null;
}

/**
* Support the subscript operator for an Iterable. Typical usage:
* <pre class="groovyTestCase">
* // custom Iterable example:
* class MyIterable implements Iterable {
* Iterator iterator() { [1, 2, 3].iterator() }
* }
* def myIterable = new MyIterable()
* assert myIterable[1] == 2
*
* // Set example:
* def set = [1,2,3] as LinkedHashSet
* assert set[1] == 2
* </pre>
*
* @param self an Iterable
* @param idx an index value (-self.size() <= idx < self.size()) but using -ve index values will be inefficient
* @return the value at the given index (after normalisation) or null if no corresponding value was found
* @since 2.1.0
*/
public static <T> T getAt(Iterable<T> self, int idx) {
return getAt(self.iterator(), idx);
}

/**
* A helper method to allow lists to work with subscript operators.
* <pre class="groovyTestCase">def list = [2, 3]
Expand Down
Expand Up @@ -1311,7 +1311,8 @@ private ClassNode checkReturnType(final ReturnStatement statement) {
);
virtualDecl.setSourcePosition(statement);
virtualDecl.visit(this);
inferred = (ClassNode) virtualDecl.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
ClassNode newlyInferred = (ClassNode) virtualDecl.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (!missesGenericsTypes(newlyInferred)) inferred = newlyInferred;
}
methodNode.putNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE, inferred);
return inferred;
Expand Down Expand Up @@ -2883,6 +2884,14 @@ private ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Method
}
}

// GROOVY-5748
if (returnType.isGenericsPlaceHolder()) {
GenericsType resolved = resolvedPlaceholders.get(returnType.getUnresolvedName());
if (resolved!=null && !resolved.isPlaceholder() && !resolved.isWildcard()) {
return resolved.getType();
}
}

GenericsType[] copy = new GenericsType[returnTypeGenerics.length];
for (int i = 0; i < copy.length; i++) {
GenericsType returnTypeGeneric = returnTypeGenerics[i];
Expand Down
7 changes: 7 additions & 0 deletions src/test/groovy/time/TimeCategoryTest.groovy
Expand Up @@ -218,4 +218,11 @@ class TimeCategoryTest extends GroovyTestCase {
}
}

void testToStringForOverflow() {
use(TimeCategory) {
def t = 800.milliseconds + 300.milliseconds
assert t.toString() == '1.100 seconds'
}
}

}
60 changes: 60 additions & 0 deletions src/test/groovy/transform/stc/GenericsSTCTest.groovy
Expand Up @@ -870,6 +870,66 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
''', '#printEqual(java.lang.Object <T>, java.util.List <T>) with arguments [int, java.util.List <java.lang.String>]'
}

void testGroovy5748() {
assertScript '''
interface IStack<T> {
INonEmptyStack<T, ? extends IStack<T>> push(T x)
}
interface IEmptyStack<T> extends IStack<T> {
INonEmptyStack<T, IEmptyStack<T>> push(T x)
}
interface INonEmptyStack<T, TStackBeneath extends IStack<T>> extends IStack<T> {
T getTop()
TStackBeneath pop()
INonEmptyStack<T, INonEmptyStack<T, TStackBeneath>> push(T x)
}
class EmptyStack<T> implements IEmptyStack<T> {
INonEmptyStack<T, IEmptyStack<T>> push(T x) {
new NonEmptyStack<T, IEmptyStack<T>>(x, this)
}
}
class NonEmptyStack<T, TStackBeneath extends IStack<T>>
implements INonEmptyStack<T, TStackBeneath> {
private final TStackBeneath stackBeneathTop;
private final T top
NonEmptyStack(T top, TStackBeneath stackBeneathTop) {
this.top = top
this.stackBeneathTop = stackBeneathTop
}
T getTop() {
top
}
TStackBeneath pop() {
stackBeneathTop
}
INonEmptyStack<T, INonEmptyStack<T, TStackBeneath>> push(T x) {
new NonEmptyStack<T, INonEmptyStack<T, TStackBeneath>>(x, this)
}
}
final IStack<Integer> stack = new EmptyStack<Integer>()
def oneInteger = stack.push(1)
assert oneInteger.getTop() == 1
def twoIntegers = stack.push(1).push(2)
assert twoIntegers.getTop() == 2
def oneIntegerAgain = stack.push(1).push(2).pop()
assert oneIntegerAgain.getTop() == 1 // BOOM!!!!
'''
}

static class MyList extends LinkedList<String> {}

public static class ClassA<T> {
Expand Down

0 comments on commit 5d9ca7f

Please sign in to comment.