Merge remote branch 'origin/master'
This commit is contained in:
+123
@@ -0,0 +1,123 @@
|
|||||||
|
package org.jetbrains.jet.lang.descriptors;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||||
|
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||||
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
|
import org.jetbrains.jet.lang.types.TypeSubstitutor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author svtk
|
||||||
|
*/
|
||||||
|
public class FunctionDescriptorWithImplicitReceiver implements FunctionDescriptor {
|
||||||
|
|
||||||
|
private FunctionDescriptor functionDescriptor;
|
||||||
|
private DeclarationDescriptor implicitReceiver;
|
||||||
|
|
||||||
|
public FunctionDescriptorWithImplicitReceiver(@NotNull FunctionDescriptor functionDescriptor, @NotNull DeclarationDescriptor implicitReceiver) {
|
||||||
|
this.functionDescriptor = functionDescriptor;
|
||||||
|
this.implicitReceiver = implicitReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionDescriptor getFunctionDescriptor() {
|
||||||
|
return functionDescriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeclarationDescriptor getImplicitReceiver() {
|
||||||
|
return implicitReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public DeclarationDescriptor getContainingDeclaration() {
|
||||||
|
return functionDescriptor.getContainingDeclaration();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public FunctionDescriptor getOriginal() {
|
||||||
|
return functionDescriptor.getOriginal();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FunctionDescriptor substitute(TypeSubstitutor substitutor) {
|
||||||
|
return functionDescriptor.substitute(substitutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Set<? extends FunctionDescriptor> getOverriddenDescriptors() {
|
||||||
|
return functionDescriptor.getOverriddenDescriptors();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public FunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
|
||||||
|
return functionDescriptor.copy(newOwner, makeNonAbstract);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public ReceiverDescriptor getReceiverParameter() {
|
||||||
|
return ReceiverDescriptor.NO_RECEIVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public ReceiverDescriptor getExpectedThisObject() {
|
||||||
|
return ReceiverDescriptor.NO_RECEIVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<TypeParameterDescriptor> getTypeParameters() {
|
||||||
|
return functionDescriptor.getTypeParameters();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JetType getReturnType() {
|
||||||
|
return functionDescriptor.getReturnType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<ValueParameterDescriptor> getValueParameters() {
|
||||||
|
return functionDescriptor.getValueParameters();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Modality getModality() {
|
||||||
|
return functionDescriptor.getModality();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Visibility getVisibility() {
|
||||||
|
return functionDescriptor.getVisibility();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
|
||||||
|
return functionDescriptor.accept(visitor, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
|
||||||
|
functionDescriptor.acceptVoid(visitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AnnotationDescriptor> getAnnotations() {
|
||||||
|
return functionDescriptor.getAnnotations();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return functionDescriptor.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
+119
@@ -0,0 +1,119 @@
|
|||||||
|
package org.jetbrains.jet.lang.descriptors;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||||
|
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||||
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
|
import org.jetbrains.jet.lang.types.TypeSubstitutor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author svtk
|
||||||
|
*/
|
||||||
|
public class VariableDescriptorWithImplicitReceiver implements VariableDescriptor {
|
||||||
|
private VariableDescriptor variableDescriptor;
|
||||||
|
private DeclarationDescriptor implicitReceiver;
|
||||||
|
|
||||||
|
public VariableDescriptorWithImplicitReceiver(VariableDescriptor variableDescriptor, DeclarationDescriptor implicitReceiver) {
|
||||||
|
this.variableDescriptor = variableDescriptor;
|
||||||
|
this.implicitReceiver = implicitReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VariableDescriptor getVariableDescriptor() {
|
||||||
|
return variableDescriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeclarationDescriptor getImplicitReceiver() {
|
||||||
|
return implicitReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JetType getOutType() {
|
||||||
|
return variableDescriptor.getOutType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JetType getInType() {
|
||||||
|
return variableDescriptor.getInType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public DeclarationDescriptor getContainingDeclaration() {
|
||||||
|
return variableDescriptor.getContainingDeclaration();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public ReceiverDescriptor getReceiverParameter() {
|
||||||
|
return ReceiverDescriptor.NO_RECEIVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public ReceiverDescriptor getExpectedThisObject() {
|
||||||
|
return ReceiverDescriptor.NO_RECEIVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<TypeParameterDescriptor> getTypeParameters() {
|
||||||
|
return variableDescriptor.getTypeParameters();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JetType getReturnType() {
|
||||||
|
return variableDescriptor.getReturnType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public CallableDescriptor getOriginal() {
|
||||||
|
return variableDescriptor.getOriginal();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public VariableDescriptor substitute(TypeSubstitutor substitutor) {
|
||||||
|
return variableDescriptor.substitute(substitutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
|
||||||
|
return variableDescriptor.accept(visitor, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
|
||||||
|
variableDescriptor.acceptVoid(visitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<ValueParameterDescriptor> getValueParameters() {
|
||||||
|
return variableDescriptor.getValueParameters();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Set<? extends CallableDescriptor> getOverriddenDescriptors() {
|
||||||
|
return variableDescriptor.getOverriddenDescriptors();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isVar() {
|
||||||
|
return variableDescriptor.isVar();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AnnotationDescriptor> getAnnotations() {
|
||||||
|
return variableDescriptor.getAnnotations();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return variableDescriptor.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package org.jetbrains.jet.lang.resolve;
|
package org.jetbrains.jet.lang.resolve;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.collect.Collections2;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -101,19 +103,17 @@ public class ImportsResolver {
|
|||||||
if (firstPhase) {
|
if (firstPhase) {
|
||||||
if (descriptor instanceof ClassifierDescriptor) {
|
if (descriptor instanceof ClassifierDescriptor) {
|
||||||
namespaceScope.importClassifierAlias(aliasName, (ClassifierDescriptor) descriptor);
|
namespaceScope.importClassifierAlias(aliasName, (ClassifierDescriptor) descriptor);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (descriptor instanceof NamespaceDescriptor) {
|
if (descriptor instanceof NamespaceDescriptor) {
|
||||||
namespaceScope.importNamespaceAlias(aliasName, (NamespaceDescriptor) descriptor);
|
namespaceScope.importNamespaceAlias(aliasName, (NamespaceDescriptor) descriptor);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (descriptor instanceof FunctionDescriptor) {
|
if (descriptor instanceof FunctionDescriptor) {
|
||||||
namespaceScope.importFunctionAlias(aliasName, (FunctionDescriptor) descriptor);
|
namespaceScope.importFunctionAlias(aliasName, (FunctionDescriptor) descriptor);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (descriptor instanceof PropertyDescriptor) {
|
if (descriptor instanceof VariableDescriptor) {
|
||||||
namespaceScope.importVariableAlias(aliasName, (PropertyDescriptor) descriptor);
|
namespaceScope.importVariableAlias(aliasName, (VariableDescriptor) descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +167,7 @@ public class ImportsResolver {
|
|||||||
trace.report(NO_CLASS_OBJECT.on(classReference, classDescriptor));
|
trace.report(NO_CLASS_OBJECT.on(classReference, classDescriptor));
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
return lookupDescriptorsForSimpleNameReference(memberReference, classObjectType.getMemberScope());
|
return addImplicitReceiver(lookupDescriptorsForSimpleNameReference(memberReference, classObjectType.getMemberScope()), classDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -176,9 +176,25 @@ public class ImportsResolver {
|
|||||||
|
|
||||||
JetType variableType = variableDescriptor.getReturnType();
|
JetType variableType = variableDescriptor.getReturnType();
|
||||||
if (variableType == null) return Collections.emptyList();
|
if (variableType == null) return Collections.emptyList();
|
||||||
return lookupDescriptorsForSimpleNameReference(memberReference, variableType.getMemberScope());
|
return addImplicitReceiver(lookupDescriptorsForSimpleNameReference(memberReference, variableType.getMemberScope()), variableDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static Collection<DeclarationDescriptor> addImplicitReceiver(@NotNull Collection<DeclarationDescriptor> descriptors, @NotNull final DeclarationDescriptor implicitReceiver) {
|
||||||
|
return Collections2.transform(descriptors, new Function<DeclarationDescriptor, DeclarationDescriptor>() {
|
||||||
|
@Override
|
||||||
|
public DeclarationDescriptor apply(@Nullable DeclarationDescriptor descriptor) {
|
||||||
|
if (descriptor instanceof FunctionDescriptor) {
|
||||||
|
return new FunctionDescriptorWithImplicitReceiver((FunctionDescriptor) descriptor, implicitReceiver);
|
||||||
|
}
|
||||||
|
if (descriptor instanceof VariableDescriptor) {
|
||||||
|
return new VariableDescriptorWithImplicitReceiver((VariableDescriptor) descriptor, implicitReceiver);
|
||||||
|
}
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Collection<DeclarationDescriptor> lookupDescriptorsForSimpleNameReference(@NotNull JetSimpleNameExpression referenceExpression, @NotNull JetScope outerScope) {
|
private Collection<DeclarationDescriptor> lookupDescriptorsForSimpleNameReference(@NotNull JetSimpleNameExpression referenceExpression, @NotNull JetScope outerScope) {
|
||||||
List<DeclarationDescriptor> descriptors = Lists.newArrayList();
|
List<DeclarationDescriptor> descriptors = Lists.newArrayList();
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public interface WritableScope extends JetScope {
|
|||||||
|
|
||||||
void addVariableDescriptor(@NotNull VariableDescriptor variableDescriptor);
|
void addVariableDescriptor(@NotNull VariableDescriptor variableDescriptor);
|
||||||
|
|
||||||
void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor);
|
void addPropertyDescriptor(@NotNull VariableDescriptor propertyDescriptor);
|
||||||
|
|
||||||
void addFunctionDescriptor(@NotNull FunctionDescriptor functionDescriptor);
|
void addFunctionDescriptor(@NotNull FunctionDescriptor functionDescriptor);
|
||||||
|
|
||||||
@@ -52,5 +52,5 @@ public interface WritableScope extends JetScope {
|
|||||||
|
|
||||||
void importFunctionAlias(@NotNull String aliasName, @NotNull FunctionDescriptor functionDescriptor);
|
void importFunctionAlias(@NotNull String aliasName, @NotNull FunctionDescriptor functionDescriptor);
|
||||||
|
|
||||||
void importVariableAlias(@NotNull String aliasName, @NotNull PropertyDescriptor variableDescriptor);
|
void importVariableAlias(@NotNull String aliasName, @NotNull VariableDescriptor variableDescriptor);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importVariableAlias(@NotNull String aliasName, @NotNull PropertyDescriptor variableDescriptor) {
|
public void importVariableAlias(@NotNull String aliasName, @NotNull VariableDescriptor variableDescriptor) {
|
||||||
checkMayWrite();
|
checkMayWrite();
|
||||||
|
|
||||||
addPropertyDescriptor(variableDescriptor);
|
addPropertyDescriptor(variableDescriptor);
|
||||||
@@ -173,7 +173,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
|
public void addPropertyDescriptor(@NotNull VariableDescriptor propertyDescriptor) {
|
||||||
addVariableDescriptor(propertyDescriptor, true);
|
addVariableDescriptor(propertyDescriptor, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -193,7 +193,7 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importVariableAlias(@NotNull String aliasName, @NotNull PropertyDescriptor variableDescriptor) {
|
public void importVariableAlias(@NotNull String aliasName, @NotNull VariableDescriptor variableDescriptor) {
|
||||||
checkMayWrite();
|
checkMayWrite();
|
||||||
|
|
||||||
getCurrentIndividualImportScope().addVariableAlias(aliasName, variableDescriptor);
|
getCurrentIndividualImportScope().addVariableAlias(aliasName, variableDescriptor);
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ public class WriteThroughScope extends WritableScopeWithImports {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
|
public void addPropertyDescriptor(@NotNull VariableDescriptor propertyDescriptor) {
|
||||||
checkMayWrite();
|
checkMayWrite();
|
||||||
|
|
||||||
writableWorker.addPropertyDescriptor(propertyDescriptor);
|
writableWorker.addPropertyDescriptor(propertyDescriptor);
|
||||||
|
|||||||
@@ -7,10 +7,20 @@ import b.ext //extension function
|
|||||||
import b.value //property
|
import b.value //property
|
||||||
import b.C.bar //function from class object
|
import b.C.bar //function from class object
|
||||||
import b.C.cValue //property from class object
|
import b.C.cValue //property from class object
|
||||||
|
import b.constant.fff //function from val
|
||||||
|
import b.constant.dValue //property from val
|
||||||
|
import b.E.f //val from class object
|
||||||
|
|
||||||
fun test(arg: B) {
|
fun test(arg: B) {
|
||||||
foo(value)
|
foo(value)
|
||||||
arg.ext()
|
arg.ext()
|
||||||
|
|
||||||
|
bar()
|
||||||
|
foo(cValue)
|
||||||
|
|
||||||
|
fff(dValue)
|
||||||
|
|
||||||
|
f.f()
|
||||||
}
|
}
|
||||||
|
|
||||||
//FILE:b.kt
|
//FILE:b.kt
|
||||||
@@ -18,7 +28,7 @@ package b
|
|||||||
|
|
||||||
class B() {}
|
class B() {}
|
||||||
|
|
||||||
fun foo(<!UNUSED_PARAMETER!>i<!>: Int) {}
|
fun foo(i: Int) = i
|
||||||
|
|
||||||
fun B.ext() {}
|
fun B.ext() {}
|
||||||
|
|
||||||
@@ -29,4 +39,21 @@ class C() {
|
|||||||
fun bar() {}
|
fun bar() {}
|
||||||
val cValue = 1
|
val cValue = 1
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class D() {
|
||||||
|
fun fff(s: String) = s
|
||||||
|
val dValue = "w"
|
||||||
|
}
|
||||||
|
|
||||||
|
val constant = D()
|
||||||
|
|
||||||
|
class E() {
|
||||||
|
class object {
|
||||||
|
val f = F()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class F() {
|
||||||
|
fun f() {}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package std.util
|
||||||
|
// Number of extension function for ava.lang.Iterable that shouldn't participate in auto generation
|
||||||
|
|
||||||
|
import java.util.Collection
|
||||||
|
import java.util.List
|
||||||
|
import java.util.AbstractList
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count the number of elements in collection.
|
||||||
|
*
|
||||||
|
* If base collection implements Collection<T> interface method Collection<T>#size() will be used.
|
||||||
|
* Otherwise, this method determines the count by iterating through the all items.
|
||||||
|
*/
|
||||||
|
fun <T> java.lang.Iterable<T>.count() : Int {
|
||||||
|
if (this is Collection<T>) {
|
||||||
|
return this.size()
|
||||||
|
}
|
||||||
|
|
||||||
|
var number : Int = 0
|
||||||
|
for (elem in this) {
|
||||||
|
++number
|
||||||
|
}
|
||||||
|
return number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first element in the collection.
|
||||||
|
*
|
||||||
|
* Will throw an exception if there are no elements
|
||||||
|
* TODO: Specify type of the exception
|
||||||
|
*/
|
||||||
|
inline fun <T> java.lang.Iterable<T>.first() : T {
|
||||||
|
if (this is AbstractList<T>) {
|
||||||
|
return this.get(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.iterator().sure().next()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the last element in the collection.
|
||||||
|
*
|
||||||
|
* If base collection implements List<T> interface, the combination of size() and get()
|
||||||
|
* methods will be used for getting last element. Otherwise, this method determines the
|
||||||
|
* last item by iterating through the all items.
|
||||||
|
*
|
||||||
|
* Will throw an exception if there are no elements.
|
||||||
|
* TODO: Specify type of the exception
|
||||||
|
*/
|
||||||
|
fun <T> java.lang.Iterable<T>.last() : T {
|
||||||
|
if (this is List<T>) {
|
||||||
|
return this.get(this.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
val iterator = this.iterator().sure()
|
||||||
|
var last : T = iterator.next()
|
||||||
|
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
last = iterator.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if collection contains given item.
|
||||||
|
*
|
||||||
|
* Method checks equality of the objects with T.equals method.
|
||||||
|
* If collection implements java.util.AbstractCollection an overridden implementation of the contains
|
||||||
|
* method will be used.
|
||||||
|
*/
|
||||||
|
fun <T> java.lang.Iterable<T>.contains(item : T) : Boolean {
|
||||||
|
std.io.println("!!!!!")
|
||||||
|
if (this is java.util.AbstractCollection<T>) {
|
||||||
|
return this.contains(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var elem in this) {
|
||||||
|
if (elem.equals(item)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -81,6 +81,14 @@ fun assert(actual: Boolean, message: String) {
|
|||||||
println("Answer: ${actual} for ${message}")
|
println("Answer: ${actual} for ${message}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun assertTrue(actual: Boolean, message: String = "") {
|
||||||
|
return assertEquals(true, actual, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assertFalse(actual: Boolean, message: String = "") {
|
||||||
|
return assertEquals(false, actual, message)
|
||||||
|
}
|
||||||
|
|
||||||
fun assertEquals(expected: Any, actual: Any?, message: String = "") {
|
fun assertEquals(expected: Any, actual: Any?, message: String = "") {
|
||||||
Assert.assertEquals(message, expected, actual)
|
Assert.assertEquals(message, expected, actual)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,16 @@ import std.test.*
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class CollectionTest() : TestSupport() {
|
class CollectionTest() : TestSupport() {
|
||||||
|
|
||||||
|
class IterableWrapper<T>(collection : java.lang.Iterable<T>) : java.lang.Iterable<T> {
|
||||||
|
private val collection = collection
|
||||||
|
|
||||||
|
override fun iterator(): java.util.Iterator<T> {
|
||||||
|
return collection.iterator().sure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
val data = arrayList("foo", "bar")
|
val data = arrayList("foo", "bar")
|
||||||
|
|
||||||
fun testAny() {
|
fun testAny() {
|
||||||
@@ -177,4 +187,39 @@ class CollectionTest() : TestSupport() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testSimpleCount() {
|
||||||
|
assertEquals(2, data.count())
|
||||||
|
assertEquals(3, hashSet(12, 14, 15).count())
|
||||||
|
assertEquals(0, ArrayList<Double>().count())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testLast() {
|
||||||
|
assertEquals("bar", data.last())
|
||||||
|
assertEquals(25, arrayList(15, 19, 20, 25).last())
|
||||||
|
// assertEquals(19, TreeSet(arrayList(90, 47, 19)).first())
|
||||||
|
assertEquals('a', linkedList('a').last())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testLastException() {
|
||||||
|
fails { arrayList<Int>().last() }
|
||||||
|
fails { linkedList<String>().last() }
|
||||||
|
fails { hashSet<Char>().last() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testContains() {
|
||||||
|
assertTrue(data.contains("foo"))
|
||||||
|
assertTrue(data.contains("bar"))
|
||||||
|
assertFalse(data.contains("some"))
|
||||||
|
|
||||||
|
// TODO: Problems with generation
|
||||||
|
// assertTrue(IterableWrapper(data).contains("bar"))
|
||||||
|
// assertFalse(IterableWrapper(data).contains("some"))
|
||||||
|
|
||||||
|
assertFalse(hashSet<Int>().contains(12))
|
||||||
|
assertTrue(linkedList(15, 19, 20).contains(15))
|
||||||
|
|
||||||
|
// assertTrue(IterableWrapper(hashSet(45, 14, 13)).contains(14))
|
||||||
|
// assertFalse(IterableWrapper(linkedList<Int>()).contains(15))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user